Friday, April 16, 2010

Use of switch Statements

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int speed;
 cout<<"1. for 0-20\n";
 cout<<"2. for 20-60\n";
 cout<<"3. for 60-100\n";
 cout<<"4. for >100\n";
 cout<<"enter speed\n";
 cin>>speed;
 switch(speed)
 {
 case 1:
 cout<<"slow speed\n";
 break;
 case 2:
 cout<<"average\n";
 break;
 case 3:
 cout<<"fast\n";
 break;
 case 4:
 cout<<"danger\n";
 break;
 default:
 cout<<"invalid choice";
 }
 getch();
 }

Swaping of numbers with using 3rd num

#include<iostream.h>
#include<conio.h>
 void main()
 {
 int a,b;
 clrscr();
 cout<<"Enter 1st no.\n";
 cin>>a;
 cout<<"Enter 2nd no.\n";
 cin>>b;
 cout<<"Before swapping\n";
 cout<<"a="<<a;
 cout<<"b="<<b;
 cout<<"\nAfter swapping\n";
 a=a+b;
 b=a-b;
 a=a-b;
 cout<<"a="<<a;
 cout<<"b="<<b;
 getch();
 }

Swaping of numbers

#include<iostream.h>
#include<conio.h>
 void main()
 {
 int a,b,c;
 clrscr();
 cout<<"Enter 1st no.\n";
 cin>>a;
 cout<<"Enter 2nd no.\n";
 cin>>b;
 cout<<"Before swapping\n";
 cout<<"a=\n"<<a;
 cout<<"b=\n"<<b;
 cout<<"After swapping\n";
 a=b;
 b=c;
 c=a;
 cout<<"a="<<a;
 cout<<"b="<<b;
 getch();
 }

Printing Table from Starting table num upto number

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int s,e,l,t,n;
 cout<<"\nenter starting number";
 cin>>s;
 cout<<"\nenter end number";
 cin>>e;
 for(n=s;n<=e;n++)
 {
 for(l=1;l<=10;l++)
 {
 t=n*l;
 cout<<"\n"<<l;
 cout<<"*";
 cout<<n;
 cout<<"=";
 cout<<t;
 }
 }
 getch();
 }

Is statement

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int l,a,p=0,n=0,z=0;
 for(l=1;l<=10;l++)
 {
 cout<<"\nenter nos.";
 cin>>a;
 if(a>0)
 p=p+1;
 else if(a<0)
 n=n+1;
 else
 z=z+1;
 }
 cout<<"\npositive no's are="<<p;
 cout<<"\nnegative no's are="<<n;
 cout<<"\nzero no's are="<<z;
 getch();
 }

Nested Loop

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int a,b;
 for(a=1;a<=5;a++)
 {
 for(b=1;b<=a;b++)
 {
 cout<<a;
 }
 cout<<"\n";
 }
 getch();
 }

Leap Year using If

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int y;
 cout<<"enter any year";
 cin>>y;
 if(y%4==0)
 cout<<"leap year";
 else
 cout<<"not";
 getch();
 }

Use of Nested If

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int p;
 cout<<"enter percentage\n";
 cin>>p;
 if(p>=90)
 cout<<"S grade\n";
 else if(p>=80)
 cout<<"A grade\n";
 else if(p>=70)
 cout<<"B grade\n";
 else
 cout<<"C grade";
 getch();
 }

Factorial of a Number

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int n,l,f;
 f=1;
 cout<<"\nenter no.";
 cin>>n;
 for(l=1;l<=n;l++)
 {
 f=f*l;
 }
 cout<<"\nfactorial no is"<<f;
 getch();
 }

Sum of even and odd numbers from 10 numbers

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int n,l,e,o;
 e=0;
 o=0;
 cout<<"enter 10 nos.";
 for(l=1;l<=10;l++)
 {
 cin>>n;
 if(n%2==0)
 e=e+n;
 else
 o=o+n;
 }
 cout<<"sum of even nos."<<e;
 cout<<"sum of odd nos."<<o;
 getch();
 }

Count Even/Odd Numbers

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int l,n,e,o;
 e=0;
 o=0;
 cout<<"enter 10 nos.";
 for(l=1;l<=10;l++)
 {
 cin>>n;
 if(n%2==0)
 e=e+1;
 else
 o=o+1;
 }
 cout<<"\neven nos. are"<<e;
 cout<<"\nodd nos. are"<<o;
 getch();
 }

Biggest out of 10

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int num,n,a=0;
 cout<<"enter 10 nos.";
 for(num=1;num<=10;num++)
 {
 cin>>n;
 if(a<n)
 {
 a=n;
 }
 }
 cout<<"biggest no. is"<<a;
 getch();
 }

Biggest Out of 3

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int a,b,c;
 cout<<"enter 1st no\n";
 cin>>a;
 cout<<"enter 2nd no\n";
 cin>>b;
 cout<<"enter 3rd no\n";
 cin>>c;
 if(a>b)
 if(a>c)
 cout<<"a is bigger\n";
 else
 cout<<"c is bigger\n";
 else if(b>c)
 cout<<"b is bigger\n";
 else
 cout<<"c is bigger";
 getch();
 }

Bigger Number

void main()
{
clrscr();
int a,b;
cout<<"enter 1st no\n"; cin>>a;
cout<<"enter 2nd no\n"; cin>>b;
if(a>b)
cout<<"a is bigger\n"; if(b>a)
cout<<"b is bigger";
getch();
}

First 20 Odd Numbers

#include<iostream.h>
#include<conio.h>
 void main()
 {
 clrscr();
 int l;
 cout<<"1st 20 odd nos.";
 for(l=1;l<=40;l++)
 if(l%2!=0)
 cout<<"\n"<<l;
 getch();
 }