Friday, January 20, 2017

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:

Function(Method) Calling In Java

Following Program will help you understand :

public class CallingMethods
 {
    public static void main(String[] nt)
    {
        printone();
        printtwo();
        printthree();
    }

public static void printone()   //static because main is static and static function can call only static members
{
System.out.println("Print one");
}
public static void printthree()
{
System.out.println("Print Three");
}
public static void printtwo()
{
System.out.println("Print Two");
}
}

Output:



Perimeter of Circle In Java

//Following will help you understand

import java.util.Scanner;
public class Perimeter
{
    public static void main(String [] nt)

    {  float x;
    Scanner input=new Scanner(System.in);
        System.out.println("Enter the radius of circle");
        x=input.nextFloat();
        float y;
                y=2*3.142f*x;      //because of float we have used f
                System.out.printf("Perimeter of Circle %f\n",y);
    }
}

Output:


Thursday, January 19, 2017

Interface in java !!

//This program helps you to understand the working of an interface



import java.util.Scanner; //package for scanning purpose
public class Purchase
{
int num,x;
Scanner obj=  new Scanner (System.in);
public void getinfo()
{
System.out.println("Enter the amount of packets purchased: ");
num=obj.nextInt();
System.out.println("Enter amount of defective packets: ");
x=obj.nextInt();

}

public static void main(String[] args)
{
result obj1=new result();
obj1.getinfo();
obj1.amtleft();
stock obj2=new result();
obj2.stk();
obj1.rslt();
}
}
class Preturn extends Purchase
{
int ret;
public void amtleft()
{
System.out.println("Amount left: ");
ret=num-x;
System.out.println(ret);

}
}
interface stock
{
public void stk();
Scanner ob = new Scanner (System.in);

}
class result extends Preturn implements stock
{
int y;

public void stk()
{
System.out.println("Enter previous stock if any: ");


}
public void rslt()
{
y= ob.nextInt();
System.out.println("Packets to be sold : "+(y+ret));
}



}

Friday, January 13, 2017

Print a String In Reverse Order

Program To Print a String In Reverse Order :

import java.util.Scanner;

public class Gndec
{

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        String name,rev="";
        System.out.println("Enter Your Name");
       
        name=input.nextLine();
        
        System.out.printf("Hello,%s\nIn reverse Order :",name);
        
        int j=0,n=name.length();
        
        for(int i=n-1;i>=0;i--)
        {
           rev=rev+name.charAt(i);
        }
       
        System.out.println(rev);
     
}
}

Output:


Power of 2

Program to Check Whether a Number is Some Power Of 2 (Two):

import java.util.Scanner;

public class Gndec
{

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int a,p=0,c=0;
        System.out.println("Enter any Number");
        a=input.nextInt();
        while(a!=1)
        {if(a%2!=0)
        {   p=1;
            System.out.println("No");
            break;
        }
        else{
            a=a/2;
        }
        c++;}
        if(p==0)
        {
            System.out.println("Yes "+c);
        }
}
}

Output: