For and While programs
- Categories Java, Iterative Statements, For & While
For Loop :
1 . Write a program to display FACTORIAL of a number
import java.util.*;
class FactorialOfNum{
public static void main(String args[ ])
{
Scanner sc =new Scanner(System.in);
int n,i,f=1;
System.out.println("enter a number");
n= sc.nextInt( );
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("THE FACTORIAL OF THE NUMBER IS : "+f);
}
}
2 . Write a program to display first 15 Fibonacci numbers
public class febonacciNum
{
public static void main(String args[])
{
int a=0,b=1,c,i;
System.out.println("First 15 Fibonacci numbers are :");
System.out.print(a+" "+b+" ");
for(i=3;i<=15;i++)
{
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
}
}
3 . Write a program to print all the factors of a number.
import java.util.Scanner; public class FactorsOfNumber { public static void main(String args[]) { int n ; Scanner sc = new Scanner(System.in); System.out.print("Enter the value of n: "); n = sc.nextInt(); System.out.println ("The all Factors of given no. "+n+" is"); for(int i = 1;i<= n;i++) { if(n%i == 0) System.out.println (i); } } }
4 . Write a program to display the Mathematical Table from the given number:
import java.util.Scanner; public class MathTables { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter number "); int num = sc.nextInt(); System.out.println("Table of " + num); for (int i = 1; i<= 10; i++) { System.out.println(num*i); } } }
5 . Write a program to calculate and display the factorials of all the numbers between ‘m’ and ‘n’ (where m<n, m>0, n>0).
import java.util.Scanner; public class FactorialOfMToN { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print("Enter m: "); int m = in.nextInt(); System.out.print("Enter n: "); int n = in.nextInt(); if (m < n && m > 0 && n > 0) { for (int i = m; i <= n; i++) { long fact = 1; for (int j = 1; j <= i; j++) fact *= j; System.out.println("Factorial of " + i + " = " + fact); } } else { System.out.println("Invalid Input"); } } }
6 . Write a program to display HCF and LCM of two numbers
import java.util.*; public class LCMandHCF { public static void main(String args[]) { Scanner sc =new Scanner(System.in); int m,n,i,min,hcf=0; System.out.println("enter two numbers"); m= sc.nextInt( ); n= sc.nextInt( ); min=Math.min(m,n); for(i=1;i<min;i++) { if(m%i==0 && n%i==0) hcf=i; } System.out.println("HCF of "+m+ " and "+n+" is : "+hcf); System.out.println("LCM of "+m+ " and "+n+" is : "+ ((m*n)/hcf)); } }
7 . Write a program to generate all 3 digit palindrome numbers.
public class ThreeDigitPrime
{
static void main()
{
int i,j,r,d;
for(i=100;i<=999;i++)
{
r=0;
for(j=i;j>0;j=j/10)
{
d=j%10;
r=r*10+d;
}
if(r==i)
System.out.println(i);
}
}
}
8 . Write a program to check and display whether two numbers are amicable pair or not
Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number.
The first ten amicable pairs are: (220, 284), (1184, 1210), (2620, 2924), (5020, 5564), (6232, 6368), (10744, 10856), (12285, 14595), (17296, 18416), (63020, 76084), and (66928, 66992).
import java.util.*; public class amicablePair { public static void main(String args[]) { Scanner sc =new Scanner(System.in); int m,n,sfm=0,sfn=0,i; System.out.println("enter two number"); m=sc.nextInt( ); n=sc.nextInt( ); for(i=1;i<=m;i++) { if(m%i==0) sfm+=i; } for(i=1;i<=n;i++) { if(n%i==0) sfn+=i; } if(sfm==sfn) System.out.println("Amicable Pair"); else System.out.println("Not Amicable Pair"); } }
While loop :
1 . Write a program to accept a number then print the sum of digits and number of digits present in it. (E.g. If the input number is 225, the sum of digits is 9 and number of digits is 3)
public class sumOfDigits
{
public static void main(int n)
{
int sum = 0,count = 0,div;
while(n>0)
{
div = n%10;
sum = sum+div;
count = count+1;
n = n/10;
}
System.out.println ("Sum of digits : "+sum);
System.out.println ("no. of digits : "+count);
}
}
2 . Write a program to finding GCD of two numbers using while loop
import java.util.Scanner; public class GCD_2_num { public static void main(String[] args) { int num1 , num2 ; System.out.print("Enter any two number:"); Scanner sc = new Scanner(System.in); num1 = sc.nextInt(); num2 = sc.nextInt(); while (num1 != num2) { if(num1 > num2) num1 = num1 - num2; else num2 = num2 - num1; } System.out.printf("GCD of given numbers is: "+num2); } }
3 . Write a program to accept a number then print the number in reverse order. (E.g. If the input number is 368, the output will be 863)
public class reverseNum
{
public static void main(int num)
{
int reverseNumber = 0, digit;
while(num>0)
{
digit = num%10;
reverseNumber = reverseNumber*10+digit;
num = num/10;
}
System.out.println ("Reverse Number is : " +reverseNumber);
}
}
4 . Write a program to find ASCII value of a Character
import java.util.*; public class DecimalToBinary { public static void main(String[] args) { Scanner in = new Scanner(System.in); // Create Stack object Stack stack = new Stack(); System.out.println("Enter decimal number: "); int num = in.nextInt(); while (num != 0) { int d = num % 2; stack.push(d); num /= 2; } System.out.print("\nBinary representation is:"); while (!(stack.isEmpty() )) { System.out.print(stack.pop()); } System.out.println(); } }
5 . Write a java program to calculate SquareRoot of given number.
import java.util.Scanner; public class SquareRoot { public static double squareRoot(int number) { double temp; double sr = number / 2; do { temp = sr; sr = (temp + (number / temp)) / 2; } while ((temp - sr) != 0); return sr; } public static void main(String[] args) { System.out.print("Enter any number:"); Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); scanner.close(); System.out.println("Square root of "+ num+ " is: "+squareRoot(num)); } }