0% found this document useful (0 votes)
6 views22 pages

CHAPTER9

The document contains a series of assignment questions and answers related to iterative constructs in Java, covering topics such as loop control structures, differences between break and continue statements, and various loop types. It includes code examples for converting loops, calculating sums, and displaying patterns, as well as explanations of common programming concepts. Additionally, it provides programming tasks for calculating the Tribonacci series, Pi value, and checking for Pronic numbers.

Uploaded by

Borra ram kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

CHAPTER9

The document contains a series of assignment questions and answers related to iterative constructs in Java, covering topics such as loop control structures, differences between break and continue statements, and various loop types. It includes code examples for converting loops, calculating sums, and displaying patterns, as well as explanations of common programming concepts. Additionally, it provides programming tasks for calculating the Tribonacci series, Pi value, and checking for Pronic numbers.

Uploaded by

Borra ram kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CHAPTER 9

Iterative Constructs in Java


Section 3: Assignment Questions

1. What are loop control structures? What are the essential segments of a loop control structure?
Ans. A loop is a set of instructions that is continually repeated until a certain condition is met. The set of
instructions may be repeated any number of times, as per the requirement.
Essential parts of a looping control structure are:
(i) initialization
(ii) test – condition
(iii) update – next step
(iv) body of the loop

2. How are these statements different from each other?


i. break ii. continue iii. System.exit(0) iv. return
Ans.
(i) The break statement terminates the current loop or switch statement. The execution then continues
from the statement immediately following the current loop or switch statement.

(ii) The continue statement skips the rest of the current iteration of the loop. It jumps back to the
beginning of the loop and continues with the next iteration, including the evaluation of loop controlling
condition.

(iii) System.exit(0) method exits current program by terminating running Java virtual machine.

(iv) The return keyword causes the control to transfer back to the method call.

3. Identify all the errors in the following statements.


i. for (int i = 5; i > 0; i++) {
System.out.println("Java is fun!"); }
Ans. It is an infinite loop. The value of i will be greater than 0 everytime.

ii. while (x < 1 && x > 50) {


a = b; }
www.bhuvantechs.com

Ans.
a. The condition is logically incorrect.
b. Value of x is never changed resulting into an infinite loop.

iii. while (x == y) ;
{ xx = yy;
x = y; }
Ans. a. While statement should not have a statement terminator (;).
b. Increment counters missing.
4. What is an empty statement? Explain its usefulness.
Ans. An empty statement is any statement that is purported to provide information, but in reality it
provides no information at all in the relevant conversational context. It contains a null statement i.e.,
just a semicolon (;).

5. Convert the following for loop statement into the corresponding while loop and do-while loop:
int sum = 0; for (int i = 0; i <= 50; i++) sum = sum + i;
Ans.
while loop do-while loop

int sum=0,i=0; int sum=0,i=0;


while ( i<=50 ) do {
{ sum=sum+i;
sum=sum+i; i++;
i++; } while ( i<=50 );
}

6. What are the differences between while loop and do-while loop?
Ans. In while loop, control checks for the condition and then executes the loop. If condition is true the
loop is executed and if it is false the loop will not be executed.

In do while loop, the loop is executed once and then the control checks for the condition for further
execution. Even if the condition is not met, the do-while loop is always executed once.

7. How many times are the following loop bodies repeated? What is the fi nal output in each case?
i. int x = 2; ii. int y = 2;
while (x < 20) while (y < 20)
if (x % 2 == 0) if (y % 2 == 0)
System.out.println(x); System.out.println(y++);
Ans. Infinite loop. Ans. Infinite loop.

iii. int z = 2;
while (z < 20)
if ((z++) % 2 == 0)
System.out.println(z);
Ans. The loop body is executed 9 times.
Output
3
www.bhuvantechs.com

5
7
9
11
13
15
17
19

Iterative Constructs in Java ~2~


