Sunday, January 22, 2017

Right Angle Number Triangle

Following program accepts no of rows and print a triangle pattern as given below:

Test Data
Input number of rows :5
 Output :
1
12
123
1234
12345

import java.util.*;
public class Inches 
{
    
    public static void main(String[] nt)
    {
      
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Number of rows");
        int x=in.nextInt();

        System.out.println("Pattern is");
        for(int i=1;i<=x;i++)
    {   for(int j=1;j<=i;j++)
        {  
            System.out.print(j);
        }
        System.out.println();
    }
    }
    
}
Output:


Display weekday from day number using Switch Java

Following will help you understand:

/import java.util.*;
public class Weekday
{
   
    public static void main(String[] nt)
    {
     
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Day Number");
        int a=in.nextInt();
        dayname(a);
       
    }
    public static void dayname(int c)
    {  String weekday;
        switch(c)
                {
                    case 1:weekday="Monday"; break;
                    case 2:weekday="Tuesday"; break;
                    case 3:weekday="Wednesday"; break;
                    case 4:weekday="Thrusday"; break;
                    case 5:weekday="Friday"; break;
                    case 6:weekday="Saturday"; break;
                    case 7:weekday="Sunday"; break;
                    default:System.out.println("Invalid day number");return;
                   
                }
        System.out.println("Weekday is "+weekday);
    }
   
}

Output:


Saturday, January 21, 2017

Addition of Two Binary Numbers

Following program accepts two binary numbers and gives the sum of that two binary numbers

/import java.util.*;
public class Table
{
    public static void main(String[] nt)
    {   int[] sum=new int [20];
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Two Binary Numbers");
        long binary1=in.nextLong();
        long binary2=in.nextLong();
        int i=0,carry=0;
        while(binary1!=0||binary2!=0)
        {sum[i++]=(int)((binary1%10+binary2%10+carry)%2);
         carry=(int)((binary1%10+binary2%10+carry)/2);
         binary1=binary1/10;
         binary2=binary2/10;
        }
        if(carry!=0)
        {
            sum[i]=carry;
        }
        System.out.print("Sum is: ");
        while(i>=0)
        {
            System.out.print(sum[i--]);
        }
        System.out.println();
       
    }
   
}

Output:


Multiplication Table upto 10 of any Number

Following is the code to print multiplication table of any number upto 10

import java.util.*;
public class Table
{
    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Any Number");
        int a=in.nextInt();
        System.out.println("Multiplication Table");
        for(int i=1;i<=10;i++)
        {
            System.out.printf("%d * %d = %d\n",a,i,a*i);
        }
    }
   
}

Output:

Friday, January 20, 2017

Generate Random Numbers in Java

Random numbers are generated with the help of object of Random class.We call the functions in random class with this object which return random numbers

Following will help you understand:

import java.util.*;
public class Gndec
{
    public static void main(String[] args)
    {
       Random num=new Random();     //num object of Random class
       for(int i=1;i<6;i++)
       {
           System.out.println(num.nextInt(300));   //for integet values between 0 and 300
       }
    }
   
}


Output:


Sum of Array Elements in Java

Following program is used to find sum of array elements :

import java.util.*;
public class Gndec {

 
    public static void main(String[] nt)
    {   Scanner input=new Scanner(System.in);   //for user input
    System.out.println("Enter Number of Elements");
    int n=input.nextInt();    //no of elements
    int[] a=new int[n];       //allocating space for n elements
    System.out.println("Enter The Elements");
        for(int i=0;i<n;i++)
       {
         a[i]=input.nextInt();       //storing all values
       }
        int sum=0;
        for(int i=0;i<n;i++)
        {
            sum=sum+a[i];
        }
        System.out.println("Sum of Elements="+sum);
    }
}

Output:


Input Functions In Scanner Class Java

We can use the following Functions



Method Inputs
nextInt() Integer
nextFloat() Float
nextDouble() Double
nextLong() Long
nextShort() Short
next() Single word
nextLine() Line of Strings
nextBoolean() Boolean

Following will help you understand:

import java.util.Scanner;    //package for Scanner Class
public class Inputtest
{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);   //Object of Scanner Class
        System.out.println("Enter Any Integer");
        int x=input.nextInt();                                  //for integer
     
        System.out.println("Enter Any String");
        String name=input.next();                         //for String
     
        System.out.println("Enter Any Double Value");
        double d=input.nextDouble();                  //for Double
     
        System.out.println("Enter Any Character");
        char c = input.next().charAt(0);               //for Character
     
        System.out.println("Enter Any Float");
        float f=input.nextFloat();                         //for float
    }
 
}

Output: