SOME BASIC PROGRAMS

A Special thanks to Menace, aka Soumitra, for getting the programs.


CONTENTS:

  1. WAP that converts alphabet to its ASCII value – ASCII
  2. WAP that distributes players in a team – TEAMS
  3. WAP to find salary of salesman – SALES
  4. WAP for electricity bill – ELECTRIC
  5. WAP for temperature conversion – TEMPMENU
  6. WAP to find area of circle, rectangle and triangle – AREASWIT
  7. WAP related to a bank – BANK
  8. Fibonacci series – FIBONACI
  9. Palindrome number – PALINDRM
  10. WAP to count number of vowels – VOWEL
  11. WAP that replaces spaces with ‘$’ – CHANGE
  12. String palindrome – PALIN
  13. Bubble search – BUBBLES
  14. Deletion of an element in Array– DELETN
  15. Insertion of an element in Array– INSRTN
  16. Menu program for matrix operations – MENU
  17. WAP to calculate power using functions – POWER1
  18. WAP to display middle row and column – MIDDLERC



1. WAP that converts alphabet to its ASCII value

#include <iostream.h>
void main()
{
char ch ;
int a,b,c ;
cout<<"Enter alphabet"<<'\n' ;
cin>>ch ;
a=ch ;
b=a+1 ;
c=a+2 ;
cout<<"ASCII value"<<char(ch)<<a<<'\n' ;
cout<<"ASCII value"<<char(ch+1)<<b<<'\n' ;
cout<<"ASCII value"<<char(ch+2)<<c<<'\n' ;
}




2. WAP that distributes players in a team


#include <iostream.h>
void main()
{
int p,t,l,a ;
cout<<"Enter total number of players:"<<'\n' ;
cin>>t ;
l=t%5 ;
a=t-l ;
p=a/5 ;
cout<<"Total teams:"<<p<<'\n' ;
cout<<"Number of players left"<<l<<'\n' ;
}

3. WAP to find salary of salesman


#include <iostream.h>
#include <conio.h>
void main()
{
float sales,c ;
cout<<"Enter daily sales"<<'\n' ;
cin>>sales ;
c=(sales>5000)?500+200:100+200 ;
cout<<"Total payable amount is:"<<'\n'<<c<<'\n' ;
getch() ;
}


4. WAP for electricity bill

#include <iostream.h>
#include <conio.h>
void main()
{
float units,charge ;
char name[20] ;
cout<<"Please enter Name and Units Consumed"<<'\n' ;
cin>>name>>units ;
if (units<=100)
{
charge=((units*0.4)+50) ;
}
else if (units>100 && units<=300)
{
charge=(((units-100)*0.5)+90) ;
}
else
{
charge=(((units-300)*0.6)+140) ;
}
cout<<"Bill for"<<name<<" is Rs. "<<charge<<'\n' ;
getch() ;
}



5. WAP for temperature conversion

#include <iostream.h>
#include <conio.h>
void main()
{
float c,f,c1,f1 ;
int choice ;
cout<<"Temperature Conversion Menu"<<'\n'<<"1. Celsius to Fahreheit"<<'\n'<<"2. Fahreheit to Celsius"<<'\n'<<'\n'<<"Please Enter Your Choice (1 or 2)"<<'\n' ;
cin>>choice ;
if (choice==1)
{
cout<<"Enter Tempertaure in Celsius"<<'\n' ;
cin>>c ;
f=((9*c)/5)+32 ;
cout<<"Temperature is"<<f<<"fahreheit"<<'\n' ;
}
else
{
cout<<"Enter temperature in Fahreheit"<<'\n' ;
cin>>f1 ;
c1=((f1-32)*5)/(9) ;
cout<<"Temperature is"<<c1<<"degree celsius"<<'\n' ;
}
getch() ;
}


6. WAP to find area of circle, rectangle and triangle