8. What is the output produced by the following code?
int n = 20;
do {
System.out.println(n);
n = n - 3;
} while (n > 0);
Ans. Output
20
17
14
11
8
5
2

9. What is the output produced by the following code?


int num = 20; while (num > 0) {
num = num - 2;
if (num == 4)
break ;
System.out.println(num); }
System.out.println("Finished");
Ans. Output
18
16
14
12
10
8
6
Finished

10. What is the output produced by the following code?


int num = 10;
while (num > 0)
{
num = num - 2;
if (num == 2)
continue ;
www.bhuvantechs.com

System.out.println(num);
}
System.out.println("Finished");
Ans. Output
8
6
4
0
Finished

Iterative Constructs in Java ~3~


11. Write a program to input n number of integers and find out:
i. Number of positive integers
ii. Number of negative integers
iii. Sum of positive numbers
iv. Product of negative numbers
v. Average of positive numbers
Ans. import java.util.Scanner;
public class CountNumbers {
public static void main(String[] args) {
int num,pCount=0,nCount=0,sum=0,i,prod=1,avg;Scanner input=new Scanner(System.in);
System.out.println("Enter the total numbers "); int count=input.nextInt();
for (i = 1; i < count+1; i++){
System.out.println("Enter the number "); num=input.nextInt();
if(num >= 0) {
pCount++;
sum=sum+num; }
else {
prod=prod*num;
nCount++; }
}
avg=sum/pCount;
System.out.println("\n Total Positive Numbers " + pCount);
System.out.println("\n Total Negative Numbers " + nCount);
System.out.println("\n Total sum of positive Numbers " + sum);
System.out.println("\n Total product of Negative Numbers " + prod);
System.out.println("\n Average of positive Numbers " + avg); }
}

12. Write a program using do – while loop to compute the sum of the first 500 positive odd integers.
Ans. import java.util.Scanner;
public class CountoddNumbers
{
public static void main(String[] args)
{
int num=1,sum=0;
do {
if(num % 2 != 0)
{
sum=sum+num;
www.bhuvantechs.com

System.out.println(num + " " + sum);


}
num++;
}while(num<501);
System.out.println("\n The Sum of first 500 positive Odd Numbers "+sum);
}
}

Iterative Constructs in Java ~4~


13. Write three different programs using for, while, and do-while loops to find the product of series
3, 9, 12, … … , 30.
Ans.
import java.util.Scanner;
public class product
{
public static void main(String[] args)
{
int num=3,prod=1;
do {
if(num % 3== 0){
prod=prod*num; }
num=num+3;
}while(num<31);
System.out.println("Product with do while loop= " + prod);
prod=1;
for(num=3; num<=30;num=num+3)
prod=prod*num;
System.out.println("Product with for loop= " + prod);
prod=1;
num=3;
while(num<=30) {
if(num % 3== 0)
{
prod=prod*num;
}
num=num+3; }
System.out.println("Product with while loop= " + prod);
}
}

14. Write a program to convert kilograms to pounds in the following tabular format
(1 kilogram is 2.2 pounds):
Ans.
import java.util.Scanner;
public class pound
{
public static void main(String[] args)
{
www.bhuvantechs.com

double pou,i;
System.out.println("KILOGRAM POUND");
for (i=1.0;i<=20.0;i=i+1)
{
pou=i*2.2;
System.out.println(i+" "+pou);
}
}
}

Iterative Constructs in Java ~5~


15. Write a program that displays all the numbers from 150 to 250 that are divisible by 5 or 6, but not
both.
Ans.
public class divisible {
public static void main(String args[]) {
System.out.println("\nDivided by 5: ");
for (int i=150; i<=250; i++)
{
if (i%5==0)
System.out.print(i +", ");
}
System.out.println("\n\nDivided by 6: ");
for (int i=150; i<=250; i++)
{
if (i%6==0)
System.out.print(i +", ");
}
System.out.println("\n\nnot Divided by 5 & 6: ");
for (int i=150; i<=250; i++)
{
if (i%5!=0 && i%6!=0)
System.out.print(i +", ");
}
System.out.println("\n"); }
}

16. Write a program in Java to read a number and display its digits in the reverse order. For example,
if the input number is
2468, then the output should be 8642.
Enter a number: 2468
Original number: 2468
Reverse number: 8642
Ans.
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter the number");
int num=input.nextInt();
www.bhuvantechs.com

int reversed = 0;
while(num != 0)
{
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number: " + reversed); }
}

Iterative Constructs in Java ~6~


17. Write a program in Java to read a number, remove all zeros from it, and display the new number.
For example,
Sample Input: 45407703 Sample Output: 454773
Ans. import java.util.Scanner;
public class Zero {
public static void main(String args[]) {
int rem=0,p,x,z=0,r=0,rev=0; Scanner sc = new Scanner(System.in);
System.out.print("Enter number till u want Tribonacci series: ");
int n=sc.nextInt();
while(n>0) {
rem=n%10;
if(rem==0)
{
x=rem;
}
else
{
p=rem;
rev=rev*10+p;
}
n=n/10; }
while(rev>0) {
z=rev%10;
r=r*10+z;
rev=rev/10; }
System.out.println("The new no.= "+r); }
}

18. Write a program to read the number n using the Scanner class and print the Tribonacci series:
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81 …and so on.
[Hint: The Tribonacci series is a generalisation of the Fibonacci sequence where each term is the sum
of the three preceding terms.]
Ans. import java.util.Scanner;
public class Tribonacci {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number till u want Tribonacci series: ");
int n=sc.nextInt(); int a = 0,b = 0,c = 1;
int d = a+b+c;System.out.println("\nTribonacci Series: ");
www.bhuvantechs.com

System.out.print(a+"\t"+b+"\t"+c);
for(int i=4;i<=n;i++){
System.out.print("\t"+d);
a=b;
b=c;
c=d;
d=a+b+c; }
}
}

Iterative Constructs in Java ~7~


19. Write a program to calculate the value of Pi with the help of the following series:
Pi = (4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15) ...
Hint: Use while loop with 100000 iterations.
Ans. import java.util.Scanner;
public class LeibnizFormula {
public static void main(String args[]) {
int x=1; double pi = 0,deno=1;
for (x = 1; x <= 100000; x++) {
if (x % 2 != 0) {
pi = pi + (4 / deno); }
else {
pi = pi - (4 / deno); }
deno=deno+2; }
System.out.println(pi); }
}

20. Write a program to display the following patterns as per the user’s choice.
Pattern 1 Pattern 2
II
CC
SS
EE
Ans. Import java.util.Scanner;
public class pattern
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Pattern 1 Pattern 2");
System.out.println(" I I");
System.out.println(" C C");
System.out.println(" S S");
System.out.println(" E E");
System.out.print("Enter the pattern number to view: ");
int n=sc.nextInt();
if(n==1)
{
System.out.println("Pattern 1");
System.out.println(" I ");
www.bhuvantechs.com

System.out.println(" C ");
System.out.println(" S ");
System.out.println(" E ");
}
if(n==2)
{
System.out.println(" Pattern 2");
System.out.println(" I");
System.out.println(" C");

Iterative Constructs in Java ~8~


System.out.println(" S");
System.out.println(" E");
}}
}

21. Write a menu-driven program to display the pattern as the per user’s choice:
Pattern 1 Pattern 2
ABCDE B
ABCD LL
ABC UUU
AB EEEE
A
For an incorrect option, an appropriate error message should be displayed. [ICSE-2018]
Ans. import java.util.Scanner;
public class pat
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("**************MENU*************");
System.out.println("Type 1 for Pattern 1 and ");
System.out.println("Type 2 for Pattern 2");
System.out.print("Enter the number: ");
int n=sc.nextInt();
char i,j;
String word="BLUE";
switch(n)
{
case 1:
for(i='E'; i>='A'; i--)
{
for(j='A'; j<=i; j++)
{
System.out.print(j+"\t");
}
System.out.println();
}
break;
case 2:
www.bhuvantechs.com

for(i=0; i<=3; i++)


{
for(j=0; j<=i; j++)
{
System.out.print(word.charAt(i)+"\t");
}
System.out.println("");
}
break;

Iterative Constructs in Java ~9~


default:
System.out.println("Wrong choice");
}
}
}

22. Write a program to input a number and check and print whether it is a Pronic number or not.
(Pronic number is a number which is the product of two consecutive integers.)
Example: 12 = 3 × 4
20 = 4 × 5
42 = 6 × 7 [ICSE-2018]
Ans. import java.util.Scanner;
public class pronicnumber {
public static void main(String args[]){
Scanner sc = new Scanner(System.in); int flag = 0;
System.out.print("Enter a number : "); int n = sc.nextInt();
for(int i=0; i<n; i++) {
if(i*(i+1) == n) {
flag = 1;
break; }
}
if(flag == 1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number."); }
}

23. Write a program to accept a number and check and display whether it is a spy number or not.
(A number is called a spy number if the sum of its digits equals the product of the digits.)
Example: Consider the number 1124.
Sum of the digits = 1 + 1 + 2 + 4 = 8.
Product of the digits = 1 * 1 * 2 * 4 = 8. [ICSE-2017]
Ans. import java.util.Scanner;
public class SpyNumber {
static boolean isSpy( int num) {
int sum=0; int product = 1; int saveNum = num;
while (num != 0) {
int digit = num % 10; sum = sum + digit;
product = product * digit; num = num / 10; }
www.bhuvantechs.com

return(sum == product) ; }
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer (-1 to exit)");int number = keyboard.nextInt();
while (number != -1){
if (isSpy(number))
System.out.println(number + " is a Spy number");
else
System.out.println(number + " is NOT a Spy number");

Iterative Constructs in Java ~ 10 ~


System.out.println("Enter an integer (-1 to exit)"); number = keyboard.nextInt(); }
System.out.println("Exiting..."); keyboard.close(); }
}

24. Write a program to accept a number and check and display whether it is a Niven number or not.
(Niven number is anumber which is divisible by the sum of its digits).
Example:
Consider the number 126.
Sum of its digits is 1 + 2 + 6 = 9 and 126 is divisible by 9. [ICSE-2016]
Ans. import java.util.Scanner;
public class NivenNumber {
static boolean isNiven( int num) {
int sumOfDigits=0; int temp = num;
while(temp != 0) {
int digit = temp % 10;
sumOfDigits = sumOfDigits + digit;
temp = temp / 10; }
return(num % sumOfDigits == 0) ; }
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer (0 to exit)"); int number = keyboard.nextInt();
while (number != 0) {
if (isNiven(number))
System.out.println(number + " is a Niven number");
else
System.out.println(number + " is NOT a Niven number");
System.out.println("Enter an integer (0 to exit)"); number = keyboard.nextInt(); }
System.out.println("Exiting..."); keyboard.close(); }
}

25. Using the switch statement, write a menu driven program to:
i. Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5…. The first two
Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.
ii. Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, an appropriate error message should be displayed. [ICSE-2012]
Ans. import java.util.Scanner;
www.bhuvantechs.com

public class menu


