Tuesday, November 10, 2015

Coding for calculator using Java codes

import java.awt.*;
import java.awt.event.*;
public class Calc extends Frame implements ActionListener
{
Frame f;
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1,b2,b3,b4,b5;
public Calc()
{
 l1=new Label("Enter 1st no.");
l2=new Label("Enter 2nd no.");
l3=new Label("result");
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
b1=new Button("Add");
b2=new Button("sub");
b3=new Button("mul");
b4=new Button("div");
b5=new Button("mod");
f=new Frame();
f.setLayout(new FlowLayout());
f.setSize(600,600);
f.setBackground(Color.pink);
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(l3);
f.add(t3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s1=t1.getText();
String s2=t2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(ae.getSource()==b1)
{
c=a+b;
}
if(ae.getSource()==b2)
{
c=a-b;
}

if(ae.getSource()==b3)
{
c=a*b;
}
if(ae.getSource()==b4)
{
c=a/b;
}

if(ae.getSource()==b5)
{
c=a%b;
}
String s3=String.valueOf(c);
t3.setText(s3);
}
public static void main(String args[])
{
Calc obj=new Calc();
}
}


Applet Program (Java)

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
//<applet code="App" width=1000 height=800></applet>
public class App extends Applet implements ItemListener
{
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1,b2;
Choice ch1, ch2;
Checkbox cb,cb1,cb2;
CheckboxGroup cbg1;
Checkbox male,female;
TextArea ta;
List li;

public void init()
{
setBackground(Color.pink);
setForeground(Color.green);
setFont(new Font("Aerial",Font.BOLD,13));
}
public void start()
{
l1=new Label("First Name",Label.CENTER);

l2=new Label("Second Name",Label.CENTER);
l3=new Label("Result",Label.CENTER);
t1=new TextField(30);
t2=new TextField(30);
t3=new TextField(30);
//t1.setEchoChar('*');
//l1.setText("First Name");

b1=new Button("save");

b2=new Button("Delete");

ch1=new Choice();
ch2=new Choice();
ch1.add("India");
ch1.add("Pak");
ch1.add("Nep");


ch2.add("Bihar");
ch2.add("karanchi");
ch2.add("kathmandu");

cb=new Checkbox("Patel");
cbg1 =new CheckboxGroup ();
cb1=new Checkbox("java");
cbg1 =new CheckboxGroup ();
cb2=new Checkbox("C++");
cbg1 =new CheckboxGroup ();


male=new Checkbox("Male");
male.setCheckboxGroup (cbg1);

female=new Checkbox("Female");
female.setCheckboxGroup (cbg1);

ta=new TextArea(5,20);

li=new List();


li.addItem("Sunday");
li.addItem("Monday");
li.addItem("Tuesday");

add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(b2);
add(ch1);
add(ch2);
add(cb);
add(cb1);
add(cb2);
add(male);
add(female);
add(ta);

add(li);


cb.addItemListener(this);
cb1.addItemListener(this);
cb2.addItemListener(this);



}


public void itemStateChanged (ItemEvent ie)
{

if(ie.getItemSelectable ()==cb)
{
if(cb.getState())
{
t1.setText("Patel");
}
}
else
{
t1.setText(" ");
}
if(ie.getItemSelectable ()==cb1)
{
if(cb1.getState())
{
t1.setText("Java");
}
}
else
{
t1.setText(" ");
}

if(ie.getItemSelectable ()==cb2)
{
if(cb2.getState())
{
t1.setText("C++");
}
}
else
{
t1.setText(" ");
}
if(cb2.getState())
{
t1.setText("C++");
}
else
{
t1.setText(" ");
}
}
}
public static void main(String args[])
{
App obj=new App();
}



Saturday, September 19, 2015

C++(Age )

If the ages of Ram, Sulabh and Ajay are input by the user, write a program to determine the youngest of the three. 


