0% found this document useful (0 votes)
50 views26 pages

Computer Programs-Nested For Loops

The document contains various Java programs demonstrating the use of nested for loops for different tasks, such as calculating factorials, identifying prime numbers, and generating patterns. Each program is structured within a 'MyClass' public class and includes user input through the Scanner class. The examples illustrate a range of applications from mathematical computations to simple output formatting.

Uploaded by

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

Computer Programs-Nested For Loops

The document contains various Java programs demonstrating the use of nested for loops for different tasks, such as calculating factorials, identifying prime numbers, and generating patterns. Each program is structured within a 'MyClass' public class and includes user input through the Scanner class. The examples illustrate a range of applications from mathematical computations to simple output formatting.

Uploaded by

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

COMPUTER PROGRAMS -

CHAPTER 9 - NESTED FOR


LOOPS.

1) public class MyClass {

public static void main(String args[]) {

for(int i =5;i<=10;i++)

for(int j=1;j<=10;j++)

System.out.println(i+"*"+j+"="+i*j) ;

System.out.println() ;

2) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter any twenty nos.");


for(int i =1;i<=20;i++)

int a=sc.nextInt();

int c=0;

for(int j=1;j<=a;j++)

if(a%j==0)

c++;

if(c>2)

System.out.print(a+" ") ;

System.out.print("are the prime nos. ") ;

3) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the no of terms.");

int n=sc.nextInt() ;
int p=1,s=1;

double sum=0.0;

for(int i =2;i<=n;i++)

p*=i;

s+=i;

sum+=(double)s/p;

System.out.println(sum) ;

4A) public class MyClass {

public static void main(String args[]) {

for(int i=1;i<=5;i++)

for(int j=1;j<=i;j++)

if(j%2==0)

System.out.print("# ");

else

System.out.print("* ");
}

System.out.println() ;

B) public class MyClass {

public static void main(String args[]) {

for(int i=1;i<=5;i++)

for(int j=5;j>=i;j--)

System.out.print(j+" ");

System.out.println() ;

5) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);


System.out.println("Enter the value of m and n where m is
greater than n and both the values are greater than 0.");

int m=sc.nextInt() ;

int n=sc.nextInt() ;

if(m>0&&n>0&&n>m)

for(int i=m;i<=n;i++)

int sum=1;

for(int j=1;j<=i;j++)

sum*=j ;

System.out.println("Factorial of "+i+" is "+sum) ;

else

System.out.println("Wrong inputs, try again. ");

6) import java.util.Scanner;

public class MyClass {


public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter your choice, 1 for prime and 2 for


non-prime. ");

int ch=sc.nextInt() ;

switch(ch)

case 1:

System.out.print("Prime numbers from 1-100 are ");

for(int i =1;i<=100;i++)

int c=0;

for(int j=1;j<=i;j++)

if(i%j==0)

c++;

if(c==2)

System.out.print(i+" ") ;

break;

case 2:
System.out.print("Composite numbers from 1-100 are ");

for(int i =1;i<=100;i++)

int c=0;

for(int j=1;j<=i;j++)

if(i%j==0)

c++;

if(c!=2)

System.out.print(i+" ") ;

break;

default:

System.out.println("Wrong choice, try again. ");

7) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {


Scanner sc=new Scanner(System.in);

System.out.println("Enter the number of students. ");

int n=sc.nextInt() ;

double t=0;

for(int i=1;i<=n;i++)

System.out.println("The details of student "+i+" : ") ;

System.out.print("Name: ");

sc.nextLine() ;

String name=sc.nextLine() ;

System.out.print("Science marks: ");

int sci=sc.nextInt() ;

System.out.print("Maths marks: ");

int ma=sc.nextInt() ;

System.out.print("English marks: ");

int eng=sc.nextInt() ;

double avg=(double) (sci+ma+eng)/3;

System.out.println("Average is "+avg);

t+=avg;

double avrg=t/n;
System.out.println("Average of all students : "+avrg) ;

8) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number.");

int a=sc.nextInt() ;

int c=0,x=0,r=0;

for(int i=1;i<=a;i++)

if(a%i==0)

c++;

for(int i=a;i>0;i/=10)

int d=i%10;

r=r*10+d;

for(int i=1;i<=r;i++)
{

if(r%i==0)

x++;

if(c==2&&x==2)

System.out.println("TWISTED PRIME NUMBER. ");

else

System.out.println("NOT TWISTED PRIME NUMBER. ");

9) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Input ten numbers.");

for(int i=1;i<=10;i++)

int a=sc.nextInt() ;

int p=1;

for(int j=1;j<=a;j++)

{
p*=j;

System.out.println("Factorial of "+a+"= "+p) ;

10) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number.");

int a=sc.nextInt() ;

for(int i=a;i>0;i/=10)

int d=i%10;

int p=1;

for(int j=1;j<=d;j++)

p*=j;

System.out.println("Factorial of "+d+" "+p);


}

11) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter a three digit no.") ;

int n=sc.nextInt() ;

int c=0;

for(int i=n;i>0;i/=10)

c++;

if(c==3)

int y=1;

for(int j=n;j>0;j/=10)

int d=j%10;

System.out.println((int)(Math.pow(d, y)));

y++;

}
}

else

System.out.println("Wrong input, try again. ");

12) public class MyClass {

public static void main(String args[]) {

System.out.print("Composite numbers from 1-100 are ");

for(int i =1;i<=100;i++)

int c=0;

for(int j=1;j<=i;j++)

if(i%j==0)

c++;

if(c>2)

System.out.print(i+" ") ;

}
13) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in) ;

for(int i =1;i<=4;i++)

int c=0;

System.out.println("Enter the percentage of all 40 students.


");

for(int j=1;j<=40;j++)

int marks=sc.nextInt() ;

if(marks>=95)

c++;

System.out.println(c+" students secured more than 95 %. ") ;

14a) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {


Scanner sc=new Scanner(System.in);

System.out.println("Enter the no of terms.");

int n=sc.nextInt() ;

int p=1;

double sum=0.0;

for(int i =1;i<=n;i++)

p*=i;

sum+=(double)1/p;

System.out.println(sum) ;

b) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the no of terms.");

int n=sc.nextInt() ;

int p=0;

int sum=0;
for(int i =1;i<=n;i++)

p+=i;

sum+=p;

System.out.println(sum) ;

c) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the no of terms.");

int n=sc.nextInt() ;

int p=1;

int sum=0;

for(int i =1;i<=n;i++)

p*=i;

sum+=p;

}
System.out.println(sum) ;

d) import java.util.Scanner;

public class MyClass {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the no of terms.");

int n=sc.nextInt() ;

int p=0;

double sum=0;

for(int i =1;i<=n;i++)

p+=i;

sum+=(double)1/p;

System.out.println(sum) ;

e) public class MyClass {

public static void main(String args[]) {


double sum=0;

for(int i =2;i<=29;i++)

int c=0;

for( int y=1;y<=i;y++)

if(i%y==0)

c++;

if(c==2)

sum+=(double)1/i;

System.out.println(sum) ;

15a) public class MyClass {

public static void main(String args[]) {

for(int i =1;i<=5;i++)

for(int j=i;j>=1;j--)

{
System.out.print(j+" ");

System.out.println() ;

b) public class MyClass {

public static void main(String args[]) {

for(int i =5;i>=1;i--)

for(int j=1;j<=i;j++)

System.out.print(j+" ");

System.out.println() ;

c) public class MyClass {

public static void main(String args[]) {

for(int i =1;i<=5;i++)
{

for(int j=5;j>=i;j--)

System.out.print(j+" ");

System.out.println() ;

d) public class MyClass {

public static void main(String args[]) {

for(int i =9;i>=1;i-=2)

for(int j=1;j<=i;j+=2)

System.out.print(j+" ");

System.out.println() ;

}
e) public class MyClass {

public static void main(String args[]) {

for(int i =5;i>=1;i--)

for(int j=5;j>=i;j--)

System.out.print(j+" ");

System.out.println() ;

f) public class MyClass {

public static void main(String args[]) {

for(int i =1;i<=5;i++)

for(int j=i;j<=5;j++)

System.out.print(j+" ");

System.out.println() ;
}

g) public class MyClass {

public static void main(String args[]) {

for(int i =9;i>=1;i-=2)

for(int j=1;j<=5;j++)

System.out.print(i+" ");

System.out.println() ;

H) public class MyClass {

public static void main(String args[]) {

for(int i =9;i>=1;i-=2)

for(int j=i;j<=9;j+=2)

{
System.out.print(j+" ");

System.out.println() ;

i) public class MyClass {

public static void main(String args[]) {

for(int i =9;i>=1;i-=2)

for(int j=9;j>=i;j-=2)

System.out.print(j+" ");

System.out.println() ;

j) public class MyClass {

public static void main(String args[]) {

int x=1;
for(int i=1;i<=5;i++)

for(int j=x,y=1;y<=i;j++,y++)

System.out.print(j+" ");

System.out.println() ;

x+=i;

LAB ACTIVITY:
1) public class MyClass {

public static void main(String args[]) {

for(int i =1;i<=5;i++)

for(int j=1;j<=i;j++)

System.out.print("* ");

System.out.println() ;
}

for(int i=4;i>=1;i--)

for(int j=1;j<=i;j++)

System.out.print("* ");

System.out.println() ;

2) public class MyClass {

public static void main(String args[]) {

for(int i =5;i>=1;i--)

for(int j=1;j<=i;j++)

System.out.print(j+" ");

System.out.println() ;

}
for(int i=2;i<=5;i++)

for(int j=1;j<=i;j++)

System.out.print(j+" ");

System.out.println() ;

You might also like