{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("**************MENU*************");
System.out.println("Type 1 for Fibonacci series ");
System.out.println("Type 2 for sum of digits of an integer");
System.out.print("Enter your choice: ");

Iterative Constructs in Java ~ 11 ~


int ch=sc.nextInt();
switch(ch)
{
case 1:
int n = 10, t1 = 0, t2 = 1;
System.out.print("First " + n + " terms: ");
for (int i = 1; i <= n; ++i)
{
System.out.print(t1 + " + ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
break;
case 2:
int m, z, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
m = s.nextInt();
while(m > 0)
{
z = m % 10;
sum = sum + z;
m = m / 10;
}
System.out.println("Sum of Digits:"+sum);
break;
default:
System.out.println("Wrong choice");
}
}
}

26. Write a menu driven program to accept a number and check and display whether (i) it is a Prime
Number or not (ii) it is an Automorphic Number or not. (Use switch-case statement).
i. Prime number: A number is said to be a prime number if it is divisible only by 1 and itself and not by
any other number. Example: 3, 5, 7, 11, 13 etc.
ii. Automorphic number: An automorphic number is the number which is contained in the last digit(s)
of its square. Example: 25 is an automorphic number as its square is 625 and 25 is present as the last
www.bhuvantechs.com

two digits. [ICSE-2010]


Ans. import java.util.Scanner;
public class PrimeAutomorphicnumber
{
public static void main(String args[])
{
int i,ch,a,no,m,f=1,c=0,p;
Scanner ob=new Scanner(System.in);
System.out.println("1. For Prime number");

Iterative Constructs in Java ~ 12 ~


System.out.println("2. For Automorphic number");
System.out.print("Enter your choice:");
ch=ob.nextInt();
switch(ch)
{
case 1:
System.out.print("Enter number to check prime:");
no=ob.nextInt();
for(i=2;i<no;i++)
{
if(no%i==0)
{
f=0;
break;
}
}
if(f==1)
System.out.print("Prime number");
else
System.out.print("Not Prime number");
break;
case 2:
System.out.print("Enter number to check Automorphic:");
no=ob.nextInt();
m=no;
while(m!=0)
{
m/=10;
c++;
}
int s=m*m;
int g=0;
if(s>9 && s<=99)
{
g=s%10;
}
else if(s>99 && s<10000)
{
g=s%100;
www.bhuvantechs.com

}
if(g==no)
System.out.println("Automorphic number");
else
System.out.println("Not Automorphic number");
break;
default:
System.out.println("Wrong choice");
}

Iterative Constructs in Java ~ 13 ~


}
}

27. Write a menu driven program to accept a number from the user and check whether it is a BUZZ
number or to accept any two numbers and to print the GCD of them.
i. A BUZZ number is the number which either ends with 7 or is divisible by 7.
ii. GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide
the larger number by the smaller; the remainder then divides the previous divisor. The process is
repeated till the remainder is zero. The divisor then results the GCD. [ICSE-2009]
Ans. import java.util.Scanner;
public class BUZZGCDnumber
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("1. Buzz Number");
System.out.println("2. GCD");
System.out.print("Enter your choice: ");
int choice = scan.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter a number: ");
int num = scan.nextInt();
int lastDigit = num % 10;
int remainder = num % 7;
if (lastDigit == 7 || remainder == 0)
System.out.println(num + " is a buzz number");
else
System.out.println(num + " is not a buzz number");
break;
case 2:
System.out.print("Enter two numbers: ");
int num1 = scan.nextInt();
int num2 = scan.nextInt();
int gcd = 1;
for(int i = 1; i <= num1 && i <= num2; ++i)
{
if(num1 % i==0 && num2 % i==0)
www.bhuvantechs.com

gcd = i;
}
System.out.printf("G.C.D of %d and %d is %d", num1, num2, gcd);
break;
}
}
}

28. Write a program to read the number x using the Scanner class and compute the series:

Iterative Constructs in Java ~ 14 ~


Sum = x/2 + x/5 + x/8 + x/11+ ... + x/20
The output should look like as shown below:
Enter a value of x: 10
Sum of the series is: 10.961611917494269
Ans. import java.util.Scanner;
public class Series
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the value of x : ");
double x = scanner.nextDouble();
double sumOfSeries = 0;
double numerator = x;
double denominator;
double term;
for(int i = 2; i <= 20; i = i + 3)
{
denominator = i;
term = numerator / denominator;
System.out.println(numerator+" /"+ denominator);
sumOfSeries = sumOfSeries + term;
}
System.out.println("Sum of the series : " + sumOfSeries);
}
}

