Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

1. Write a java program for palindrome number/Reverse Number.

Source code:
public class PalindromeExample{
public static void main(String args[]){
int r,sum=0,temp;
int n=454;//It is the number variable to be checked for palindrome

temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
2. Write a java program to check the given number is prime/not.
Source code:

public class PrimeNumber


{
public static void main(String args[])
{
int num=7,count=0,i;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
count++;
}

}
if(count==3)
{
System.out.println("prime");
}
else
{
System.out.println("not a prime");
}
}
}
3. Program to check whether the given number is Armstrong number.

Source code:

public class ArmstrongNumber


{

public static void main(String[] args)


{

int num=370, number, temp, total=0;

number=num;
while(number!= 0)
{
temp = number % 10;
total = total + temp*temp*temp;
number /= 10;
}

if(total == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}

Read user input text using Scanner

import java.util.Scanner;
public class JavaExample {
public static void main(String[] args) {

// creating a scanner
Scanner scan = new Scanner(System.in);

System.out.print("Enter your first name: ");

// read user input and store it in a string variable


String firstName = scan.nextLine();

System.out.print("Enter your last name: ");

// read second input


String lastName = scan.nextLine();

// prints the user entered data


System.out.println("Your Name is: "+firstName+" "+lastName);

// close scanner
scan.close();
}
}

You might also like