0% found this document useful (0 votes)
21 views5 pages

Solution - Chapter 8 - Iterative Construct

The document provides various Java programming exercises focusing on iterative constructs such as while, for, and do-while loops. It includes code examples for printing numbers in reverse, calculating averages, checking divisibility, and counting frequencies of positive, negative, and zero values. Additionally, it covers factorial calculation and frequency distribution of student marks.

Uploaded by

Jahnavi Singh
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)
21 views5 pages

Solution - Chapter 8 - Iterative Construct

The document provides various Java programming exercises focusing on iterative constructs such as while, for, and do-while loops. It includes code examples for printing numbers in reverse, calculating averages, checking divisibility, and counting frequencies of positive, negative, and zero values. Additionally, it covers factorial calculation and frequency distribution of student marks.

Uploaded by

Jahnavi Singh
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/ 5

CHP 8-ITERATIVE CONSTRUCTS IN JAVA

II] Assignment-

1.while to for loop


for(int i=0;i<=20;i++)
System.out.print(i + " ");

2. x=5 and y=50


x<=y y=y/x
1. 5<=50 y=50/5=10
2. 5<=10 y=10/5=2
5<=2 not possible
Ans: The loop iterates twice.

3.for to do-while loop


int x=10,c=20;
do
{
c=c-2;
} while(c>=10);

4. p=200
if(p<100) the loop breaks p=p-20
1. 200<100 p=180
2. 180<100 p=160
3. 160<100 p=140
4. 140<100 p=120
5. 120<100 p=100
6. 100<100 p=80
7. 80< 100 the loop breaks
Ans: The loop will execute 7 times.
OUTPUT: 80

5.for to while loop


int f=1.i=1
while (i<=5)
{
f=f*i;
System.out.println(f);
i++;
}

IV] Write the following program codes-

1.(i)//to print from 10-1 using for loop


public class loop
{
public static void main()
{
System.out.println("Numbers from 10-1: ");
for(int i=10;i>=1;i--)
System.out.println(i);
}
}

(ii)// to print from 10-1 using while loop


public class loop
{
public static void main()
{
System.out.println("Numbers from 10-1: ");
int a=10;
while(a>=1)
{
System.out.println(a);
a--;
}
}
}

(iii)//to print from 10-1 using do while loop


public class loop
{
public static void main()
{
System.out.println("Numbers from 10-1: ");
int b=10;
do
{
System.out.println(b);
b--;
}
while(b>=1);
}
}

2.//to print even numbers from 2-100 using do while


public class loop
{
public static void main()
{
System.out.println("Even numbers from 2-100:");
int a=2;
do
{
if(a%2==0)
System.out.println(a);
a++;
}
while(a<=100);
}
}

3.//to print odd numbers from 1-100 using while


public class loop
{
public static void main()
{
System.out.println("Odd numbers from 1-100:");
int a=1;
while(a<=100)
{
if(a%2!=0)
System.out.println(a);
a++;
}
}
}

4.//to input marks of 30 students and find total and average


import java.util.Scanner;
public class loop
{
public static void main()
{
Scanner obj=new Scanner(System.in);
int i;
float sum=0,avg;
for(i=1;i<=30;i++)
{
System.out.print("Enter Marks:");
float mk=obj.nextFloat();
sum=sum+mk;
}
avg=sum/30;
System.out.println("Total: " + sum);
System.out.println("Average: " + avg);
}
}

5.//to print multiples of 3


import java.util.Scanner;
public class loop
{
public static void main()
{
Scanner obj=new Scanner(System.in);
System.out.println("How many multiples of 3 do you want: ");
int opt=obj.nextInt();
System.out.println("Multiples of 3: ");
for(int i=1;i<=opt;i++)
{
System.out.print(i + " , ");
}
}
}

6.//to enter 50 numbers and check divisibility by 3;


import java.util.Scanner;
public class loop
{
public static void main()
{
Scanner obj=new Scanner(System.in);
int i=1,div=0;
while(i<=50)
{
System.out.print("Enter a number: ");
int a=obj.nextInt();
if(a%3==0)
div++;
i++;
}
System.out.println("Numbers divisible by 3: " + div);
}
}

7.//to enter a numbers and print its table


import java.util.Scanner;
public class loop
{
public static void main()
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a number: ");
int a=obj.nextInt();
for(int i=1;i<=12;i++)
{
System.out.println(a + "*" + i + "= " + a*i);
}
}
}

8.//to enter 25 numbers and find frequency of +,- and 0


import java.util.Scanner;
public class loop
{
public static void main()
{
Scanner obj=new Scanner(System.in);
int i=1;
int sump=0,sumn=0,sumz=0;
while(i<=25)
{
System.out.print("Enter number: ");
int a=obj.nextInt();
if(a>0)
sump++;
else if(a==0)
sumz++;
else
sumn++;
i++;
}
System.out.println("Number of positive number: " + sump);
System.out.println("Number of negative numbers: " + sumn);
System.out.println("Number of zero: " + sumz);
}
}

9.//program to print factorial of 5


public class loop
{
public static void main()
{
int i,fac=1;
for(i=1;i<=5;i++)
{
fac=fac*i;
}
System.out.println("Factorial of 5: " + fac);
}
}

10.//to display frequency of marks of 30 students


import java.util.Scanner;
public class loop
{
public static void main()
{
Scanner obj=new Scanner(System.in);
int mkp=0,mks=0,mkg=0,mke=0;
for(int i=1;i<=30;i++)
{
System.out.print("Enter Marks: ");
int mk=obj.nextInt();
if(mk>80)
mke++;
else if(80>=mk && mk>60)
mkg++;
else if(60>=mk && mk>40)
mks++;
else
mkp++;
}
System.out.println("Frequency of marks- ");
System.out.println("Poor: " + mkp);
System.out.println("Satisfactory: " + mks);
System.out.println("Good: " + mkg);
System.out.println("Excellent: " + mke);
}
}

You might also like