OOP – Assignment V

Lab Manual Download : Write some simple programs in Java such as i) To find factorial of number. ii) To display first 50 prime numbers.  iii) To find sum and average of N numbers.

ASSIGNMENT NO 5

Title: Develop a program to demonstrate working of looping constructs, methods in Java

Objectives: To learn looping constructs, methods in Java.

Problem Statement:   Write some simple programs in Java such as

  1. To find factorial of number.
  2. To display first 50 prime numbers
  3. To find sum and average of N numbers.

Outcomes: Students will be able to demonstrate use of data types, looping constructs, methods, and switch statements in Java.

Hardware requirements: Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more.

Software requirements:  64 bit Linux/Windows Operating System, JDK 1.4 or later

Theory:  In any programming language, loops are used to execute a set of statements repeatedly until a particular condition is satisfied.

A sequence of statements are executed until a specified condition is true. This sequence of statements to be executed is kept inside the curly braces { } known as the Loop body. After every execution of loop body, condition is verified, and if it is found to be true the loop body is executed again. When the condition check returns false, the loop body is not executed. Java programming language provides the following types of loop to handle looping requirements.

Sr.No. Loop & Description
1 while loop : Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
2 for loop : Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
3 do…while loop : Like a while statement, except that it tests the condition at the end of the loop body.

Loop Control Statements: Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Java supports the following control statements.

Sr.No. Control Statement & Description
1 break statement : Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
2 continue statement: Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Following is the general form of a typical decision making structure found in most of the programming languages −

Java programming language provides following types of decision making statements.

Sr.No. Statement & Description
1 if statement: An if statement consists of a boolean expression followed by one or more statements.
2 if…else statement: An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
3 nested if statement: You can use one if or else if statement inside another if or else if statement(s).
4 switch statement: A switch statement allows a variable to be tested for equality against a list of values.

Conclusion : In this assignment, we have studied data types, operators, switch statement and methods in Java and demonstrated their use in program.

Roll No. Name of Student Date of Performance Date of Assessment Grade Sign of Student Sign of Faculty

Conclusion : In this assignment, we have studied data types, operators, switch statement and methods in Java and demonstrated their use in program.

Program

class AvgFactPrime {
 public static void main(String args[])
 {
 System.out.println("*****************MENU******************");
 System.out.println("1.\tSum and Avg\n2.\tFactorial\n3.\tPrime");
 System.out.println("Enter your choice");
 int choice=Integer.parseInt(System.console().readLine());
 System.out.println("Enter a number");
 int num=Integer.parseInt(System.console().readLine());
 switch(choice)
 {
 case 1:
 int result=0;
 for(int i=1;i<=num;i++) 
 result=result+i;
 System.out.println("Addition of first "+num+" numbers is "+result);
 double avg=result/(double)num;
 System.out.println("Average of sum of first "+num+" numbers is "+avg);
 break;
 case 2:
 System.out.println("Factorial of first "+num+" numbers:");
 for(int i=1;i<=num;i++)
 {
 int fact=1;
 for(int j=1;j<=i;j++)
 fact=fact*j;
 System.out.println(i+"! = "+fact);
 }
 break;
 case 3:
 System.out.println("Prime numbers between 1 to "+num);
 for(int i=1;i<=num;i++)
 {
 int j,flag=0,x=0;
 x=i/2;
 for(j=2;j<=x;j++)
 {
 if(i%j==0)
 {
 flag=1;
 break;
 }
 }
 if(flag==0)
 System.out.print(i+"\t");
 }
 break;
 default:
 System.out.println("Sorry !!! Wrong Choice");
 }
 }
}

OUTPUT:

ass-i-op2ass-i-op

Leave a Reply

Your email address will not be published. Required fields are marked *