29. Write a program in Java to compute and display factorial of numbers up to a number entered via
the Scanner class. The output should look like as shown below when 7 is input.
Enter a number: 7
1! ( =1) = 1
2! ( =1 × 2) = 2
3! ( =1 × 2 × 3) = 6
4! ( =1 × 2 × 3 × 4) = 24
5! ( =1 × 2 × 3 × 4 × 5) = 120
6! ( =1 × 2 × 3 × 4 × 5 × 6) = 720
7! ( =1 × 2 × 3 × 4 × 5 × 6 × 7) = 5040
Ans. import java.util.Scanner;
class Factorial
{
www.bhuvantechs.com

public static void main(String args[])


{
int n, c, i,x,fact = 1;
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
if (n < 0)

Iterative Constructs in Java ~ 15 ~


System.out.println("Number should be non-negative.");
else
{
for (c = 1; c <= i; c++)
fact = fact*c;
System.out.println("Factorial of "+i+"! = "+fact);
}
fact=1;
}
}
}

30. Write a program in Java to find the sum of the given series:
(i)Ans. import java.util.Scanner;
class series
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of x ");
int x=in.nextInt();
System.out.println("Enter the value of n terms ");
int n=in.nextInt();
double i;
double sum=0;
for(i = 1;i <=n; i ++)
{
sum=sum+(Math.pow(x,i));
}
System.out.println("The sum is "+sum);
}
}
(ii)Ans. import java.util.Scanner;
class series
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of n terms ");
www.bhuvantechs.com

int n=in.nextInt();
double i,x=3;
double sum=0;
for(i = 1;i <=n; i ++)
{
if(i%2==0)
sum=sum-(Math.pow(x,i));
else
sum=sum+(Math.pow(x,i));

Iterative Constructs in Java ~ 16 ~


}
System.out.println("The sum is "+sum);
}
}
(iii)Ans. import java.util.Scanner;
class series
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of x ");
int x=in.nextInt();
System.out.println("Enter the value of n terms ");
int n=in.nextInt();
double i,y=0;
double sum=0;
for(i = 1;i <=n; i ++)
{
y=(i/(Math.pow(x,i)));
sum=sum+y;
}
System.out.println("The sum is "+sum);
}
}
(iv)Ans. import java.util.Scanner;
class series
{
public static void main(String args[])
{
double i,y=0;
double sum=0;
for(i = 1;i <=3; i ++)
{
y=(i/(i+1));
sum=sum+y;
}
System.out.println("The sum is "+sum);
}
}
www.bhuvantechs.com

(v)Ans. import java.util.Scanner;


class series
{
public static void main(String args[])
{
double i,y=0,j;
double sum=0;
for(i = 1;i <=10; i ++)
{

Iterative Constructs in Java ~ 17 ~


y=(i/Math.sqrt(i));
sum=sum+y;
}
System.out.println("The sum is "+sum);
}
}
(vi)Ans.import java.util.Scanner;
class series_factorial
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of x ");
int x=in.nextInt();
System.out.println("Enter the value of n terms ");
int n=in.nextInt();
double i,y=0,j;
double sum=0;
for(i = 1,j=3; i <= n; i ++,j=j+2)
{
y=x+i;
sum=sum+(y/j);
}
System.out.println("The sum is "+sum);
}
}
(vii)Ans. import java.util.Scanner;
class series_factorial
{
public static int fact(int index)
{
int f = 1, i;
for(i = 1; i <= index; i ++)
{
f = f*i;
}
return f;
}
public static void main(String args[])
www.bhuvantechs.com

{
Scanner in = new Scanner(System.in);
int i , num, x;
double frac, sum=0;
System.out.println("Enter the numerator");
x = in.nextInt();
for(i = 1; i <= 20; i ++)
{
frac = x/fact(i);

Iterative Constructs in Java ~ 18 ~


sum = sum + frac;
}
System.out.println("The sum is "+sum);
}
}
(viii) Ans. import java.util.Scanner;
class series_factorial
{
public static int fact(int index)
{
int f = 1, i;
for(i = 1; i <= index; i ++)
{
f = f*i;
}
return f;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i , num, x;
double frac, sum=0;
System.out.println("Enter the numerator");
x = in.nextInt();
for(i = 1; i <= 10; i ++)
{
frac = Math.pow(x, i)/fact(i);
sum = sum + frac;
}
System.out.println("The sum is "+sum);
}
}
ix.Ans. import java.util.Scanner;
class series_factorial
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i;
www.bhuvantechs.com

