write a program to input a number and print its multiplication table of 20 or Check if the number is prime or not
Answers
Programs using java :-
i.) Write a program to input a number and print its multiplication table of 20.
import java.util.Scanner; //importing package
class asc //class declaration
{
public static void main (String args[]) //main()method declaration
{
Scanner sc = new Scanner(System.in); //input object creation
System.out.println("Enter a number");
int n = sc.nextInt();
for(int i =1 ; i<=20 ; i++)
{
int a = n * i;
System.out.println(a);
}
}
}
Output :- in the attachment
Glossary :-
ii.) Check if the number is prime or not
import java.util.Scanner; //importing package
class prime //class declaration
{
public static void main(String args[]) //main()method declaration
{
System.out.println("Enter a no.");
Scanner sc=new Scanner(System.in); //input object creation
int c=0, i, a; //variable declaration
a = sc.nextInt();
for (i=1;i<=a;i++)
{
if(a%i==0)
c++;
}
if(c==2)
{
System.out.println("Prime");
}
else
{
System.out.println("Not prime");
}
}
}
Output :- in the attachment
Glossary :-