Tuesday, 29 December 2015

Logical questions and answers in java

1> Print max. number among multiple numbers?

public class MyMaxNum
{
    public static void main(String[] args) 
       {
           
int[] m={12,23,85,1004,65,19,46,700,12,300};
int largst=0;
for (int i = 0; i < m.length; i++)
           {
largst=m[i]; 
}
for (int i = 0; i < m.length; i++)
          {
if(m[i]>largst)
{
largst=m[i];
}
}
System.out.println("Maximum Number:"+largst);


       }
}

OutPut:  Maximum Number: 1004




2> Print min. number among multiple numbers?

public class MyMinNum
{
    public static void main(String[] args) 
       {
           
int[] m={12,23,85,1004,65,19,46,700,12,300};
int min=0;
for (int i = 0; i < m.length; i++)
        {
min=m[i]; 
}
for (int i = 0; i < m.length; i++)
       {
if(m[i]>min)
{
min=m[i];
}
}
System.out.println("Minimum number:"+min);
    }
}

OutPut:  Minimum number: 12




3> Print multiple of 3?

public class MyMultiple
{
    public static void main(String[] args) 
       {
             for (int i = 1; i < 20; i++)
{
if(i%3==0)
{
System.out.print(i+",");
}
}
        }
}

OutPut:   3,6,9,12,15,18,


4> Print Fabbonocci series?

public void MyFabonocci
{
          public static void main(String[] args)
         {
                 // fabonocci series  0,1,1,2,3,5,8,13,21
                 int a=0; int b=1; int c=0;

                 System.Out.Print(a+","+b);
                 for(int i=0;i<7;i++)
                {
                        c=a+b;
                       System.Out.Print(","+c);   
                       a=b;
                       b=c;
                }

         }

}

OutPut: 0,1,1,2,3,5,8,13,21


5> find out  Factorial of 5?

package Test;
public class TestCalss
{
public static void main(String[] args)
     {
int res=1;
// here give i value as factorial number for which you required.
for (int i = 5; i>0; i--)
{
res=res*i;
}
System.out.println(res);
}
}

OutPut: 120

6> How to print power of a number?

      ///  Example for 10^3
      // Here base is 10 and exponential is 3
      package test;
      public class TestCalss
     {
public static void main(String[] args)
             {
int base=10;
int exp=3;
int res=1;

while(exp!=0)
{
 res=res*base;
 exp--;
}

System.out.println(res);
            }

       }

OutPut: 1000
  

7> Print sum of Natural number upto 20?

     package Test;
     public class TestCalss
    {
          public static void main(String[] args)
         {
             int res=0;
             for (int i =0; i <=20; i++)
             {
                 res=res+i;
             }
             System.out.println(res);
        }
   }

OutPut:  210


8> print ODD number upto 20

    public class TestCalss
   {
        public static void main(String[] args)
         {
              //odd number
              //1,3,5,7,9,11,13,15,17,19
             for (int i = 1; i <20; i++)
             {
if(i%2==1)
{
System.out.println(i+",");
}
              }
         }
}

OutPut:  1,3,5,7,9,11,13,15,17,19,
9> print EVEN number upto 20

    public class TestCalss
   {
        public static void main(String[] args)
         {
              //Even number

             for (int i = 1; i <20; i++)
             {
if(i%2==0)
{
System.out.println(i+",");
}
              }
         }
}

OutPut:  2,4,6,8,10,12,14,16,18,20,


10> Print prime numbers up to 100?


      for (int i = 1; i <100; i++)
{
boolean m=true;
for (int j = 2; j <i; j++)
{
if(i%j==0)
{
m=false;
}
}
if(m)
{
System.out.print(i+" ");
}

}

Output: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 





No comments:

Post a Comment