Write Java programs for the following: Write a Java program to accept a number f
ID: 3879522 • Letter: W
Question
Write Java programs for the following:
Write a Java program to accept a number from the user and display the following menu :
a. First 5 multiples
b. Squares of first 5 multiples
c. Cubes of first five multiples.
Based on the user's choice appropriate result should be displayed
Note: Use switch case construct
2. To take the input of a number and then check whether it is a prime number or a composite number, and display an appropriate message.
3. To take the input of a number and then find and display the sum of it’s digits.
4. To take the input of a number and then check whether it is a palindrome number or not, and display an appropriate message.
5. To take the input of a number n and then find and display it’s factorial ( n!). For example 4!=1x2x3x4
Explanation / Answer
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class program1 {
//1
void multiples()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number :");
int n = sc.nextInt();//reading input
System.out.println("a. First 5 multiples b. Squares of first 5 multiples c. Cubes of first five multiples. Enter your choice :");
char c=sc.next().charAt(0);//read choice
//using switch
switch(c)
{
case 'a':
System.out.println("The first five multiples are :");
for(int i=1;i<6;i++)
System.out.println(n*i);
break;
case 'b':
System.out.println("Square of first five multiples are :");
for(int i=1;i<6;i++)
System.out.println((n*i)*(n*i));
break;
case 'c':
System.out.println("Cubes of first five multiples are :");
for(int i=1;i<6;i++)
System.out.println((n*i)*(n*i)*(n*i));
break;
}
}
//2
void check_prime()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number :");
int n = sc.nextInt();//reading input
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
break;
}
if(i==n)
System.out.println(n+"is a Prime number");
else
System.out.println(n+"is a composite number");
}
//3
void sum_digits()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number :");
int n = sc.nextInt();//reading input
int s=0,k=n;//to store sum
while(k>0)
{
s=s+(k%10);
k=k/10;
}
System.out.println("Sum of digits in "+n+" is: "+s);
}
//4
void check_palindrome()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number :");
int n = sc.nextInt();//reading input
int k=n,r=0;
while(k>0)
{
r=r*10 + (k%10);//reversing number
k=k/10;
}
if(r==n)//matching numbers
System.out.println(n+" is a palindrome");
else
System.out.println(n+" is nor a palindrome");
}
//5
void factorial()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number :");
int n = sc.nextInt();//reading input
int f=1;
for(int i=1;i<=n;i++)
{
f=f*i;//calculating factorial
}
System.out.println("Factorial of "+n+" :"+f);
}
public static void main(String argv[])
{
program1 p = new program1();
p.multiples();
p.check_prime();
p.sum_digits();
p.check_palindrome();
p.factorial();
}
}
output:
run:
Enter number :
5
a. First 5 multiples
b. Squares of first 5 multiples
c. Cubes of first five multiples.
Enter your choice :
a
The first five multiples are :
5
10
15
20
25
Enter number :
5
5is a Prime number
Enter number :
123
Sum of digits in 123 is: 6
Enter number :
121
121 is a palindrome
Enter number :
4
Factorial of 4 :24
BUILD SUCCESSFUL (total time: 56 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.