#include <iostream.h>
void main()
{
float r,l,b,ba,h ;
const float pi=3.14 ;
int choice ;
cout<<'\n'<<"What do you want to calculate"<<'\n'<<"1. Area of Circle"<<'\n'<<"2. Area of Rectangle"<<'\n'<<"3. Area of Right Triangle"<<'\n' ;
cout<<'\n'<<'\n'<<
"Please enter your choice"<<'\n' ;
cin>>choice ;
switch (choice)
{
case 1 : cout<<'\n'<<"Please enter Radius:"<<'\n' ;
cin>>r ;
cout<<'\n'<<"Area is "<<pi*r*r<<'\n' ;
break ;
case 2 : cout<<'\n'<<"Please enter lenght and breadht"<<'\n' ;
cin>>l>>b ;
cout<<'\n'<<"Area is "<<l*b<<'\n' ;
break ;
case 3 : cout<<'\n'<<"Please enter base and height"<<'\n' ;
cin>>ba>>h ;
cout<<'\n'<<"Area is "<<(ba*h)/2<<'\n' ;
break ;
default:cout<<'\n'<<"Invalid Choice"<<'\n' ;
}
}


7. WAP related to a bank

#include <iostream.h>
#include <math.h>
void main()
{
float p,r,t,am,a,b ;
cout<<"Enter deposit and time:"<<'\n' ;
cin>>p>>t ;


if (p>0&&p<2000&&t>=2&&t<5)
  {  r=5 ;
     a=(100+r)/100 ;
     b=pow(a,t) ;
     am=p*b ;
     cout<<"Amount in your account is "<<am<<'\n' ;
  }

else if (p>=2000&&p<6000&&t>=2&&t<5)
  {  r=7 ;
     a=(100+r)/100 ;
     b=pow(a,t) ;
     am=b*p ;
     cout<<"Amount in your account is "<<am<<'\n' ;
  }

else if (p>=6000&&t>=1&&t<5)
  {  r=8 ;
     a=(100+r)/100 ;
     b=pow(a,t) ;
     am=p*b ;
     cout<<"Amount in your account is "<<am<<'\n' ;
  }

else if (t>=5&&p>0)
  {  r=10 ;
     a=(100+r)/100 ;
     b=pow(a,t) ;
     am=p*b ;
     cout<<"Amount in your account is "<<am<<'\n' ;
  }

else
  {  r=3 ;
     a=(100+r)/100 ;
     b=pow(a,t) ;
     am=p*b ;
     cout<<"Amount in your account is "<<am<<'\n' ;
  }
}





8. Fibonacci series

#include <iostream.h>
void main()
{
int a,b,c,n ;
cout<<"How many elements?"<<'\n' ;
cin>>n  ;
a=0 ;
b=1 ;
cout<<"FIBONACCI SERIES"<<'\n' ;
cout<<a<<'\t'<<b ;
for (int i =2;i<n;++i)
 {
    c=a+b ;
    cout<<'\t'<<c ;
    a=b ;
    b=c ;
 }
}



9. Palindrome number


#include <iostream.h>
#include <conio.h>
void main()
{
long int num;
int n,rev=0,digit ;
cout<<"Please enter the number :"<<'\n' ;
cin>>num ;
n=num ;
do
  {
     digit=num%10 ;
     rev=(rev*10)+digit ;
     num=num/10 ;
  }
while (num!=0) ;
if (n==rev)
  cout<<"The number is a PALINDROME"<<'\n' ;
else
  cout<<"The number is not a PALINDROME"<<'\n' ;
getch() ;
}


10. WAP to count number of vowels

#include <iostream.h>
#include <stdio.h>
void main()
{
char string[35] ;
int vowel=0 ;
cout<<"Enter string"<<'\n' ;
gets(string) ;
for (int i=0;string[i]!='\0';++i)
{
 if (string[i]=='a' || string[i]=='A' || string[i]=='e' || string[i]=='E' || string[i]=='i' || string[i]=='I' || string[i]=='o' || string[i]=='O' || string[i]=='u' || string[i]=='U')
 ++vowel ;
}
cout<<"Total number of vowels are "<<vowel ;
}



11. WAP that replaces spaces with ‘$’ 

#include <iostream.h>
#include <stdio.h>
void main()
{
char string[35] ;
cout<<"Enter string"<<'\n' ;
gets(string) ;
for (int i=0;string[i]!='\0';++i)
{
if (string[i]==' ')
string[i]='$' ;
}
puts(string) ;
}





12. String palindrome

#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char str[20] ;
int len,i,j,flag=0 ;
cout<<"Enter string"<<'\n' ;
cin.getline(str,20) ;
len=strlen(str) ;
for (i=0,j=len-1;i<len/2;++i,--j)
  {
if (str[i]!=str[j])
{
 flag=1 ;
 break ;
}
  }
if (flag==0)
cout<<"The string is a palindrome."<<endl ;
else
cout<<"The string is not a palindrome."<<endl ;
getch() ;
}





13. Bubble search

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr() ;
int a[10],i,j,temp ;
cout<<"Enter 10 values"<<'\n' ;
for (i=0;i<10;++i)
  cin>>a[i] ;
for (i=0;i<10;++i)
  {
 for (j=0;j<(10-1)-i;++j)
 {
 if (a[j]>a[j+1])
{
temp=a[j] ;
a[j]=a[j+1] ;
a[j+1]=temp ;
 }
}
}
cout<<"The sorted list in ascending order is : " ;
for (i=0;i<10;++i)
  cout<<'\n'<<a[i] ;
getch() ;
}



14. Deletion of an element from an Array

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr() ;
int a[10],i,pos=0,b,s,flag=0 ;
cout<<"Enter lenght of list"<<'\n'<<"Please enter less than 10"<<endl ;
cin>>s ;
if (s==0)
cout<<"Empty list"<<endl<<"Nothing to DELETE"<<endl ;
else
  {
cout<<"Enter numbers:"<<endl ;
for (i=0;i<s;++i)
cin>>a[i] ;
cout<<"Enter number to be deleted"<<endl ;
cin>>b ;
for (i=0;i<s;++i)
{
 if (a[i]==b)
{
pos=i ;
cout<<"The number to be deleted is at "<<pos<<endl ;
flag=1 ;
break ;
 }
}
  if (flag==0)
  cout<<"Number not found"<<endl ;
  else
{
 cout<<"The new list is: "<<endl ;
 for (i=pos;i<s;++i)
 {
a[i]=a[i+1] ;
 }
for(i=0;i<s-1;++i)
cout<<a[i]<<endl ;
}
  }
getch() ;
}




15.  Insertion of an element into an Array

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr() ;
int n[10],s,a,pos ;
cout<<"Enter lenght of list"<<'\n'<<"Enter less than 10"<<'\n' ;
cin>>s ;
if (s>=10)
cout<<"No space"<<endl ;
else
{
cout<<"Enter the numbers"<<endl ;
for(int i=0; i<s; i++)
cin>>n[i] ;
cout<<"Enter the number to insert"<<endl ;
cin>>a ;
if(a>n[s-1])
pos=s ;
else if(a<n[0])
pos=0 ;
else
 {
for(i=0; i<s-1; i++)
{
if( (n[i]<=a) && (n[i+1]>=a) )
{
pos=i+1 ;
break ;
}
}
 }
 for(i=(s-1); i>=pos; i--)
 {
 n[i+1]=n[i];
 }
 n[pos]=a ;
 cout<<"The new array is"<<endl ;
 for(i=0; i<s+1; i++)
 {
 cout<<n[i]<<'\n' ;
 }
}
getch();
}


16. Menu program for matrix operations

#include <iostream.h>
#include <conio.h>
#include <process.h>
void main()
{
clrscr() ;
char ch,ch1,ch2 ;
int i,j,m,n,p,q,k,a[10][10],b[10][10],c[10][10] ;
do
  {
cout<<"Enter size of 1st matrix"<<'\n' ;
cin>>m>>n ;
cout<<"Enter the elements of 1st matrix"<<'\n' ;
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
cin>>a[i][j] ;
}
cout<<'\n'<<"Enter size of 2nd matrix"<<'\n' ;
cin>>p>>q ;
cout<<"Enter the elements of 2nd matrix"<<'\n' ;
for (i=0;i<p;++i)
{
for (j=0;j<q;++j)
cin>>b[i][j] ;
}
cout<<'\t'<<'\t'<<'\t'<<"MENU"<<'\n' ;
cout<<"The following operations can be done on the above matrices"<<'\n' ;
cout<<"1.> ADDITION"<<'\n'<<"2.> SUBTRACTION"<<'\n'<<"3.> MULTIPLICATION"<<'\n'<<"4.> Checking for EQUALITY"<<'\n'<<"5.> TRANSPOSE"<<'\n'<<"6.> EXIT"<<'\n' ;
cout<<"Enter your choice"<<'\n' ;
cin>>ch ;
switch(ch)
{
case '1': if ((m!=p) || (n!=q))
cout<<"Addition not possible"<<'\n' ;
else
{
 for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
c[i][j]=a[i][j]+b[i][j] ;
}
 cout<<"The new matrix is "<<'\n' ;
 for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
cout<<c[i][j]<<'\t' ;
cout<<'\n' ;
}
}
break ;
case '2': if ((m!=p) || (n!=q))
cout<<"Subtraction not possible"<<'\n' ;
else
{
 for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
c[i][j]=a[i][j]-b[i][j] ;
}
 cout<<"The new matrix is "<<'\n' ;
 for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
cout<<c[i][j]<<'\t' ;
cout<<'\n' ;
}
}
break ;
case '3': if (n!=p)
cout<<"Multiplication not possible"<<'\n' ;
else
{
 for (i=0;i<m;++i)
{
for (j=0;j<q;++j)
 {
c[i][j]=0 ;
for (k=0;k<n;++k)
c[i][j]+=a[i][k]*b[k][j] ;
 }
}
 cout<<"The new matrix is "<<'\n' ;
 for (i=0;i<m;++i)
{
for (j=0;j<q;++j)
cout<<c[i][j]<<'\t' ;
cout<<'\n' ;
}
}
break ;
case '4': if ((m!=p) || (n!=q))
cout<<"Matrices are not equal"<<'\n' ;
else
{
 for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
 {
if (a[i][j]==b[i][j])
{
 cout<<"Matrices are equal"<<'\n' ;
 continue ;
}
else
cout<<"Matrices are not equal"<<'\n' ;
 }
}
}
break ;
case '5': cout<<'\n'<<"Please choose any one matrix"<<'\n' ;
cout<<"1.> A"<<'\n'<<"2.> B"<<'\n' ;
cin>>ch1 ;
if (ch1=='1')
{
 c[10][10]=0 ;
 cout<<"The matrix you have chosen is "<<'\n' ;
 for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
cout<<a[i][j]<<'\t' ;
cout<<'\n' ;
}
 for (i=0;i<n;++i)
{
for (j=0;j<m;++j)
c[i][j]=a[j][i] ;
}
 cout<<"The new matrix is "<<'\n' ;
 for (i=0;i<n;++i)
{
for (j=0;j<m;++j)
cout<<c[i][j]<<'\t' ;
cout<<'\n' ;
}
}
else
{
 c[10][10]=0 ;
 cout<<"The matrix you have chosen is "<<'\n' ;
 for (i=0;i<p;++i)
{
for (j=0;j<q;++j)
cout<<b[i][j]<<'\t' ;
cout<<'\n' ;
}
 for (i=0;i<q;++i)
{
for (j=0;j<p;++j)
c[i][j]=b[j][i] ;
}
 cout<<"The new matrix is "<<'\n' ;
 for (i=0;i<q;++i)
{
for (j=0;j<p;++j)
cout<<c[i][j]<<'\t' ;
cout<<'\n' ;
}
}
break ;
case '6': cout<<"You have decided to exit the program!!!"<<'\n' ;
getch() ;
exit(0) ;
break ;
default : cout<<"You have entered a wrong choice"<<'\n' ;
cout<<"Kindly look at what you are trying to do!!"<<'\n' ;
}
cout<<'\n'<<"Do you want to enter again?"<<'\n' ;
cin>>ch2 ;
if (ch2=='y' || ch2=='Y')
cout<<"Follow the steps again"<<'\n' ;
  }
while (ch2=='y' || ch2=='Y') ;
getch() ;
}


17. WAP to calculate power using functions


#include <iostream.h>
long power1(int,int) ;
void main()
{
int m,n,ans ;
cout<<"Enter base"<<'\n' ;
cin>>m ;
cout<<"Enter power"<<'\n' ;
cin>>n ;
ans=power1(m,n) ;
cout<<"The answer is  "<<ans<<'\n' ;
}
long power1(int m, int n)
{
int i,ans ;
if (m==0)
ans=0 ;
else if (n==0)
ans=1 ;
else
  {
for (i=0;i<n-2;++i)
m=m*m ;
ans=m ;
  }
return(ans) ;
}



18. WAP to display middle row and column


#include <iostream.h>
#include <conio.h>
void main()
{
clrscr() ;
int a[10][10],i,j,m,n,mr[10],mc[10] ;
cout<<"Enter size of array"<<'\n' ;
cin>>m>>n ;
if ((m%2==0) || (n%2==0))
cout<<"Further not possible"<<'\n' ;
else
  {
if (m!=n)
cout<<"Further not possible"<<'\n' ;
else
{
 cout<<"Enter elements"<<'\n' ;
 for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
cin>>a[i][j] ;
}
 cout<<"The array formed is"<<'\n' ;
 for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
cout<<a[i][j]<<'\t' ;
cout<<'\n' ;
}
 i=(m-1)/2 ;
 for (j=0;j<n;++j)
 mr[j]=a[i][j] ;
 j=(n-1)/2 ;
 for (i=0;i<m;++i)
 mc[i]=a[i][j] ;
 cout<<"The middle row is "<<'\n' ;
 for (j=0;j<n;++j)
 cout<<mr[j]<<'\t' ;
 cout<<'\n'<<"The middle column is "<<'\n' ;
 for (i=0;i<m;++i)
 cout<<mc[i]<<'\t' ;
}
  }
getch() ;
}



ċ
AREASWIT.CPP
(1k)
Neil Mathew,
1 Jul 2010, 08:37
ċ
ASCII.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:37
ċ
BANK.CPP
(1k)
Neil Mathew,
1 Jul 2010, 08:37
ċ
BUBBLES.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:37
ċ
CHANGE.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:37
ċ
DELETN.CPP
(1k)
Neil Mathew,
1 Jul 2010, 08:37
ċ
ELECTRIC.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:37
ċ
FIBONACI.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:37
ċ
INSRTN.CPP
(1k)
Neil Mathew,
1 Jul 2010, 08:38
ċ
MENU.CPP
(4k)
Neil Mathew,
1 Jul 2010, 08:38
ċ
MIDDLERC.CPP
(1k)
Neil Mathew,
1 Jul 2010, 08:38
ċ
PALIN.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:38
ċ
PALINDRM.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:38
ċ
POWER1.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:38
ċ
SALES.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:38
ċ
TEAMS.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:38
ċ
TEMPMENU.CPP
(1k)
Neil Mathew,
1 Jul 2010, 08:38
ċ
VOWEL.CPP
(0k)
Neil Mathew,
1 Jul 2010, 08:38