Professional Documents
Culture Documents
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}
if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}
if (num % i == 0) {
flag = true;
break;
}
if (!flag)
else
For a number
1. public class ReverseNumberExample1
2. {
3. public static void main(String[] args)
4. {
5. int number = 987654, reverse = 0;
6. while(number != 0)
7. {
8. int remainder = number % 10;
9. reverse = reverse * 10 + remainder;
10. number = number/10;
11. }
12. System.out.println("The reverse of the given number is: " + reverse);
13. }
14. }
For a string
1. import java.util.*;
2. class Swap_With {
3. public static void main(String[] args) {
4. int x, y, t;// x and y are to swap
5. Scanner sc = new Scanner(System.in);
6. System.out.println("Enter the value of X and Y");
7. x = sc.nextInt();
8. y = sc.nextInt();
9. System.out.println("before swapping numbers: "+x +" "+ y);
10. /*swapping */
11. t = x;
12. x = y;
13. y = t;
14. System.out.println("After swapping: "+x +" " + y);
15. System.out.println( );
16. }
17. }
Without
1. class demo {
2. public static void main(string arg[]) {
3. System.out.println("Before swapping");
4. int x = 10;
5. int y = 20;
6. System.out.println("value of x:" + x);
7. System.out.println("value of y:" + y);
8. system.out.println("After swapping");
9. x = x + y;
10. y = x - y;
11. x = x - y;
12. System.out.println("value of x:" + x);
13. System.out.println("value of y:" + y);
14. }
15. }
8)pattern programs
9)remove duplicate number in an array
import java.util.*;
class Main{
if(n==0 || n==1){
return n;
int j = 0;
if(array[i] != array[i+1]){
temp[j++] = array[i];
temp[j++] = array[n-1];
array[i] = temp[i];
return j;
int size=in.nextInt();
for(int i=0;i<array.length;i++){
array[i]=in.nextInt();}
System.out.print(array[i]+" ");
import java.util.*;
import java.util.Scanner;
class Main
string = string.toLowerCase();
//Iterate the string forward and backward, compare one character at a time
if(string.charAt(i) != string.charAt(string.length()-i-1)){
flag = false;
break;
}
if(flag)
else
1. import java.util.Scanner;
2. public class SumOfDigitsExample1
3. {
4. public static void main(String args[])
5. {
6. int number, digit, sum = 0;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number: ");
9. number = sc.nextInt();
10. while(number > 0)
11. {
12. //finds the last digit of the given number
13. digit = number % 10;
14. //adds last digit to the variable sum
15. sum = sum + digit;
16. //removes the last digit from the number
17. number = number / 10;
18. }
19. //prints the result
20. System.out.println("Sum of Digits: "+sum);
21. }
22. }
13)2nd smallest & 2nd largest
import java.util.Scanner;
class Main
{
int n, temp;
n = s.nextInt();
a[i] = s.nextInt();
temp = a[i];
a[i] = a[j];
a[j] = temp;
System.out.println("Second Largest:"+a[n-2]);
System.out.println("second Smallest:"+a[1]);