Labels

Monday, February 15, 2016

A java program to calculate combinations of r objects from a set of n objects

A java program to calculate combinations of r objects from a set of n objects;

import java.util.Scanner;
class Recursion{
    double c,n;
    double A (double n){
       if (n==1)
           return 1;
       c=A(n-1)*n;
       return c;
    }
    double B1(double r)
    {
        if(r==1)
           return 1;
        n=B1(r-1)*r;
        return n;
    }
    double B2(double d)
    {
        double r;
        if(d==1)
            return 1;
        r=B2(d-1)*d;
        return r;
    }
}
public class comb {
    public static void main(String[] args)
    {
        Scanner s=new Scanner(System.in);
        System.out.print("Enter the highest power of combination: ");
        double n= s.nextDouble();
        System.out.print("Enter the power till which user wants answer: ");
        double r= s.nextDouble();
        Recursion rec=new Recursion();
        double a,b,diff;
        diff=n-r;
        a= rec.A(n);
        b= rec.B1(r)*rec.B2(diff);
        double result= a/b;
        System.out.println("Result: "+result);
    }
}

To run source code follow following steps:-
  • Save file with name comb.java
  • Open terminal or cmd and move to file location
  • Type: javac comb.java
  • Type: java comb

Tuesday, February 9, 2016

A Java program to show use of abstract class;

A Java program to show use of abstract class;

abstract class A{
    abstract void callme();
    void callmeto()
    {
        System.out.println("This is a concrete class");
    }
}
class B extends A{
    @Override
    void callme()
    {
        System.out.println("B's implementation on callme()");
    }
}
class C extends A{
    @Override
    void callme()
    {
        System.out.println("C's implementation of callme()");
    }
}
public class Abstract_demo {
    public static void main(String[] args)
    {
        B b=new B();
        C c=new C();
        b.callmeto();
        b.callme();
        c.callme();
    }

}

To run the program follow following step:-

  • Save code with name Abstract_demo.java
  • Open terminal and cmd and move to file location.
  • Compile: javac Abstract_demo.java
  • Run: java Abstract_demo




Monday, February 8, 2016

A Java program to find the largest and smallest in a array;

A Java program to find the largest and smallest in a array;

public class array {
       
        public static void main(String[] args) {
              
                int numbers[] = new int[]{12,34,1034,34,56,67,89,01,345,235,56};
                int smallest = numbers[0];
                int largetst = numbers[0];
                for(int i=1; i< numbers.length; i++)
                {
                        if(numbers[i] > largetst)
                                largetst = numbers[i];
                        else if (numbers[i] < smallest)
                                smallest = numbers[i];                      
                }
                System.out.println("Largest Number is : " + largetst);
                System.out.println("Smallest Number is : " + smallest);
        }
}

To compile follow following steps:-
  • Save the code with file name array
  • Open terminal or cmd and move to the file location.
  • Type javac array.java
  • Type java array

Saturday, February 6, 2016

A Java program to accept array element from user;

A Java program to accept array element from user;

import java.io.*;

public class user_array {
    public static void main(String[] args) throws IOException
    {
        int n;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the length of array: ");
        n= Integer.parseInt(br.readLine());
        int a[]= new int[n];
        for(int i=0;i        {
            System.out.print("Enter the "+(i+1)+" element of the array: ");
            a[i]= Integer.parseInt(br.readLine());
        }
        for(int i=0;i        {
            System.out.println("The "+(i+1)+" element of array: "+a[i]);
        }
    }
}



To run the above code follow following steps:-

  • Save the above code with file name user_array and extension .java
  • Open terminal or cmd and move to location of file using cd command.
  • To compile type: javac user_array.java
  • To run type: java user_array

A Java program to use multiple methods in a class;

A Java program to use multiple methods in a class;

import java.util.Scanner;

class Teacher{
    String n, a, s, h;
    long p;
    double ms, it;
    void accept()
    {
        Scanner i=new Scanner(System.in);
        System.out.print("Enter name of the teacher: ");
        n= i.nextLine();
        System.out.print("Enter address of the teacher: ");
        a= i.nextLine();
        System.out.print("Enter phone number of the teacher: ");
        p= i.nextLong();
        System.out.print("Enter highest qualification of the teacher: ");
        h= i.nextLine();
        System.out.print("Enter subject specialization of the teacher: ");
        s= i.nextLine();
        System.out.print("Enter monthly salary of the teacher: ");
        ms= i.nextDouble();
    }
    void display()
    {
        System.out.println("Detail of teacher:-\nName: "+n+"\nAddress: "+a+
                        "\nPhone Number: "+p+"\nHighest Qualification: "+h+
                        "\nSubject Specialization: "+s+"\nMonthly Salary: "+ms);
    }
    double isalary()
    {
        int year;
        Scanner i= new Scanner(System.in);
        System.out.print("Enter the years of service: ");
        year= i.nextInt();
        if (year>1)
        {
            ms= ms+ (year*5000);
        }
        return ms;
    }
    double annual()
    {
        double ann;
        ann= ms*12;
        if(ann<=150000)
            it=0;
        else
        {
            it= (5*ann)/100;
        }
        return it;
    }
}
public class multi {
    public static void main(String[] args){
        Teacher t=new Teacher();
        t.accept();
        t.display();
        System.out.println("Monthly Salary of teacher: "+t.isalary());
        System.out.println("Income tax of the teacher: "+t.annual());
    }
}

To run the above code follow following steps:-

  • Save the above code with file name multi and extension .java
  • Open terminal or cmd and move to location of file using cd command.
  • To compile type: javac multi.java
  • To run type: java multi

Thursday, February 4, 2016

Accept user data using Java IO package;

Accept user data using Java IO Package;

import java.io.*;

public class accept {
public static void main(String[] args) throws IOException{
int a;
String b;
double c;
char d;
long e;
short f;
float g;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter an integer: ");
a= Integer.parseInt(br.readLine());
System.out.println("Enter a string: ");
b= br.readLine();
System.out.println("Enter a double: ");
c= Double.parseDouble(br.readLine());
System.out.println("Enter a character: ");
d= (char) br.read();
System.out.println("Enter a long: ");
e= Long.parseLong(br.readLine());
System.out.println("Enter a short: ");
f= Short.parseShort(br.readLine());
System.out.println("Enter a float: ");
g= Float.parseFloat(br.readLine());
System.out.println(a+"\n"+b+"\n"+c+"\n"+d+"\n"+e+"\n"+f+"\n"+g);
}

}

To run the above code follow following steps:-

  • Save the above code with file name accept and extension .java
  • Open terminal or cmd and move to location of file using cd command.
  • To compile type: javac accept.java
  • To run type: java accept

Accept user data using Scanner class;

Using Scanner Class in Java to input user data;

import java.util.Scanner;

public class scan {
public static void main(String[] args){
int a;
String b;
double c;
long e;
float f;
short g;
Scanner s= new Scanner(System.in);
System.out.print("Enter a integer: ");
a= s.nextInt();
System.out.print("Enter a string: ");
b= s.nextLine();
System.out.print("Enter a double: ");
c= s.nextDouble();
System.out.print("Enter a long: ");
e= s.nextLong();
System.out.print("Enter a float: ");
f= s.nextFloat();
System.out.print("Enter a short: ");
g= s.nextShort();
System.out.println(a+"\n"+b+"\n"+c+"\n"+e+"\n"+f+"\n"+g);
}

}

To compile the following code do following things:-

  • Save the above code with a file name scan and with extension .java
  • Now, open your terminal or cmd and move to the location where your file is being saved using cd command.
  • Compile java code using javac . Here file name is scan so type javac scan.java
  • Run the code using java . Here file name is scan so type java scan


Thursday, December 24, 2015

To print the next character accepted from the user;

To print the next character accepted from the user;
#include
int main()
{
char a,b;
printf("Enter any character: ");
a= getchar();
int c=a;
int d=c+1;
printf("The alterante of character user entered is ");
b= putchar(d);
printf("\n");
return 0;
}

Print next character

To compile this program in Linux:
  1. Save this file with extension .c
  2. g++ compiler come pre installed in linux. Type this in terminal g++ alternate.c -o alternate. Here, I have assumed that the file name is alternate.c. -o array will provide me with an output file with extension exe file and name alternate.
  3. Now, run this file typing ./alternate and press enter.
To compile this program using cmd:
  1. Save this file with extension.c
  2. Type this in the cmd g++ alternate.c -o alternate.
  3. To run type alternate and press enter.
Note: To use g++ compiler in windows user has to install these compiler by itself as g++ compiler does not come per installed in windows operating system.



Wednesday, December 23, 2015

Largest of three numbers using Macro;

A C program to find the largest of three numbers using macro;

#include
#define CONDITION if (b>=97 && b<=122)\
printf("The character u entered is a small alphabet.\n");\
else\
{\
if(b>=65 && b<=112)\
printf("The character you entered is a large alphabet.\n");\
else\
printf("You have not entered a alphabet.\n");\
}
int main()
{
char a;
printf("Enter any character: ");
a= getchar();
int b= a;
CONDITION;
return 0;

}

Largest of three using Macro
















To compile this program in Linux:
  1. Save this file with extension .c
  2. g++ compiler come pre installed in linux. Type this in terminal g++ display.c -o display. Here, I have assumed that the file name is display.c. -o array will provide me with an output file with extension exe file and name display.
  3. Now, run this file typing ./display and press enter.
To compile this program using cmd:
  1. Save this file with extension.c
  2. Type this in the cmd g++ display.c -o display.
  3. To run type display and press enter.
Note: To use g++ compiler in windows user has to install these compiler by itself as g++ compiler does not come per installed in windows operating system.




Thursday, December 17, 2015

The largest of three numbers;

A C program to find the largest of three numbers;

#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter first number ");
scanf("%d",&a);
printf("Enter second number ");
scanf("%d",&b);
printf("Enter third number ");
scanf("%d",&c);
if(a>b)
{
if(a>c)
printf("First is largest\n");
}
else
{
if(b>c)
printf("Second is largest\n");
else
printf("Third is largest\n");
}
return 0;
}
Largest of three number
Largest of three number

To compile this program in Linux:
  1. Save this file with extension .c
  2. g++ compiler come pre installed in linux. Type this in terminal g++ max.c -o max. Here, I have assumed that the file name is max.c. -o array will provide me with an output file with extension exe file and name max.
  3. Now, run this file typing ./max and press enter.
To compile this program using cmd:
  1. Save this file with extension.c
  2. Type this in the cmd g++ max.c -o max.
  3. To run type max and press enter.
Note: To use g++ compiler in windows user has to install these compiler by itself as g++ compiler does not come per installed in windows operating system.