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 :-

\begin{array}{| c | c | c |}\cline{1-3} \bf variable & \bf Type & \bf Description\\ \cline{1-3} n &\sf int & to\:\:store\:\:a\:\:number \\ \cline{1-3} i &\sf int & to\:\:run\:\:the\:\:l{o}{o}p\:\:20\:\:times\\ \cline{1-3} a &\sf int & to\:\:store\:\:the\:\:multiplication\:\:table \\ \cline{1-3}\end{array}

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 :-

\begin{array}{| c | c | c |}\cline{1-3} \bf variable & \bf Type & \bf Description\\ \cline{1-3} a &\sf int & to\:\:store\:\:a\:\:number \\ \cline{1-3} i &\sf int & to\:\:run\:\:the\:\:l{o}{o}p\\ \cline{1-3} c &\sf int & to\:\:store\:\:the\:\:number\:\:of\:\:counts\\ \cline{1-3}\end{array}



Discussion

No Comment Found