prowessapps.in Java Loop Programs

 WAP to calculate the sum of numbers from 1 to 10.

class Test {
 public static void main(String[] a)
 {
  int sum =0;
  for(int i =0;i<=10;i++) {
   sum = sum+i;
  }
  System.out.print("Sum = "+sum);
 }
}
OUTPUT :
Sum = 55  

 WAP to find the factorial of a given number.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  int n,res=1;
  Scanner s=new Scanner(System.in);
  System.out.print("Enter a Num: ");
  n = s.nextInt();
  for(int i=n; i>=2;i--) {
   res = res*i;
  }
  System.out.print("Result = "+res);
 }
}
OUTPUT :
Enter a Num: 5
Result = 120 

 WAP to check the given number is prime or not.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter a num: ");
  int n = s.nextInt();
  int flag = 0;
  for(int i =2; i<=Math.sqrt(n);i++){
   if(n%i==0) {
    flag = 1;
    break;
   }
  }
  if(flag==0) {
   System.out.print(n+" is a prime no.");
  }
  else {
   System.out.print(n+" is not a prime no.");
  }
 }
} 
OUTPUT :
Enter a num: 45
45 is not a prime no. 

 WAP to swap two values without using third variable.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter a Num: ");
  int n = s.nextInt();
  int c=0;
  while(n>0) {
   n = n/10;
   c++;
  }
 System.out.print("Digits = "+c);
 }
}  
OUTPUT :
Enter a Num: 12345
Digits = 5

 WAP to check the given number is PALINDROME or NOT.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter a Num: ");
  int n = s.nextInt();
  int orig= n;
  int rev =0;
  while(n>0) {
   int t = n%10;
   rev = 10*rev+t;
   n = n/10;
  }
  if(orig==rev) {
   System.out.print("Palindrome");
  }
  else {
   System.out.print("NOT Palindrome");
  } 
 }
}
OUTPUT :
Enter a Num: 12321
Palindrome

 WAP to find x to the power y(xy ) using loop.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Base: ");
  int x = s.nextInt();
  System.out.print("Enter Power: ");
  int y = s.nextInt();
  int res = 1;
  for(int i = 1; i<=y; i++) {
   res = res*x;
  }
  System.out.print("Result = "+res);
 }
}
OUTPUT :
Enter Base: 5
Enter Power: 4
Result = 625 


 WAP to check a given number is armstrong or not.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  int x,rem,p=0,sum=0, digits =0; 
  Scanner s=new Scanner(System.in);
  System.out.print("Enter a Num: ");
  x = s.nextInt();
  int temp = x;
  while(temp>0) {
   temp /= 10;
   digits++;
  }
  temp = x;
  while(temp!=0) {
   rem = temp%10;
   p =(int) Math.pow(rem,digits);
   sum = sum+p;
   temp = temp/10;
  }
  if(sum==x) {
   System.out.print("Armstrong Num.");
  }
  else {
   System.out.print("Not Armstrong Num.");
  }
 }
}
OUTPUT :
Enter a Num: 153
Armstrong Num.

 WAP to check a given number is perfect or not.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter a Num: ");
  int x = s.nextInt();
  int sum = 0;
  for(int i = 1; i <= x/2; i++) {
   if(x%i ==0) {
    sum = sum+i;
   }
  }
  if(sum==x) {
   System.out.print("Perfect number");
  }
  else {
   System.out.print("NOT Perfect Number");
  }
 }
}
OUTPUT :
Enter a Num: 28
Perfect number 

 WAP to calculate hcf and lcm of two numbers.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Num1: ");
  int x = s.nextInt();
  System.out.print("Enter Num2: ");
  int y = s.nextInt();
  int gr =(x >= y)?x:y;
  int low =(x < y)?x:y;
  int temp, lcm;
  while(low!=0) {
   temp = gr % low;
   gr = low;
   low = temp;
  }
  System.out.println("HCF = "+gr);
  lcm = (x*y)/gr;
  System.out.print("LCM = "+lcm); 
 }
}
OUTPUT :
Enter Num1: 12
Enter Num2: 15
HCF = 3
LCM = 60

 WAP to convert decimal to binary number.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter a Num: ");
  int x = s.nextInt();
  int r;
  for(int i = 15; i>=0; i--) {
   r = x>>i;
   if((r & 1)==1)
    System.out.print("1");
   else
    System.out.print("0");
  }
 }
}
OUTPUT :
Enter a Num: 10
0000000000001010




CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved