Applet for Addition
Read in your tongue here..
Wednesday, June 25, 2014
Tuesday, May 27, 2014
Abstract class in Java
import java.util.Scanner;
abstract class Person{
String name, city;
Scanner sc=new Scanner(System.in);
}
class Emp extends Person{
int empcode;
float bsal;
void get()
{
System.out.println("Enter employee name:");
name=sc.nextLine();
System.out.println("Enter city:");
city=sc.nextLine();
System.out.print("Enter employee code:");
empcode=sc.nextInt();
System.out.print("Enter basic salary:");
bsal=sc.nextFloat();
}
void disp()
{
System.out.println("Employee name:"+name);
System.out.println("City:"+city);
System.out.println("Employee Code:"+empcode);
System.out.println("Basic Salary:"+bsal);
}
}
class Stu extends Person{
int roll;
String course;
void get()
{
System.out.println("Enter student name:");
name=sc.nextLine();
System.out.println("Enter city:");
city=sc.nextLine();
System.out.print("Enter course:");
course=sc.nextLine();
System.out.print("Enter roll:");
roll=sc.nextInt();
}
void disp()
{
System.out.println("Student name:"+name);
System.out.println("City:"+city);
System.out.println("Roll:"+roll);
System.out.println("Course:"+course);
}
}
public class Abs{
public static void main(String args[])
{
Emp obj1=new Emp();
Stu obj2=new Stu();
obj1.get();
obj2.get();
obj1.disp();
obj2.disp();
}
}
abstract class Person{
String name, city;
Scanner sc=new Scanner(System.in);
}
class Emp extends Person{
int empcode;
float bsal;
void get()
{
System.out.println("Enter employee name:");
name=sc.nextLine();
System.out.println("Enter city:");
city=sc.nextLine();
System.out.print("Enter employee code:");
empcode=sc.nextInt();
System.out.print("Enter basic salary:");
bsal=sc.nextFloat();
}
void disp()
{
System.out.println("Employee name:"+name);
System.out.println("City:"+city);
System.out.println("Employee Code:"+empcode);
System.out.println("Basic Salary:"+bsal);
}
}
class Stu extends Person{
int roll;
String course;
void get()
{
System.out.println("Enter student name:");
name=sc.nextLine();
System.out.println("Enter city:");
city=sc.nextLine();
System.out.print("Enter course:");
course=sc.nextLine();
System.out.print("Enter roll:");
roll=sc.nextInt();
}
void disp()
{
System.out.println("Student name:"+name);
System.out.println("City:"+city);
System.out.println("Roll:"+roll);
System.out.println("Course:"+course);
}
}
public class Abs{
public static void main(String args[])
{
Emp obj1=new Emp();
Stu obj2=new Stu();
obj1.get();
obj2.get();
obj1.disp();
obj2.disp();
}
}
Monday, May 26, 2014
Factorial by recursion using static method
package Rahul;
import java.util.Scanner;
public class Fact {
static int fact(int num)
{
if(num==0)
return 1;
else
return(num*fact(num-1));
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter number:");
int n=sc.nextInt();
System.out.print("Factorial of "+n+"="+Sec.fact(n));
}
}
import java.util.Scanner;
public class Fact {
static int fact(int num)
{
if(num==0)
return 1;
else
return(num*fact(num-1));
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter number:");
int n=sc.nextInt();
System.out.print("Factorial of "+n+"="+Sec.fact(n));
}
}
Location:
Patna, Bihar, India
Wednesday, May 21, 2014
Inheritance in Java
import java.io.*;
class Student {
String n,c,s;
int r,sem;
Student(){};
void get(String name, String cou, String ses, int roll, int seme)
{
n=name;
c=cou;
s=ses;
r=roll;
seme=sem;
}
void display(){System.out.println("Name: "+n+"\nCourse: "+c+"\nSession: "+s+"\nRoll: "+r+"\nSemester: "+sem);}
}
class Marks extends Student{
int m[]=new int[3];
Marks()
{
super();
m[0]=m[1]=m[2]=0;
}
void get(int marks[])
{
m[0]=marks[0];
m[1]=marks[1];
m[2]=marks[2];
}
void display()
{
Student:display();
System.out.println("Makrs:\n"+m[0]+"\t"+"\t"+m[1]+"\t"+m[2]);
}
}
class Main{
public static void main(String args[])throws IOException
{
InputStreamReader ni=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(ni);
Marks obj1=new Marks();
System.out.print("Enter Student's name:");
String name=String.valueOf(in.readLine());
System.out.print("Enter course:");
String cou=String.valueOf(in.readLine());
System.out.print("Enter session:");
String ses=String.valueOf(in.readLine());
System.out.print("Enter roll number:");
int roll=Integer.parseInt(in.readLine());
System.out.print("Enter semester:");
int seme=Integer.parseInt(in.readLine());
obj1.get(name,cou,ses,roll,seme);
System.out.print("Enter marks of three subjects:");
int marks[]=new int[3];
marks[0]=Integer.parseInt(in.readLine());
marks[1]=Integer.parseInt(in.readLine());
marks[2]=Integer.parseInt(in.readLine());
obj1.get(marks);
obj1.display();
}
}
class Student {
String n,c,s;
int r,sem;
Student(){};
void get(String name, String cou, String ses, int roll, int seme)
{
n=name;
c=cou;
s=ses;
r=roll;
seme=sem;
}
void display(){System.out.println("Name: "+n+"\nCourse: "+c+"\nSession: "+s+"\nRoll: "+r+"\nSemester: "+sem);}
}
class Marks extends Student{
int m[]=new int[3];
Marks()
{
super();
m[0]=m[1]=m[2]=0;
}
void get(int marks[])
{
m[0]=marks[0];
m[1]=marks[1];
m[2]=marks[2];
}
void display()
{
Student:display();
System.out.println("Makrs:\n"+m[0]+"\t"+"\t"+m[1]+"\t"+m[2]);
}
}
class Main{
public static void main(String args[])throws IOException
{
InputStreamReader ni=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(ni);
Marks obj1=new Marks();
System.out.print("Enter Student's name:");
String name=String.valueOf(in.readLine());
System.out.print("Enter course:");
String cou=String.valueOf(in.readLine());
System.out.print("Enter session:");
String ses=String.valueOf(in.readLine());
System.out.print("Enter roll number:");
int roll=Integer.parseInt(in.readLine());
System.out.print("Enter semester:");
int seme=Integer.parseInt(in.readLine());
obj1.get(name,cou,ses,roll,seme);
System.out.print("Enter marks of three subjects:");
int marks[]=new int[3];
marks[0]=Integer.parseInt(in.readLine());
marks[1]=Integer.parseInt(in.readLine());
marks[2]=Integer.parseInt(in.readLine());
obj1.get(marks);
obj1.display();
}
}
Saturday, April 19, 2014
Calculating GCD of two numbers using Java
class GCD{
public static void main(String args[])
{
int gcd=1;
int n1=Integer.parseInt(args[0]);
int n2=Integer.parseInt(args[1]);
int num=n1<n2?n1:n2;
if(num==1)
System.out.println("GCD of "+n1+" and "+n2+"="+gcd);
else
{
for(int i=2;i<=num/2;i++)
if(n1%i==0&&n2%i==0)
gcd=i;
System.out.println("GCD of "+n1+" and "+n2+"="+gcd);
}
}
}
public static void main(String args[])
{
int gcd=1;
int n1=Integer.parseInt(args[0]);
int n2=Integer.parseInt(args[1]);
int num=n1<n2?n1:n2;
if(num==1)
System.out.println("GCD of "+n1+" and "+n2+"="+gcd);
else
{
for(int i=2;i<=num/2;i++)
if(n1%i==0&&n2%i==0)
gcd=i;
System.out.println("GCD of "+n1+" and "+n2+"="+gcd);
}
}
}
Calculating roots of quadratic equation using Java
class Quad{
public static void main(String args[])
{
int a,b,c;
double d,r1,r2;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=Integer.parseInt(args[2]);
d=b*b-4*a*c;
if(d==0)
{
r1=-b+Math.sqrt(d)/(2*a);
System.out.println("Roots are real and equal:\nRoot="+r1);
}
else if(d>0)
{
r1=-b+Math.sqrt(d)/(2*a);
r2=-b-Math.sqrt(d)/(2*a);
System.out.println("Roots are real and unequal:\nRoot 1="+r1+"\nRoot 2="+r2);
}
else
System.out.println("Roots are imaginary.");
}
}
public static void main(String args[])
{
int a,b,c;
double d,r1,r2;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=Integer.parseInt(args[2]);
d=b*b-4*a*c;
if(d==0)
{
r1=-b+Math.sqrt(d)/(2*a);
System.out.println("Roots are real and equal:\nRoot="+r1);
}
else if(d>0)
{
r1=-b+Math.sqrt(d)/(2*a);
r2=-b-Math.sqrt(d)/(2*a);
System.out.println("Roots are real and unequal:\nRoot 1="+r1+"\nRoot 2="+r2);
}
else
System.out.println("Roots are imaginary.");
}
}
Prime Numbers between 1 to 100 using Java
class Prime2{
public static void main(String args[])
{
System.out.println("Prime numbers between 1 to 100 are:");
int i=1,c=0,j;
for(;i<=100;i++)
{
for(j=2;j<=i/2;j++)
if(i%j==0)
c++;
if(c==0)
System.out.print(i+" ");
c=0;
}
}
}
public static void main(String args[])
{
System.out.println("Prime numbers between 1 to 100 are:");
int i=1,c=0,j;
for(;i<=100;i++)
{
for(j=2;j<=i/2;j++)
if(i%j==0)
c++;
if(c==0)
System.out.print(i+" ");
c=0;
}
}
}
Checking Prime Number using Java
class Prime{
public static void main(String args[])
{
int num=Integer.parseInt(args[0]),c=0;
for(int i=2;i<=num/2;i++)
if(num%i==0)
c++;
if(c==0)
System.out.println(num+" is a prime number.");
else
System.out.println(num+" is not a prime number.");
}
}
public static void main(String args[])
{
int num=Integer.parseInt(args[0]),c=0;
for(int i=2;i<=num/2;i++)
if(num%i==0)
c++;
if(c==0)
System.out.println(num+" is a prime number.");
else
System.out.println(num+" is not a prime number.");
}
}
Wednesday, April 16, 2014
Armstrong Number
//Using Java
class Arm{
public static void main(String args[])
{
int num=Integer.parseInt(args[0]);
int temp=num,rem;
double sum=0;
while(temp!=0)
{
rem=temp%10;
sum=sum+Math.pow(rem,3);
temp/=10;
}
if(sum==num)
System.out.println(num+" is an armstrong number.");
else
System.out.println(num+" is not an armstrong number.");
}
}
class Arm{
public static void main(String args[])
{
int num=Integer.parseInt(args[0]);
int temp=num,rem;
double sum=0;
while(temp!=0)
{
rem=temp%10;
sum=sum+Math.pow(rem,3);
temp/=10;
}
if(sum==num)
System.out.println(num+" is an armstrong number.");
else
System.out.println(num+" is not an armstrong number.");
}
}
Lucas Series in Java
class Lucas{
public static void main(String args[])
{
int n=Integer.parseInt(args[0]),num=1,i;//Input from command line
for(i=0;i<n;i++)
{
num+=i;
System.out.print(num+" ");
}
}
}
public static void main(String args[])
{
int n=Integer.parseInt(args[0]),num=1,i;//Input from command line
for(i=0;i<n;i++)
{
num+=i;
System.out.print(num+" ");
}
}
}
Monday, April 7, 2014
Lucas Series
//Using C++
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int num=1,i,n;
cout<<"Enter number of terms:";
cin>>n;
for(i=0;i<n;i++)
{
num=num+i;
cout<<setw(2)<<num<<' ';
}
getch();
clrscr();
}
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int num=1,i,n;
cout<<"Enter number of terms:";
cin>>n;
for(i=0;i<n;i++)
{
num=num+i;
cout<<setw(2)<<num<<' ';
}
getch();
clrscr();
}
Palindrome String WITHOUT using String Functions
//Using C++
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
char *wrd;
int n=0;
cout<<"Enter a string:";
cin>>wrd;
while(wrd[n]!='\0')
n++; //Counting length of the string
n--;
for(int i=0;i<n/2;i++,n--)
{
if(wrd[i]!=wrd[n])
{
cout<<"\nNot Palindrome!";
getch();
exit(0);
}
}
cout<<"\nPalindrome.";
getch();
}
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
char *wrd;
int n=0;
cout<<"Enter a string:";
cin>>wrd;
while(wrd[n]!='\0')
n++; //Counting length of the string
n--;
for(int i=0;i<n/2;i++,n--)
{
if(wrd[i]!=wrd[n])
{
cout<<"\nNot Palindrome!";
getch();
exit(0);
}
}
cout<<"\nPalindrome.";
getch();
}
Palindrome String using String Functions
//Using C++
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char word[10],word2[10];
int n;
cout<<"Enter string:";
cin>>word;
strcpy(word2,word);
strrev(word);
if(stricmp(word,word2)==0)
cout<<"\nPalindrome.";
else
cout<<"\nNot Palindrome!";
getch();
clrscr();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char word[10],word2[10];
int n;
cout<<"Enter string:";
cin>>word;
strcpy(word2,word);
strrev(word);
if(stricmp(word,word2)==0)
cout<<"\nPalindrome.";
else
cout<<"\nNot Palindrome!";
getch();
clrscr();
}
Saturday, April 5, 2014
Deleting Duplicate Elements from an Array
/*Using C*/
#define size 10
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num[size],j;
clrscr();
printf("Enter %d numbers:",size);
for(i=0;i<size;i++)
scanf("%d",&num[i]);
puts("The array is:");
for(i=0;i<size;i++)
printf("%d ",num[i]);
for(i=0;i<size;i++)
for(j=i+1;j<size;j++)
if(num[i]==num[j])
num[j]='\0';
puts("\nUpdated array:");
for(i=0;i<size;i++)
printf("%d ",num[i]);
getch();
}
#define size 10
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num[size],j;
clrscr();
printf("Enter %d numbers:",size);
for(i=0;i<size;i++)
scanf("%d",&num[i]);
puts("The array is:");
for(i=0;i<size;i++)
printf("%d ",num[i]);
for(i=0;i<size;i++)
for(j=i+1;j<size;j++)
if(num[i]==num[j])
num[j]='\0';
puts("\nUpdated array:");
for(i=0;i<size;i++)
printf("%d ",num[i]);
getch();
}
Tuesday, February 4, 2014
Simple Arithemetic Mean
#include<iostream>
#include<conio.h>
#include<iomanip>
#include<windows.h>
using namespace std;
int i;
int main()
{
void line(void);
float sum=0;
int n;
cout<<"Enter number of observations:";
cin>>n;
float num[n];
cout<<"Enter "<<n<<" data values:";
for(i=0;i<n;i++)
{
cin>>num[i];
sum+=num[i];
}
system("cls"
);
line();
cout<<setw(5)<<"S.No."<<setw(15)<<"Observations";
line();
for(i=0;i<n;i++)
cout<<setw(5)<<i+1<<setw(9)<<num[i]<<endl;
line();
cout<<setw(5)<<n<<setw(9)<<sum;
line();
cout<<"\nAverage="<<setprecision(2)<<sum/n;
getch();
return sum/n;
}
void line(void)
{
cout<<endl;
for(i=0;i<25;i++)
cout<<'_';
cout<<endl;
}
#include<conio.h>
#include<iomanip>
#include<windows.h>
using namespace std;
int i;
int main()
{
void line(void);
float sum=0;
int n;
cout<<"Enter number of observations:";
cin>>n;
float num[n];
cout<<"Enter "<<n<<" data values:";
for(i=0;i<n;i++)
{
cin>>num[i];
sum+=num[i];
}
system("cls"
);
line();
cout<<setw(5)<<"S.No."<<setw(15)<<"Observations";
line();
for(i=0;i<n;i++)
cout<<setw(5)<<i+1<<setw(9)<<num[i]<<endl;
line();
cout<<setw(5)<<n<<setw(9)<<sum;
line();
cout<<"\nAverage="<<setprecision(2)<<sum/n;
getch();
return sum/n;
}
void line(void)
{
cout<<endl;
for(i=0;i<25;i++)
cout<<'_';
cout<<endl;
}
Falling String Character wise
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<dos.h>
#include<iomanip.h>
void main()
{
int i,j,l,t=1;
char* word;
cout<<"Enter string: ";
gets(word);
l=strlen(word);
for(i=0;i<l;i++,t++)
{
for(j=2;j<=24;j++)
{
clrscr();
cout<<word+i;
clrscr();
cout<<setw(l)<<word+i+1;
gotoxy(t,j);
cout<<word[i];
delay(100);
}
}
getch();
clrscr();
}
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<dos.h>
#include<iomanip.h>
void main()
{
int i,j,l,t=1;
char* word;
cout<<"Enter string: ";
gets(word);
l=strlen(word);
for(i=0;i<l;i++,t++)
{
for(j=2;j<=24;j++)
{
clrscr();
cout<<word+i;
clrscr();
cout<<setw(l)<<word+i+1;
gotoxy(t,j);
cout<<word[i];
delay(100);
}
}
getch();
clrscr();
}
Subscribe to:
Posts (Atom)