Posts

Showing posts with the label While

Introduction To Array

 package arrays; import java.util.Scanner; public class ArrayIntro { public static void main(String[] args) { /* int[] marks; marks = new int[5]; */ //int marks[] = new int [5]; //ALL ARE SAME int[] age = {2,4,8,16,32}; double[] percentage = {1.0,3.14,4.6,78}; Scanner sc = new Scanner(System.in); while(6>age.length) { int n = sc.nextInt(); System.out.println(age[n-1]); }}}

Find Palindrome Number

 package whileLoops; import java.util.Scanner; public class PalindromeNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int temp = n; int reversedNumber = 0; while(temp>0) { int lastDigit = temp % 10; reversedNumber = reversedNumber * 10 + lastDigit; temp /= 10; } if (reversedNumber == n) { System.out.println(n+ "  is palindrome"); } else { System.out.println(n + " is not a palindrome"); } } }

Sum of Digits

 package whileLoops; import java.util.Scanner;  public class SumOfDigits { public static void main(String[] args) { Scanner sc = new  Scanner(System.in); int n = sc.nextInt(); int temp = n; int sum = 0; while(temp >0) { int lastDigit = temp%10; temp /= 10; sum += lastDigit; System.out.println(" Last Digit " + lastDigit + " temp " + temp + " Sum " + sum); } System.out.println("The sum of all digit of " + n + " is " + sum); } }