InterviewSolution
| 1. |
write a program to input a number and print its multiplication table of 20 or Check if the number is prime or not |
|
Answer» em>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 :- |
|