double sum=0;
for(i = 0; i <= 20; i ++)
{
sum=sum+(Math.pow(2,i)-1);
}
System.out.println("The sum is "+sum);
}
}
x. Ans. import java.util.Scanner;

Iterative Constructs in Java ~ 19 ~


class series_factorial
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i;
double prod=1;
for(i = 0; i <= 20; i ++)
{
prod=prod*(Math.pow(2,i));
}
System.out.println("The product is "+prod);
}
}

31. Write a menu driven class to accept a number from the user and check whether it is Palindrome or
a Perfect number.
i Palindrome number: A number is a Palindrome which when read in reverse order is same as read in
the right order.
Example: 11, 101, 151, etc.
ii. Perfect number: A number is called Perfect if it is equal to the sum of its factors other than the
number itself.
Example: 6 = 1+2+3 [ICSE-2008]
Ans. import java.io.*;
import java.util.Scanner;
class PALINDROME_OR_PERFECT
{
public static void main(String args[])
{
int run,num,ch;
System.out.println("ENTER YOUR CHOICE :");
System.out.println("1 FOR PALINDROME ");
System.out.println("2 FOR PERFECT NUMBER ");
Scanner in = new Scanner(System.in);
ch= in.nextInt();
if(ch==1)
{
System.out.println("ENTER A NUMBER FOR FINDING WHETHER IT IS A PALINDROME");
num=in.nextInt();
www.bhuvantechs.com

int d;
int rev=0;
int num1=num;
do
{
d=num%10;
rev=(rev*10)+d;
num=num/10;
}while(num!=0);

Iterative Constructs in Java ~ 20 ~


if(num1==rev)
System.out.println("IT IS A PALINDROME");
else
System.out.println("IT IS NOT A PALINDROME");
}
else if(ch==2)
{
System.out.println("ENTER A NUMBER FOR FINDING WHETHER IT IS A PERFECT NUMBER");
num=in.nextInt();
int i=1;
int sum=0;
while(i<num)
{
if(num%i==0)
sum=sum+i;
i++;
}
if(sum==num)
System.out.println("IT IS A PERFECT NUMBER");
else
System.out.println("IT IS NOT A PERFECT NUMBER");
}
else
System.out.println("ENTERED WRONG DATA");
}
}

32. Write a program to print the sum of negative numbers, sum of positive even numbers and sum of
positive odd numbers from a list of n numbers entered by the user. The program terminates when the
user enters a zero. [ICSE-2005]
Ans. import java.util.*;
public class Sums
{
void Sums()
{
int nn = 0, ne = 0, no = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter nos (Press 0 to stop): ");
int n;
www.bhuvantechs.com

do {
n = sc.nextInt();
if (n < 0) {
nn = nn + n;
} else if (n > 0) {
if (n % 2 == 0) {
ne = ne + n;
} else {
no = no + n;

Iterative Constructs in Java ~ 21 ~


}
}
} while (n != 0);
System.out.println("Sum of negative nos: " + nn);
System.out.println("Sum of even nos: " + ne);
System.out.println("Sum of odd nos: " + no);
}
}

www.bhuvantechs.com

Iterative Constructs in Java ~ 22 ~

You might also like