 int main()
{
      int ram_age,sulabh_age,ajay_age;
      cout<<"Enter Ram age:";
      cin>>ram_age;
      cout<<"Enter Sulabh age:";
      cin>>sulabh_age;
      cout<<"Enter Ajay age:";
      cin>>ajay_age;
 
      if (ram_age<sulabh_age && ram_age<ajay_age)
            cout<<"Ram is youngest";
      else if(sulabh_age<ram_age && sulabh_age<ajay_age)
            cout<<"Sulabh is youngest";
      else
            cout<<"Ajay is youngest";
 
      getch();
      return 0;

}

Tuesday, October 14, 2014

#include<stdio.h>
#include<conio.h>
 void main()
 {
int arr[10];
int i,j;
int temp;
int count;

for(i=0;i<10;i++)
{
printf(" Enter value= ");
scanf("%d",&arr[i]);
}
printf("\nUnsorted array -->\n");
for(i=0;i<10;i++)
printf("%d\t",arr[i]);

for(i=0;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\nSorted array -->\n");
for(i=0;i<10;i++)
printf("%d\t",arr[i]);

i=0;
while(i<10)
{
j=i;
count=1;
while(arr[j]==arr[j+1])
{
count++;
j++;
}
printf("\nFrequency of %d= %d",arr[j],count);
i=i+count;
}
getch();
 }

Monday, December 16, 2013

Array




#include<stdio.h>
#include<conio.h>
 void main()
 {
int arr[10];
int i,j;
int temp;
int count;

for(i=0;i<10;i++)
{
printf(" Enter value= ");
scanf("%d",&arr[i]);
}
printf("\nUnsorted array -->\n");
for(i=0;i<10;i++)
printf("%d\t",arr[i]);

for(i=0;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\nSorted array -->\n");
for(i=0;i<10;i++)
printf("%d\t",arr[i]);

i=0;
while(i<10)
{
j=i;
count=1;
while(arr[j]==arr[j+1])
{
count++;
j++;
}
printf("\nFrequency of %d= %d",arr[j],count);
i=i+count;
}
getch();
 }

ARRAY

// WAP TO INPUT A NO. AND SEARCH THAT NO. IN 10 ELEMENTS ARRAY.

#include<stdio.h>
#include<conio.h>
 void main()
 {
int arr[10];
int i;
int sd;

clrscr();

for(i=0;i<10;i++)
{
printf(" Enter value= ");
scanf("%d",&arr[i]);
}
printf("\nGi");
for(i=0;i<10;i++)
printf("%d",arr[i]);


 }

SORTING

WAP TO ENTER 10 NO.S IN AN ARRAY AND ARRANGE THAT NO.S IN ASCENDING ORDER.
    (2nd METHOD- SELECTION SORT) */


#include<stdio.h>
#include<conio.h>
 void main()
 {
int arr[10];
int i,j;
int temp;
int small,pos;

clrscr();

for(i=0;i<10;i++)
{
printf(" Enter value= ");
scanf("%d",&arr[i]);
}
printf("\nUnsorted array -->\n");
for(i=0;i<10;i++)
printf("%d\t",arr[i]);

for(i=0;i<10;i++)
{
small=arr[i];
for(j=i;j<10;j++)
{
if(small>arr[j])
{
small=arr[j];
pos=j;
}
}
temp=arr[i];
arr[i]=arr[pos];
arr[pos]=temp;
}
printf("\nSorted array -->\n");

for(i=0;i<10;i++)
printf("%d\t",arr[i]);

getch();
 }

/* WAP TO PRINT THE FOLLOWING FORMAT  :-


A B C D E F G F E D C B A
A B C D E F     F E D C B A
A B C D E            E D C B A
A B C D                   D C B A
A B C                           C B A
A B                                   B A
A                                          A  


#include<stdio.h>
#include<conio.h>
 void main()
 {
int i,j;
int k,l,m;

clrscr();

j=71;
k=j;

for(i=0;i<7;i++)
{
l=65;
while(l<=k)
{
printf(" %c",l);
l++;
}
if(i==0)
l=l-2;
else
{
for(m=1;m<(2*i-1);m++)
{
printf(" ");
}
l=l-1;
}
while(l>=65)
{
if(i==0)
printf(" %c",l);
else
printf(" %c",l);

l--;
}
printf("\n");
k--;
i++;
}
getch();
 }

FOLLOWING FORMAT

/* WAP TO PRINT THE FOLLOWING FORMAT  :-


   1
         2   3
4   5   6
     7   8   9   10  



#include<stdio.h>
#include<conio.h>
 void main()
 {
int i,j,m=1;
int space;

clrscr();

space=3;
for(i=1;i<=4;i++)
{
for(j=1;j<=space;j++)
{
printf("  ");                     // 2-SPACES.
}
for(j=1;j<=i;j++)
{
printf("%d   ",m);
m++;
}
printf("\n");
space=space-1;
}
getch();
 }
/* WAP TO PRINT THE FOLLOWING FORMAT  :-
   *
 *   *
*   *   *
     *   *   *   *
   *   *   *   *   *          


#include<stdio.h>
#include<conio.h>
 void main()
 {
int i,j;
int row,space;

clrscr();

printf("Enter the no. of rows you want= ");
scanf("%d",&row);
clrscr();
space=row-1;
for(i=1;i<=row;i++)
{
for(j=1;j<=space;j++)
{
printf("  ");                     // 2-SPACES.
}
for(j=1;j<=i;j++)
{
printf("*   ");                   // 1 * & 3-SPACES.
}
printf("\n");
space=space-1;
}
getch();
 }

USING 3rd VARIABLE

WAP TO INPUT TWO NO.S AND INTERCHANGE THEIR VALUE USING 3rd VARIABLE

#include<stdio.h>
#include<conio.h>

void main()
{
      int num1;
      int num2;
      int num3;

      clrscr();
      printf("\nEnter 1st no.=");
      scanf("%d",&num1);
      printf("Enter 2nd no.=");
      scanf("%d",&num2);
      num3=num1;
      num1=num2;
      num2=num3;
      printf("\nswapped value of num1=%d",num1);
      printf("\nswapped value of num2=%d\n",num2);
      getch();

}

Friday, December 13, 2013

 /* WAP TO PRINT THE FOLLOWING FORMAT  :-

     *
 *    *
*   *   *
     *   *   *   *
   *   *   *   *   *
      *
      *
      *
      *
      *                


#include<stdio.h>
#include<conio.h>
 void main()
 {
int i,j,k;
int row,space;

clrscr();

printf("Enter the total no. of rows you want= ");
scanf("%d",&row);
clrscr();
space=row/2-1;
for(i=0;i<row/2;i++)
{
for(j=0;j<space;j++)
printf("  ");

if(i==0 || i==row/2-1)
{
for(j=0;j<=i;j++)
printf("*   ");
}
else
{
for(j=0;j<2;j++)
{
printf("*");
for(k=0;k<4*i-1;k++)
printf(" ");
}
}
printf("\n");
space=space-1;
}
for(i=row/2;i<row;i++)
{
space=row/2-1;
for(j=0;j<space;j++)
{
printf("  ");
}
printf("*\n");
}
getch();
 }

Tuesday, November 19, 2013

STRING


Que:- WAP TO INPUT ANY STRING AND COUNT NO. OF VOWELS.

#include<stdio.h>
#include<conio.h>

 void main()
 {
char str[20];
int i;
int vc=0;

clrscr();

printf("\n Enter any string -->  ");
scanf("%s",str);

i=0;
while(str[i]!='\0')
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
vc++;
i++;
}
printf("\n No. of vowels in %s= %d",str,vc);
getch();
 }

Wednesday, November 13, 2013

FOLLOWING FORMAT

/* WAP TO PRINT THE FOLLOWING FORMAT  :-
   *
 *   *
*   *   *
     *   *   *   *
   *   *   *   *   *           



#include<stdio.h>
#include<conio.h>
 void main()
 {
int i,j;
int row,space;

clrscr();

printf("Enter the no. of rows you want= ");
scanf("%d",&row);
clrscr();
space=row-1;
for(i=1;i<=row;i++)
{
for(j=1;j<=space;j++)
{
printf("  ");                     // 2-SPACES.
}
for(j=1;j<=i;j++)
{
printf("*   ");                   // 1 * & 3-SPACES.
}
printf("\n");
space=space-1;
}
getch();
 }

FOLLOWING FORMAT

/* WAP TO PRINT THE FOLLOWING FORMAT  :-
   *
 *   *
*   *   *
     *   *   *   *
   *   *   *   *   *           



#include<stdio.h>
#include<conio.h>
 void main()
 {
int i,j;
int row,space;

clrscr();

printf("Enter the no. of rows you want= ");
scanf("%d",&row);
clrscr();
space=row-1;
for(i=1;i<=row;i++)
{
for(j=1;j<=space;j++)
{
printf("  ");                     // 2-SPACES.
}
for(j=1;j<=i;j++)
{
printf("*   ");                   // 1 * & 3-SPACES.
}
printf("\n");
space=space-1;
}
getch();
 }

/*WAP TO PRINT THE FOLLOWING FORMAT:
      *
      # #
      * * *
      # # # #
      * * * * *      


#include<stdio.h>
#include<conio.h>
 void main()
 {
int i;
int j;

clrscr();

for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(i%2!=0)
printf(" *");
else
printf(" #");
}
printf("\n");
}
getch();
 }












#include<stdio.h>
#include<conio.h>
 void main()
 {
int i,j;
int row,space;

clrscr();

printf("Enter the no. of rows you want= ");
scanf("%d",&row);
clrscr();
space=row-1;
for(i=1;i<=row;i++)
{
for(j=1;j<=space;j++)
{
printf("  ");
}
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
space=space-1;
}
getch();
 }

Tuesday, November 5, 2013

SUM OF THE SREIES

/*THE NATURAL LOGARITHM CAN BE APPROXIMATED BY THE FOLLOWING SERIES: (x-1/x)+[1/2(x-1/x)*(x-1/x)]+[1/2(x-1/x)*(x-1/x)*(x-1/x)].....upto 7 terms.  IF x IS INPUT THROUGH THE KEYBOARD, WAP OT CALCULATE SUM OF THE SREIES*/



#include<stdio.h>
#include<conio.h>
#include<math.h>
 void main()
 {
int x;
int i;
float sum=0;

clrscr();

printf("\nEnter the value of x= ");
scanf("%d",&x);

if(x!=0)
{
for(i=1;i<=7;i++)
{
if(i==1)
sum=sum+pow(((x-1.0)/x),i);

else
sum=sum+(1.0/2)*pow(((x-1.0)/x),i);
}
printf("\nSum of the given series= %f",sum);
}

else
printf("\n\tERROR.\n\tYou have entered a wrong input.");
getch();
 }

Thursday, October 24, 2013

FOLLOWING SERIES

/*WAP TO ADD 1st SEVEN TERMS OF THE FOLLOWING SERIES: (1/1!)+(2/2!)+.......+(7/7!) [using FOR loop]*/


#include<stdio.h>
#include<conio.h>
 void main()
 {
int i,j;
float fact;
float sum=0;

clrscr();
printf("\nGiven series -->\n\t\t");
printf("1/1! + 2/2! + 3/3! + 4/4! + 5/5 + 6/6! + 7/7!");
for(i=1;i<=7;i++)
{
fact=1;
for(j=1;j<=i;j++)
{
fact=fact*j;
}
sum=sum+i/fact;
}
printf("\n\n\nSum of the 1st seven terms of the given series= %f",sum);
getch();
 }

ARMSTRONG NO

/* WAP TO PRINT AND COUNT ARMSTRONG NO.S FROM 1 TO 1000.



#include<stdio.h>
#include<conio.h>
 void main()
 {
int num;
int n;
int d,sum;
int count=0;

clrscr();

printf("\nArmstrong no.s from 1 to 1000 -->\n\n\t\t");
num=1;
while(num<1000)
{
n=num;
sum=0;
while(n>0)
{
d=n%10;
sum+=d*d*d;
n=n/10;
}
if(num==sum)
{
printf("%d\t",num);
count++;
}
num++;
}
printf("\n\nTotal no. of Armstrong no.s from 1 to 1000 = %d",count);
getch();
 }

Wednesday, October 23, 2013

SUM OF ALL 3-DIGITS

//WAP TO INPUT A 3-DIGIT NO. AND CALCULATE SUM OF ALL DIGITS.


#include<stdio.h>
#include<conio.h>
void main()
{
      int num;
      int r,sum=0;
      clrscr();

      printf("\nEnter a 3-digit no.=");
      scanf("%d",&num);
      r=num%10;
      sum=sum+r;
      num=num/10;
      r=num%10;
      sum=sum+r;
      num=num/10;
      r=num%10;
      sum=sum+r;
      printf("\n\nThe sum of all digits of entered no.=%d",sum);
      getch();


}