0% found this document useful (0 votes)
119 views13 pages

Loop Programs

The document contains multiple Java programs that demonstrate various functionalities such as displaying natural numbers, calculating sums and averages, finding factorials, checking for perfect and Armstrong numbers, and generating multiplication tables. Each program is structured with a main method and utilizes the Scanner class for user input. The code snippets are intended for educational purposes to illustrate basic programming concepts in Java.

Uploaded by

Harleen Kaur
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)
119 views13 pages

Loop Programs

The document contains multiple Java programs that demonstrate various functionalities such as displaying natural numbers, calculating sums and averages, finding factorials, checking for perfect and Armstrong numbers, and generating multiplication tables. Each program is structured with a main method and utilizes the Scanner class for user input. The code snippets are intended for educational purposes to illustrate basic programming concepts in Java.

Uploaded by

Harleen Kaur
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

Write a program in Java to display n terms of natural number and their sum.

Ans:

import [Link].*;

public class NaturalSum

public static void main(String args[])

int n,sum=0;

Scanner sc=new Scanner([Link]);

n=[Link]();

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

sum=sum+i;

[Link](sum);

Write a program in Java to read 10 numbers from keyboard and find their sum and average.

Ans:

Import [Link].*;

public class NumberSA

public static void main(String args[])


{

int n,sum=0,avg=0;

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

Scanner sc=new Scanner([Link]);

n=[Link]();

sum=sum+n;

avg=sum/10;

Write a program in Java to display the cube of the number upto given an integer.

Ans:

import [Link].*;

public class Cube

public static void main(String[] args)

int a,n;

Scanner sc=new Scanner([Link]);

n=[Link]();

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

a=i*i*i;

[Link]("cube of "+i+" is:"+a);

Write a program in java to display the multiplication table of a given integer.

Ans:

import [Link].*;

public class Table1

public static void main(String args[])

Scanner sc=new Scanner([Link]);

int n=[Link]();

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

[Link](n+" * "+i+" = "+n*i);

}
}

Write a program in Java to display the n terms of odd natural number and their sum

Ans:

import [Link].*;

public class OddSum

public static void main(String args[])

int sum=0;

Scanner sc=new Scanner([Link]);

int n=[Link]();

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

if(i%2!=0)

[Link](i);

sum=sum+i;

[Link]("Sum = "+sum);

}
}

Write a java program to calculate the factorial of a given number.

Ans:

import [Link].*;

public class Factorial

public static void main(String[] args)

Scanner sc=new Scanner([Link]);

int n=[Link]();

int i,fact=1;

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

fact=fact*i;

[Link]("factorial of "+n+" is: "+fact+"!");

}
Write a program in Java to display the n terms of even natural number and their sum.

Ans:

import [Link].*;

public class EvenSum

public static void main(String args[])

int sum=0;

Scanner sc=new Scanner([Link]);

int n=[Link]();

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

if(i%2==0)

[Link](i);

sum=sum+i;

[Link]("Sum = "+sum);

Write a program in Java to display the n terms of square natural number and their sum.

Ans:

import [Link].*;
public class Square

public static void main(String args[])

int n,sum=0;

Scanner sc=new Scanner([Link]);

n=[Link]();

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

int sq=i*i;

[Link]("Square of "+i+" is : "+sq);

sum=sum+sq;

[Link]("Sum of square: "+sum);

}
Write a Java program to check whether a given number is a perfect number or not.

Ans:

import [Link].*;

public class PerfectNo

public static void main(String args[])

int sum=1,n;

Scanner sc=new Scanner([Link]);

n=[Link]();

if(n==1)

[Link]("Not a perfect number");

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

if(n%i==0)

if(i*i==n)

sum=sum+i;

else

sum=sum+i+(n/i);

if(sum==n)

[Link]("Perfect Number");
}

else

[Link]("Not a perfect number");

Write a Java program to find the perfect numbers within a given number of range.

Ans:

import [Link].*;

class PerfectRange

public static void main(String main[])

Scanner sc = new Scanner([Link]);

[Link]("Enter Start Number");

int start=[Link]();

[Link]("Enter End Number");

int end=[Link]();

[Link]("Perfect Numbers between " + start + " and " + end + " are :");

for (int i=start; i<=end; i++)

int sum=0;

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


{

if (i % j==0)

sum=sum+j;

if (i==sum)

[Link](i + "\t");

Write a Java program to check whether a given number is an Armstrong number or not.

Ans:

import [Link].*;

public class Armstrong

public static void main(String args[])

int temp,digit=0,num;

Scanner sc=new Scanner([Link]);

num=[Link]();

int sum=0,i=0;

temp=num;

while(temp>0)
{

temp=temp/10;

digit++;

temp=num;

while(temp>0)

i=temp%10;

sum+=(int)([Link](i,digit));

temp=temp/10;

if(sum==num)

[Link]("Armstrong number");

else

[Link]("Not an Armstrong number");

}
Write a Java program to find the Armstrong number for a given range of number.

Ans:

import [Link].*;

public class ArmstrongRange

public static void main(String args[])

int num1, num2;

Scanner sc = new Scanner([Link]);

[Link]("from: ");

num1 = [Link]();

[Link]("to: ");

num2 = [Link]();

[Link]("Armstrong numbers are :");

for (int i = num1; i<num2; i++)

int temp=i;

int digit=0;

while(temp!=0)

temp/=10;

digit++;

int sum=0;

temp=i;

while(temp!=0)

int j=temp%10;

sum+=(int)([Link](j,digit));

temp=temp/10;

}
if(sum == i)

[Link](i);

Write a Java program to determine whether a given number is prime or not.

Ans:

You might also like