Tuesday, June 30, 2015

Fibonacci Program In Java Using For Loop And Recurson

First two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

import java.util.Scanner;

public class Fabo
{

public static void main(String[] args)
{

Scanner scn=new Scanner(System.in);
System.out.println("type limit to find fibonacci no.");
int n=scn.nextInt();
int n1=0;
int n2=1;
int n3=0;
for(int i=1;i<=n;i++)
{
n3=n1+n2;
n1=n2;
n2=n3;
System.out.print(n3 +"  ");

}


}

}

Recursion : 


public class FabonacciRec {

public static void main(String[] args) {

Scanner scn=new Scanner(System.in);
int n=scn.nextInt();

for(int i=1;i<=n;i++)
{
System.out.print(fab(i)+" ");
}


}
public static int fab(int n)
{
if(n==1||n==2)
{
return 1;
}
return fab(n-1)+fab(n-2);
}

}



Make A simple Calculator In Java

import java.util.*;

public class CalculatorUsingMethods
 {
static double add(double num1, double num2)
{
return num1+num2;
}
static double subtract(double num1, double num2)
{
return num1-num2;
}
static double multiply(double num1, double num2)
{
return num1*num2;
}
static double division(double num1, double num2)
{
return num1/num2;
}
public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
System.out.println(" type first number ");
double num1=scn.nextDouble();
System.out.println(" type second no.");
double num2=scn.nextDouble();
System.out.println("type operator");
String op=scn.next();
if(op.equals("+"))
System.out.println(add(num1, num2));
else if(op.equals("-"))
System.out.println(subtract(num1, num2));
else if(op.equals("*"))
System.out.println(multiply(num1, num2));
else if(op.equals("/"))
System.out.println(division(num1, num2));
else
System.out.println(" bad operator");

}

}

Check Armstrong no . in java

  1. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.


import java.util.*;

public class Armstrong
{

public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
System.out.println("enter a no. to check no is armstrong or not");

int n=scn.nextInt();
int c=0,a,b;
b=n;
while(n>0)
{
 a=n%10;
 n=n/10;
 c=c+(a*a*a);
}
if(b==c)
{
  System.out.println("number is armstrong" );
}

else
{
  System.out.println("no. is not armstrong" );
}

}
}

Output :

enter a no. to check no is armstrong or not
371
number is armstrong


Get Id And Name Of Employee And Print Them in java

import java.util.Scanner;


class Emp
{
int id;
String name;

Emp(int i, String n)
{
id=i;
name=n;
}
public void display()
{
System.out.println(id+"\t"+name);
}
}
public class PrintempDetail {


public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
System.out.println("type size ");
int  n=scn.nextInt();

Emp [] arr=new Emp[n];
for(int i=0;i<n;i++)
{
System.out.print("enter id :");
int id=scn.nextInt();
System.out.print("Enter name :");
String name=scn.next();
arr[i]=new Emp(id, name);
}

System.out.println("\nThe employee  data is");
for(int i=0;i<arr.length;i++)
{
arr[i].display();
}


}

}

Output :

type size
2
enter id :1
Enter name :hari
enter id :2
Enter name :jay

The employee  data is
1 hari
2 jay

Arrays in Increasing Order Triangle



public class PatternForm
{
public static void main(String args[])

{
int [][]a=new int[5][5];
a[0][0]=1;
a[1][1]=2;
a[2][2]=3;
a[3][3]=4;
a[4][4]=5;
int k=0;
for(int i=0;i<a.length;i++)
for(int j=0;j<=i;j++)
{
a[i][j]=k;
k++;
}
for(int i=0;i<a.length;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();

}

}


}



Output :

0
1   2
3   4    5
6   7    8   9
10 11 12 13 14


Transpose Matrix in java

matrix which is formed by turning all the rows of a given matrix into columns and vice-versa. The transpose of matrix A is written AT.


