0% found this document useful (0 votes)
94 views7 pages

Public Class Public Static Void Int: Armstrong

The document provides code snippets in Java for various programming tasks including: 1) Checking if a number is an Armstrong number 2) Checking if a number is a prime number 3) Reversing a number or string 4) Swapping two numbers with and without a third variable 5) Pattern programs and other common programming problems.

Uploaded by

Rajeshkannan A.R
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)
94 views7 pages

Public Class Public Static Void Int: Armstrong

The document provides code snippets in Java for various programming tasks including: 1) Checking if a number is an Armstrong number 2) Checking if a number is a prime number 3) Reversing a number or string 4) Swapping two numbers with and without a third variable 5) Pattern programs and other common programming problems.

Uploaded by

Rajeshkannan A.R
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
You are on page 1/ 7

4)check Armstrong number

public class Armstrong {

public static void main(String[] args) {

int number = 371, originalNumber, remainder, result = 0;

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.");
}
}

5)check prime number

public class Main {

public static void main(String[] args) {

int num = 29;

boolean flag = false;

for (int i = 2; i <= num / 2; ++i) {

// condition for nonprime number

if (num % i == 0) {

flag = true;

break;
}

if (!flag)

System.out.println(num + " is a prime number.");

else

System.out.println(num + " is not a prime number.");

6)to reverse a number or a string

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

public class StringReverseExample{


public static void main(String[] args) {
String string = "abcdef";
String reverse = new
StringBuffer(string).reverse().toString();
System.out.println("\nString before reverse: "+string);
System.out.println("String after reverse: "+reverse);
}
}
7)swap 2 numbers using 3rd variable and without 3rd variable

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{

public static int removeDuplicates(int array[], int n){

if(n==0 || n==1){

return n;

int[] temp = new int[n];

int j = 0;

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

if(array[i] != array[i+1]){

temp[j++] = array[i];

temp[j++] = array[n-1];

//Changing the original array

for(int i=0; i<j; i++){

array[i] = temp[i];

return j;

public static void main (String[] args) {

Scanner in=new Scanner(System.in);

int size=in.nextInt();

int array[]=new int[size];

for(int i=0;i<array.length;i++){

array[i]=in.nextInt();}

int length = array.length;

length = removeDuplicates(array, length);


//Printing The array elements

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

System.out.print(array[i]+" ");

10)sum of all natural numbers from 1 to n

11)check whether a string is palindrome or not

import java.util.*;

import java.util.Scanner;

class Main

public static void main(String[] args) {

Scanner in=new Scanner(System.in);

String string = in.nextLine();

boolean flag = true;

//Converts the given string into lowercase

string = string.toLowerCase();

//Iterate the string forward and backward, compare one character at a time

//till middle of the string is reached

for(int i = 0; i < string.length()/2; i++){

if(string.charAt(i) != string.charAt(string.length()-i-1)){

flag = false;

break;

}
if(flag)

System.out.println("Given string is palindrome");

else

System.out.println("Given string is not a palindrome");

12)find the sum of digits

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
{

public static void main(String[] args)

int n, temp;

Scanner s = new Scanner(System.in);

System.out.print("Enter no. of elements you want in array(Minimum 2):");

n = s.nextInt();

int a[] = new int[n];

System.out.println("Enter all the elements:");

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

a[i] = s.nextInt();

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

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

if (a[i] > a[j])

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]);

You might also like