public class TransposeMatrix {

public static void metrix(int[] arr, int j)
{
for(int i=0;i<arr.length;i++)
{
if((i+1)%j==1)
{
System.out.println();
}
System.out.print(" "+arr[i]);
}
System.out.println(" transpose matrix");


}
public static void main(String[] args)
{
int [] a={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int j=2;

metrix(a,j);
}

 }


Output :
1 2
 3 4
 5 6
 7 8
 9 10
 11 12
 13 14
 15 16 transpose matrix


Multi Dimensional Array Program In Java







import java.util.Scanner;

public class Multidimen
{
public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
System.out.println("Type two no. for matrix");
int x=scn.nextInt();
int y=scn.nextInt();
int [][] a= new int [x][y];
int i, j, k=0;
for( i=0;i<x;i++)
   for(j=0;j<y;j++)
{
a[i][j]=k;
        k++;
}
for( i=0; i<x;i++)
{
for(j=0; j<y; j++)
{
System.out.print(a[i][j] +" ");
}
 }  
System.out.println();
}
}



Output :
Type two no. for matrix
3
4
0 1 2 3
4 5 6 7
8 9 10 11


Find and Count Items in a cart Or Menu Card





public class MenuCard
{
public static void main(String[] args)
        {

   String[] menu = {"apples","apples", "bananas", "pears", "milk", "cookies", "bread", "lemons"};  
   int count=0;
   Scanner user_input = new Scanner(System.in);
   System.out.println(" type any item to check in menu");
   String item;
   item = user_input.next();
   for(String s:menu)
   {
     if(s.equalsIgnoreCase(item))
     {
     count++;
             
     }
       
   }
   System.out.println("no. of item is "+count);
   }
}

output :

type any item to check in menu
apples
no. of item is 2

Custome Sorting Program In Arrays

if you have any query please comment bellow :



import java.util.Scanner;

public class ArraySort
{

public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
System.out.println("type size for array");
int size=scn.nextInt();

int [] arr=new int[size];
for(int i=0;i<size;i++)
{
System.out.print("type no. for sorting :");
arr[i]=scn.nextInt();
}
int limit=size-1;
boolean flag=false;
int temp;
for(int i=0;i<limit;i++)
{
for(int j=0;j<limit-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
flag=true;
}
}
if(flag==false)break;
else flag=false;

}
System.out.println("Sorted array are");
for(int i=0;i<size;i++)
{
System.out.println(arr[i]);
}

}

}

Average Using ArrayList


import java.util.*;

public class ArrayListForAverage {

public static void main(String[] args)
{

double average;
int total=0;


ArrayList<Integer> al=new ArrayList<Integer>();
al.add(56);
al.add(75);
al.add(83);
al.add(82);
al.add(82);
for(int i=0; i<al.size(); i++)
{
total+=al.get(i);
}
average=total/al.size();
System.out.println("average is =" +average );

ArrayList<Integer> al1=new ArrayList<Integer>();
for(int i=0;i<=10; i++)
{
al1.add(i);
System.out.println(i);
}
System.out.println("size of array is = "+al1.size());

}

}

Staircase

Problem Statement
Your teacher has given you the task to draw the structure of a staircase. Being an expert programmer, you decided to make a program for the same. You are given the height of the staircase. You need to print a staircase as shown in the example.
Input Format
You are given an integer N depicting the height of the staircase.
Constraints
1N100
Output Format
Draw a staircase of height N in the format given below.
For example:
     #
    ##
   ###
  ####
 #####
######
Staircase of height 6, note that last line has 0 spaces before it.
Sample Input
6
Sample Output
     #
    ##
   ###
  ####
 #####
######



import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
       Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if((i+j)>n)
{
System.out.print("#");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
    }
}

Monday, June 29, 2015

Plus Minus

Problem Statement
You're given an array containing integer values. You need to print the fraction of count of positive numbers, negative numbers and zeroes to the total numbers. Print the value of the fractions correct to 3 decimal places.
Input Format
First line contains N, which is the size of the array.
Next line contains N integers A1,A2,A3,,AN, separated by space.
Sample Input
6
-4 3 -9 0 4 1          
Sample Output
0.500
0.333
0.167
Explanation
There are 3 positive numbers, 2 negative numbers and 1 zero in the array.
Fraction of the positive numbers, negative numbers and zeroes are 36=0.50026=0.333and 16=0.167 respectively.



import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {double negative=0;
double positive=0;
double zero=0;
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int [] arr=new int[n];
for(int m:arr)
{
m=scn.nextInt();
if(m==0)
{
zero++;
}
else if(m<0)
{
negative++;
}
else if(m>0)
{
positive++;
}
}
System.out.println(positive/n);
System.out.println(negative/n);
System.out.println(zero/n);
    }
}