Sie sind auf Seite 1von 85

1) WAP to accept 3 digits (0-9) and print all possible combinations from these

digits.
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,i,j,k;

cout<<"Enter the 3 digits"<<endl;
cin>>a>>b>>c;
int A[]={a,b,c};
cout<<"The possible numbers are"<<endl;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
for(k=0;k<=2;k++)
{
if(A[i]!=A[j]&&A[j]!=A[k]&&A[i]!=A[k])

cout<<A[i]<<A[j]<<A[k]<<endl;
}
}
}




getch();
}



Enter the 3 digits
4
3
7
The possible numbers are
437
473
347
374
743
734


2) WAF that takes the time as 3 integer arguments (hours, minutes, seconds).
Since the clock struck 12:00. Use this function to calculate the amount of time in
seconds between two times both of which are in I 12 hour cycle of the clock.
Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>
struct times
{
int h;
int m;
int s;
} t1,t2;

void main()
{
clrscr();
int s,s1,s2;
cout<<"Enter the first time"<<endl;
cin>>t1.h>>t1.m>>t1.s;
cout<<"Enter the second time"<<endl;
cin>>t2.h>>t2.m>>t2.s;

s1=t1.h*3600+t1.m*60+t1.s;
s2=t2.h*3600+t2.m*60+t2.s;
s=abs(s2-s1);

cout<<"The difference is "<<s<<endl;

getch();
}

Output-
Enter the first time
3
45
50
Enter the second time
3
45
55
The difference is 5






3.WAP to accept a date and check for validity
#include<iostream.h>
#include<conio.h>
void main()
{
int dd, mm, yy;
clrscr();
cout << "Enter the date";
cin >> dd;
cin >> mm >> yy;
if ( mm==1||mm == 3||mm == 5 || mm == 7 || mm == 10 || mm ==12)
{
if (dd<=31 && dd >0)
cout<<"Valid date ";
else
cout<< "Invalid date ";
}
else
if ( mm==4 || mm == 6 || mm == 9 || mm == 11)
{
if (dd<=30 && dd >0)
cout<<"Valid date ";
else
cout<< "Invalid date ";
}
else
if (mm == 2)
{
if ((yy%100== 0)&&(yy%400 == 0)||(!yy%100 == 0)&&(yy%4 == 0))
if (dd>0 && dd<=29)
cout<<"Valid date";
else
cout<<"Invalid date ";
else
if (dd>0 && dd<= 28)
cout<<"Valid date";
else
cout<<"Invalid date";
}
else
cout<<"Invalid date";
}

Output
Enter the date
23
03
1998
Valid date

4. Wap to find sum of series 1+1/2!+1/3!..upto N
#include <iostream.h>
#include <conio.h>
// Function to calculate the series
// 1 + 1/2! + 1/3! + 1/4! +...+ 1/n!
//Function to calculate the factorial
int fact(int num)
{
int prod = 1, i = 1;
while(i<=num)
{
prod = prod*i;
i++;
}
return(prod);
}

float sumsquare(int n)
{
int i = 1, fac;
float term, sum = 0;
while(i<=n)
{
fac = fact(i);
term = float(1/float(fac));
sum = sum + term;
i++;
}
cout << "\n\n\tThe sum is " << sum;
return(sum);
}
void main()
{
clrscr();
int n;
float nsum = 0;
cout << "Enter the value of N : ";
cin >> n;
nsum = sumsquare(n);
cout << "\nThe sum up to N is : " << nsum;
}
Output
Enter value of n
2
The sum is
1.25


5.Wap to find sum of fibonacci series by recursion
#include<iostream.h>
#include<conio.h>
int fibonacci(int n);
void main()
{clrscr();
int n;
cout<<"\n\n Enter the number of terms upto which you want the sum of fibonnaci series : ";
cin>>n;
cout<<"\n\nThe fibonacci series generated is : \n\n";
cout<<"1 1 ";
int s=fibonacci(n);
cout<<"\n\nThe sum of fibonacci series for "<<n<<" terms = "<<s;
getch();
}
int first=1;
int second=1;
int third;
int i=2;
int sum=0;
int fibonacci(int n)
{ if(n==1)
sum=1;
else if(n==2)
sum=2;
else if(n>1 && i!=n)
{
third=first+second;
cout<<third<< ;
if(i==2)
sum+=first+second+third;
else
sum+=third;
first=second;
second=third;
++i;
fibonacci(n); }
return sum;
}

Output
Enter the number of terms upto which you want the sum of fibonacci series
3
The sum of fibonacci series is 4






6. From a 2D array A[4][4], WAP to prepare a 1D array B[16] that will have all
the elements of A stored in the row major form.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
clrscr();
char a[4][4],b[16];
int i,j,k,m,n;
cout<<"enter order"<<endl;
cin>>m>>n;
cout<<"Enter the array"<<endl;
cout<<endl;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
cout<<endl;
cout<<"You entered"<<endl;
for(i=1;i<=m;i++)
{
endl;
for(j=1;j<=n;j++)
{
cout<<a[i][j];
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
b[k]=a[i][j];
k++;
}
}
cout<<"the array is"<<endl;
cout<<endl;
for(i=1;i<=m*n;i++)
{
cout<<b[i];
}
getch();
}

Output
enter order
4
4
Enter the array
1
2
3
4
5
6
7
8
9
0
11
12
The array is
12345678901112






7.Write a program that displays area of square, rectangle and triangle using
values given by user beforehand.
#include<iostream.h>
#include<conio.h>
#include<math.h>
int area(int);
int area(int,int);
int area(int,int,int);
void main()
{
clrscr();
int a=2,b=3,c=4;
cout<<"Area of square="<<area(a)<<endl;
cout<<"Area of rectangle="<<area(a,b)<<endl;
cout<<"Area of triangle="<<area(a,b,c)<<endl;
getch();
}
int area(int a)
{
return(a*a);
}
int area(int a,int b)
{
return(a*b);
}
int area(int a,int b,int c)
{
int s=(a+b+c)/2;
return(sqrt(s*(s-a)*(s-b)*s-c));
}
Output-
Area of square=4
Area of rectangle=6
Area of triangle=5
8. Write a program that accepts a number in decimal form and displays binary
form of it.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
int r=1;
cout<<"Enter decimal no"<<endl;
cin>>n;
while(n>0)
{
int d=n%2;
r=r*10+d;
n=n/2;
}
r=r/10;
cout<<"Binary="<<r;
getch();
}
Output-
Enter decimal no
2
Binary=10
8.Write a program to accept radius of circle and display area and circumference.
#include<iostream.h>
#include<conio.h>
class circle
{
int r;
public:
float ar,ci;
void input(int radius)
{
r=radius;
}
void calc();
float area();
float circum();
};
void circle ::calc()
{
ar=3.14*r*r;
ci=2*3.14*r;
}
float circle ::area()
{
return(ar);
}
float circle ::circum()
{
return(ci);
}
void main()
{
clrscr();
circle c;
int radius;
cout<<"Enter radius"<<endl;
cin>>radius;
c.input(radius);
c.calc();
cout<<"Area="<<c.area();
cout<<"Circumference="<<c.circum();
getch();
}
Output-
Enter radius
1
Area=3.14
Circumference=6.28
9.Write a program to find hcf and gcd of two numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
int hcf,lcm;
cout<<"Enter 2 numbers"<<endl;
cin>>a>>b;
for(int i=1;i<=a;i++)
{
if(a%i==0 && b%i==0)
hcf=i;
}
for(int j=a;j<=a*b;j++)
{
if(j%a==0 && j%b==0)
{
lcm=j;
break;
}
}
cout<<"HCF="<<hcf<<endl;
cout<<"LCM="<<lcm;
getch();
}
Output-
Enter 2 numbers
8
12
HCF=4
LCM=24
10..Write a program to accept a string and display number of characters in it.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char str[100];
cout<<"Enter string"<<endl;
cin.getline(str,100);
for(int i=0;i<100;i++)
{
if(str[i]=='\0')
break;
}
cout<<"No of characters="<<i<<endl;
getch();
}
Output-
Enter string
Computers
No of characters=9

11.Write a program to accept a number and display prime numbers up to it.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,c;
cout<<"Enter n greater than 5"<<endl;
cin>>n;
while(n<=5)
{
cout<<"Greater than 5"<<endl;
cin>>n;
}
for(int i=2;i<=n;i++)
{
c=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
cout<<i<<endl;
}
getch();
}
Output-
Enter n greater than 5
6
2
3
5

12.Write a program to accept two numbers and swap them.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<"Enter numbers to be swapped"<<endl;
cin>>a>>b;
cout<<"Original numbers"<<a<<","<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"Swapped numbers="<<a<<","<<b<<endl;
getch();
}
Output-
Enter numbers to be swapped
2
3
Original numbers-2,3
Swapped numbers-3,2











13.Write a program to demonstrate function overloading.
#include<iostream.h>
#include<conio.h>
#include<math.h>
int vol(int);
int vol(int,int);
int vol(int,int,int);
void main()
{
clrscr();
int s=2,r=3,h=4;
cout<<"Volume of cube="<<vol(s)<<endl;
cout<<"Volume of cylinder="<<vol(r,h)<<endl;
cout<<"Volume of cuboid="<<vol(s,r,h)<<endl;
getch();
}
int vol(int s)
{
return(s*s*s);
}
int vol(int r,int h)
{
return(3.14*r*r*h);
}
int vol(int s,int r,int h)
{
return(s*r*h);
}
Output-
Volume of cube=8
Volume of cylinder=113
Volume of cuboid=2

14. WAP to find a^b using function overloading
#include<iostream.h>
#include<math.h>
#include<conio.h>
int calc(int a, int b)
{
return pow(a,b);
}
float calc(float a, float b)
{
return pow(a,b);
}
float calc(int a, float b)
{
return pow(a,b);
}
float calc(float a, int b)
{
return pow(a,b);
}

void main()
{clrscr();
int P,Q;
float R,S;
cout<<"Enter int P : "; cin>>P;
cout<<"Enter int Q : "; cin>>Q;
cout<<"Enter real R : "; cin>>R;
cout<<"Enter real S : "; cin>>S;
cout<<P<<"^"<<Q<<" = "<<pow(P,Q)<<"\n\n";
cout<<P<<"^"<<R<<" = "<<pow(P,R)<<"\n\n";
cout<<R<<"^"<<Q<<" = "<<pow(R,Q)<<"\n\n";
cout<<R<<"^"<<S<<" = "<<pow(R,S);
getch();
}


Output:
Enter int P : 2
Enter int Q : 5
Enter real R : 2.2
Enter real S : 5.1
2^5 = 32
2^2.2 = 4.594794
2.2^5 = 51.536326
2.2^5.1 = 55.764227
15.Write a program class account with subclasses savings and current.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
class account
{
public:
char name[25];
int accno;
int opbal;
void read()
{
cout<<"Enter name of customer"<<endl;
gets(name);
cout<<"Enter account no"<<endl;
cin>>accno;
cout<<"Enter opening balance"<<endl;
cin>>opbal;
}
void dep()
{
int deposit;
cout<<"Enter money to be deposited"<<endl;
cin>>deposit;
opbal+=deposit;
}
void disp()
{
cout<<"Name of customer-";
puts(name);
cout<<"Account number-"<<accno<<endl;
cout<<"Balance="<<opbal<<endl;
}
}acc;
class savings:public account
{
public:
int take;
void withdraw()
{
cout<<"Enter ammount you wish to withdraw"<<endl;
cin>>take;
opbal-=take;
}
void ci()
{
take=take*pow((1+0.04),3)-take;
cout<<"Customer must pay interest of "<<take<<endl;
}
}sav;

class current:public account
{
public:
int check;
void checkbook()
{
cout<<"Enter ammount you wish to withdraw"<<endl;
cin>>check;
opbal-=check;
}
void minbal()
{
if(opbal<100)
cout<<"You must pay penalty of "<<(100-opbal)*0.04<<endl;
}
}cur;
void main()
{
clrscr();
int ch;
cout<<"Enter 1 for savings account, 2 for current account"<<endl;
cin>>ch;
if(ch==1)
{
cout<<"Enter customer details"<<endl;
sav.read();
cout<<"Enter 1 to deposit, 2 to withdraw"<<endl;
cin>>ch;
if(ch==1)
sav.dep();
else
{
sav.withdraw();
}
cout<<"Details-"<<endl;
sav.disp();
if(ch==2)
sav.ci();
}
else
{
cout<<"Enter customer details"<<endl;
cur.read();
cout<<"Enter 1 to deposit and 2 to withdraw"<<endl;
cin>>ch;
if(ch==1)
cur.dep();
else
{
cur.checkbook();
}
cout<<"Details-"<<endl;
cur.disp();
if(ch==2)
cur.minbal();
}
getch();
}
Output-
Enter 1 for savings account, 2 for current account
1
Enter customer details
Enter name of customer
Rahul
Enter account no
123
Enter opening balance
1000
Enter 1 to deposit, 2 to withdraw
1
Enter money to be deposited
100
Details-
Name of customer-Rahul
Account number-123
Balance-110
16.Write a program class bank with two subclasses book and audio cassette.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class publication
{
public:
char title[25];
float price;
void getdata()
{
cout<<"Enter title"<<endl;
gets(title);
cout<<"Enter price"<<endl;
cin>>price;
}
void putdata()
{
cout<<"Title-";
puts(title);
cout<<"Price-"<<price<<endl;
}
}pub;
class book:public publication
{
public:
publication cheat;
int count;
void getdata()
{
cout<<"Enter count"<<endl;
cin>>count;
}
void putdata()
{
cout<<"Count-"<<count<<endl;
}
}bo;
class audio:public publication
{
public:
publication cheat;
float tape;
void getdata()
{
cout<<"Enter running time of tape in mins"<<endl;
cin>>tape;
}
void putdata()
{
cout<<"Time in minutes-"<<tape<<endl;
}
}aud;
void main()
{
clrscr();
pub.getdata();
bo.getdata();
aud.getdata();

pub.putdata();
bo.putdata();
aud.putdata();
getch();
}
Output-
Enter title
Computer Science
Enter price
200
Enter count
300
Enter running time of tape in minutes
50
Title-Computer Science
Price-200
Count-300
Time in minutes-50
17.Write a program class employee with subclass manager.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class employee
{
public:
int empno;
char name[25];
char add[50];
char dept[25];
void input()
{
cout<<"Enter employee no"<<endl;
cin>>empno;
cout<<"Enter employee name"<<endl;
gets(name);
cout<<"Enter employee address"<<endl;
gets(add);
cout<<"Enter employee department"<<endl;
gets(dept);
}
void disp()
{
cout<<"Employee number-"<<empno<<endl;
cout<<"Employee name-";
puts(name);
cout<<"Employee address-";
puts(add);
cout<<"Employee department-";
puts(dept);
}
}emp;
class manager:public employee
{
public:
int no;
void ino()
{
cout<<"Enter no of employees"<<endl;
cin>>no;
}
void ono()
{
cout<<"Number of employees-"<<no<<endl;
}
}man;
void main()
{
clrscr();
man.input();
man.ino();
man.disp();
man.ono();
getch();
}
Output-
Enter employee number
10
Enter employee name
Rahul
Enter employee address
Bangalore
Enter employee department
Sales
Enter no of employees
10
Employee number-1
Employee name-Abc
Employee address-Bangalore
Employee department-sales
Number of employees-10
18.Write a program to accept student details and display them.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
public:
char name[25];
int age;
void readdata()
{
cout<<"Enter name"<<endl;
gets(name);
cout<<"Enter age"<<endl;
cin>>age;
}
void display()
{
cout<<"Name of student-";
puts(name);
cout<<"Age-"<<age<<endl;
}
};
class primarystudent:public student
{
public:
char activity[25];
int nohrs;
void readprimary()
{
cout<<"Enter activity"<<endl;
gets(activity);
cout<<"Enter number of hours spent in activity"<<endl;
cin>>nohrs;
}
void displayprimary()
{
cout<<"Activity-";
puts(activity);
cout<<"Number of hours spent-"<<nohrs<<endl;
}
}prime;
class secondarystudent:public student
{
public:
char combo[25];
void readsecondary()
{
cout<<"Enter combination of student"<<endl;
gets(combo);
}
void displaysecondary()
{
cout<<"Combination of student-";
puts(combo);
}
}sec;
class equipment:public secondarystudent
{
public:
char namee[25];
int roll;
void readequip()
{
cout<<"Enter name of equipment"<<endl;
gets(namee);
cout<<"Enter roll no of equipment"<<endl;
cin>>roll;
}
void displayequip()
{
cout<<"Name of equipment-";
puts(namee);
cout<<"Roll no-"<<roll<<endl;
}
}equip;
class book:public student
{
public:
char bookname[25];
int nopgs;
char author[25];
void readb()
{
cout<<"Enter name of book"<<endl;
gets(bookname);
cout<<"Enter number of pages"<<endl;
cin>>nopgs;
cout<<"Enter author name"<<endl;
gets(author);
}
void displayb()
{
cout<<"Name of book-";
puts(bookname);
cout<<"Number of pages-"<<nopgs<<endl;
cout<<"Author-";
puts(author);
}
}bo;
void main()
{
clrscr();
int ch;
cout<<"Enter book details"<<endl;
bo.readb();
cout<<"Enter 1 if primary student, 2 if secondary"<<endl;
cin>>ch;
if(ch==1)
{
cout<<"Enter basic details"<<endl;
prime.readdata();
cout<<"Enter specific details"<<endl;
prime.readprimary();
cout<<"Book details-"<<endl;
bo.displayb();
cout<<"Student details-"<<endl;
prime.display();
prime.displayprimary();
}
else
{
cout<<"Enter basic details"<<endl;
equip.readdata();
cout<<"Enter specific details"<<endl;
equip.readsecondary();
cout<<"Enter equipment details"<<endl;
equip.readequip();
cout<<"Book details-"<<endl;
bo.displayb();
cout<<"Student details-"<<endl;
equip.display();
equip.displaysecondary();
cout<<"Equipment details"<<endl;
equip.displayequip();
}
getch();
}
Output-
Enter book details
Enter name of book
Computer Science
Enter number of pages
200
Enter author name
Sumita Arora
Enter 1 if primary student,2 if secondary
1
Enter basic details
Enter name
RAVI
Enter age
9
Enter specific details
Enter activity
Football
Enter number of hours spent in activity
3
Book details-
Name of book-Computer Science
Number of pages-200
Author-Sumita Arora
Student details-
Name of student-RAVI
Age-13
Activity-Football
Number of hours spent-2
19.WAP to display details of student and his grades using inheritance

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
Class person{ char name[21]; int age;
public:
void indata()
{ cout<<"\n\nEnter the name of Student: " ;
gets(name);
cout<<"\n\nEnter the age : ";
cin>>age;
}
void outdata()
{ cout<<"\n\n";
for(int i=0; i<79; i++)
cout<<"-";
cout<<"\n\nName of the student is: "<<name;
cout<<"\n\nAge of the student is : "<<age;
}
};
class student: public person
{ float Tmarks;
int rollno;
public:
char calgrade()
{if(Tmarks>90)
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';
else if(Tmarks>70&&Tmarks<=80)
return 'C';
else if(Tmarks>60&&Tmarks<=70)
return 'D';
else
return 'E';
}

void enter()
{ cout<<"\n\nEnter the roll number: "; cin>>rollno;
cout<<"\n\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{ cout<<"\n\nRoll number : "<<rollno;
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{ clrscr();
student A;
A.indata();
A.enter();
A.outdata();
A.display();
getch();
}


Output:

Enter the name of Student: Rahul Verma
Enter the age : 16
Enter the roll number: 34
Enter total marks (out of 100) : 92
-------------------------------------------------------------------------------
Name of the student is: Rahul Verma
Age of the student is : 16
Roll number : 34
Total marks are : 92
Grade = A
20. Write a program to accept worker details and display them along with
monthly salary.
#include<iostream.h>
#include<conio.h>
class worker
{
char wname[25];
float hrwrk,wgrate;
float totalwage;
float calcwage();
public:
void in_data();
void out_data();
}wor;
float worker ::calcwage()
{
return(hrwrk*wgrate);
}
void worker ::in_data()
{
cout<<"Enter name of worker"<<endl;
cin>>wname;
cout<<"Enter hours worked"<<endl;
cin>>hrwrk;
cout<<"Enter hourly wage"<<endl;
cin>>wgrate;
totalwage=calcwage();
}
void worker ::out_data()
{
cout<<"Name of worker-"<<wname<<endl;
cout<<"Hours worked-"<<hrwrk<<endl;
cout<<"Hourly wage-"<<wgrate<<endl;
cout<<"Total wage-"<<totalwage<<endl;
}
void main()
{
clrscr();
wor.in_data();
wor.out_data();
getch();
}
Output-
Enter name of worker
Rahul
Enter hours worked
2
Enter hourly wage
1
Name of worker-Rahul
Hours worked-2
Hourly wage-1
Total wage-2
21.WAP to create a text file(.txt) and display number of words, alphabets,
vowels and consonants and number of lowercase and uppercase letters using
the concept of DATA FILE HANDLING

#include<fstream.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
void main()
{
clrscr();
char a[80];

int words=0, upper_letters=0, lower_letters=0, alpha=0, vowels=0,consonants=0;

ofstream string("str.txt");

cout<<"\n\nEnter the string : ";
cin.getline(a,79);

string<<"The string is : "<<a<<"\n\n";

for(int i=0; i<strlen(a); i++)
{
if(a[i]==' ')
while(a[i]==' ')
{
i++;
}

if(isalnum(a[i]))
{
while(a[i]!=' ')
{
i++;
}
words++;
}
}

string<<"\n\nThe number of words in string are : "<<words;

string<<"\n\n\nAlphabets in string are : \n\n";
for(i=0; i<strlen(a); i++)
{
if(isalpha(a[i]))
{
alpha++;
string<<a[i]<<" ";
}
}
string<<"\n\nTotal number of alphabets in string => "<<alpha;

string<<"\n\n\nUppercase letters in string are : \n\n";
for(i=0; i<strlen(a); i++)
{
if(isupper(a[i]))
{
upper_letters++;
string<<a[i]<<" ";
}
}
string<<"\n\nTotal number of uppercase letters in string => "<<upper_letters;


string<<"\n\n\nLowercase letters in string are : \n\n";
for(i=0; i<strlen(a); i++)
{
if(islower(a[i]))
{
lower_letters++;
string<<a[i]<<" ";
}
}
string<<"\n\nTotal number of Lowercase letters in string => "<<lower_letters;


string<<"\n\n\nVowels in string are : \n\n";
for(i=0; i<strlen(a); i++)
{
if(isalpha(a[i]))
{

if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||a[i]=='i'||a[i]=='I'||a[i]=='o'||a[i]=='O'||a[i]=
='u'||a[i]=='U')
{
vowels++;
string<<a[i]<<" ";
}
}
}
string<<"\n\nTotal number of vowels in string => "<<vowels;

string<<"\n\n\nConsonants in string are : \n\n";
for(i=0; i<strlen(a); i++)
{
if(isalpha(a[i]))
{

if(a[i]!='a'&&a[i]!='A'&&(a[i]!='e'&&a[i]!='E'&&a[i]!='i'&&a[i]!='I'&&(a[i]!='o'&&a[i]!='O')&&a[i]!
='u'&&a[i]!='U')
{
consonants++;
string<<a[i]<<" ";
}
}
}
string<<"\n\nTotal number of vowels in string => "<<consonants;
getch();
}
Output-
Output :

Enter the string : Rahul Verma


Output of created text file:


The string is : Rahul Verma


The number of words in string are : 2



Alphabets in string are :

R a h u l V e r m a

Total number of alphabets in string => 10



Uppercase letters in string are :

R V

Total number of uppercase letters in string => 2



Lowercase letters in string are :

a h u l e r m a

Total number of Lowercase letters in string => 8



Vowels in string are :

a u e a

Total number of vowels in string => 4



Consonants in string are :

R h l V r m

Total number of vowels in string => 6


22.WAP to find the number of times the word to appears in the file.
#include<fstream.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>

void COUNT_TO()
{
fstream tfile("NOTES.TXT", ios :: in);
char str;
int c=0;
clrscr();
while(tfile)
{
tfile.get(str);
if (str==' ')
{
tfile.get(str);
if (tolower(str)=='t')
{
tfile.get(str);
if (tolower(str)=='o')
tfile.get(str);
if (str==' ')
c++;
}
}
}
tfile.close();
cout << "Number of times 'to' appear is "<< c;
}
main()
{
clrscr();
COUNT_TO();
return 0;
}

Output-
Number of times to appears is 6

23.RECORD:Write a function program with sum() as function with 2 arguments-
double x and int n.The function should return a value of type
double and find sum of the following series-
1 + x/1! + x^3/2! +x^5/3!...+x^2n-1/n!
#include<iostream.h>
#include<conio.h>
double sum(double,int);
void main()
{
clrscr();
double x;
int n;
cout<<"Enter x and n"<<endl;
cin>>x>>n;
cout<<"Sum of series="<<sum(x,n)<<endl;
getch();
}
double sum(double x,int n)
{
double sum=1,temp=1/x;
for(int i=1;i<=n;i++)
{
temp*=x*x/i;
sum+=temp;
}
return(sum);
}
Output-
Enter x and n
2
3
Sum of series=12.333333
24.RECORD:Declare a structure to represent a complex number having real
and imaginary parts. Write functions to add, subtract, multiply
and divide the two complex numbers.
#include<iostream.h>
#include<conio.h>
struct complex
{
double r,i;
}n1,n2,n3;
void add()
{
n3.r=n1.r+n2.r;
n3.i=n1.i+n2.i;
}
void sub()
{
n3.r=n1.r-n2.r;
n3.i=n1.i-n2.i;
}
void mul()
{
n3.r=(n1.r*n2.r)-(n1.i*n2.i);
n3.i=(n1.r*n2.i)+(n1.i*n2.r);
}
void div()
{
n2.i*=-1;
n3.r=(n1.r*n2.r)-(n1.i*n2.i);
n3.i=(n1.r*n2.i)+(n1.i*n2.r);
double den=(n2.r*n2.r)+(n2.i*n2.i);
n3.r/=den;
n3.i/=den;
}
void main()
{
clrscr();
char ch;
int real,imag;
cout<<"Enter real and complex part of first number"<<endl;
cin>>n1.r>>n1.i;
cout<<"Enter real and complex part of second number"<<endl;
cin>>n2.r>>n2.i;
cout<<"Enter + for addition, - for subtraction, * for multiplication, / for division"<<endl;
cin>>ch;
switch(ch)
{
case '+':cout<<"Addition"<<endl;
add();
break;
case '-':cout<<"Subtraction"<<endl;
sub();
break;
case '*':cout<<"Multiplication"<<endl;
mul();
break;
case '/':cout<<"Division"<<endl;
div();
break;
default:cout<<"Invalid input"<<endl;
n3.r=0,n3.i=0;
}
if(n3.i>0)
cout<<"Number="<<n3.r<<"+"<<n3.i<<"i";
else
cout<<"Number="<<n3.r<<n3.i<<"i";
getch();
}
Output-
Enter real and complex part of first number
1
2
Enter real and imaginary part for second number
3
4
Enter + for addition, - for subtraction, * for multiplication, / for division
+
Addition
Number=4+6i
25.RECORD:From a 2D array A[4][4] write a program to prepare
a 1D array B[16] that will have all the elements of A
if they are stored in column major form.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int A[4][4],B[16];
for(int i=0;i<4;i++)
{
cout<<"Enter elements of row no."<<i+1<<endl;
for(int j=0;j<4;j++)
{
cin>>A[i][j];
}
}
int k=0;
for(i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
B[k]=A[j][i];
k++;
}
}
cout<<"Single dimension array is-"<<endl;
for(i=0;i<16;i++)
cout<<B[i]<<",";
getch();
}
Output-
Enter elements of first row
1
2
3
4
Enter elements of second row
5
6
7
8
Enter elements of third row
9
10
11
12
Enter elements of fourth row
13
14
15
16
Array is-1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16
26.RECORD:Write two overloaded functions to find GCD of two numbers
amongst
which one function would be normal, other recursive.
#include<iostream.h>
#include<conio.h>
int gcd(int a,int b);
int gcd(int n);
int c,d;
void main()
{
clrscr();
int a,b;
int ch;
cout<<"Enter the two numbers"<<endl;
cin>>a>>b;
cout<<"Enter 1 for normal function, 2 for recursive function"<<endl;
cin>>ch;
if(ch==1)
cout<<"GCD="<<gcd(a,b);
else if(ch==2)
{
c=a,d=b;
cout<<"GCD="<<gcd(a);
}
else
cout<<"Invalid input"<<endl;
getch();
}
int gcd(int a,int b)
{
int g=0;
for(int i=1;i<=a;i++)
{
if(a%i==0 && b%i==0)
g=i;
}
return(g);
}
int gcd(int n)
{
if(n==1)
return 1;
else if(c%n==0 && d%n==0)
return n;
else
return(gcd(n-1));
}
Output-
Enter the 2 numbers
8
12
Enter 1 for normal function, 2 for recursive
1
GCD=4
27.RECORD:Write an overloaded function power() having two versions. The first
version takes double n and int p and returns a double value. The
second takes int n and int p and returns int value. Use a default value
2 for p in case p is omitted in function call.
#include<iostream.h>
#include<conio.h>
#include<math.h>
double power(double,int p=2);
int power(int,int p=2);
void main()
{
clrscr();
double n;
int p;
int c,ch;
cout<<"Enter n"<<endl;
cin>>n;
cout<<"Enter 1 if you wish to enter p"<<endl;
cin>>c;
if(c==1)
{
cout<<"Enter p"<<endl;
cin>>p;
}
cout<<"Enter 1 for n^p, 2 for n*n*n....p times"<<endl;
cin>>ch;
if(c==1 && ch==1)
cout<<"Result="<<power(n,p);
else if(c!=1 && ch==1)
cout<<"Result="<<power(n);
else if(c==1 && ch==2)
{
n=(int)n;
cout<<"Result="<<power(n,p);
}
else if(c!=1 && ch==2)
{
n=(int)n;
cout<<"Result="<<power(n);
}
else
cout<<"Invalid choice"<<endl;
getch();
}
double power(double n,int p)
{
double temp=pow(n,p);
return temp;
}
int power(int n,int p)
{
int temp=1;
for(int i=1;i<=p;i++)
{
temp*=n;
}
return temp;
}
Output-
Enter n
2
Enter 1 if you wish to enter p
0
Enter 1 for n^p, 2 for n*n*n...p times
2
Result=4
28.RECORD:Write two versions of an overloaded function sum(). The first
version
accepts and int array and returns sum of all elements. The second
version accepts an int array and a character. It returns sum of even
elements if character is 'E', sum of odd elements if character is 'O',
and 0 if any other character.
#include<iostream.h>
#include<conio.h>
int sum(int ar[5]);
int sum(int ar[5],char);
void main()
{
clrscr();
int ar[5];
char ch;
cout<<"Enter 5 elements for the array"<<endl;
for(int i=0;i<5;i++)
cin>>ar[i];
cout<<"Enter 1 for sum of even or odd elements"<<endl;
cin>>ch;
if(ch=='1')
{
cout<<"Enter E for even, O for odd"<<endl;
cin>>ch;
cout<<"Sum="<<sum(ar,ch);
}
else
cout<<"Sum="<<sum(ar);
getch();
}
int sum(int ar[5])
{
int s=0;
for(int i=0;i<5;i++)
{
s+=ar[i];
}
return s;
}
int sum(int ar[5],char ch)
{
int e=0,o=0;
for(int i=0;i<5;i++)
{
if(ar[i]%2==0)
e+=ar[i];
else
o+=ar[i];
}
if(ch=='E')
return e;
else if(ch=='O')
return o;
else
return 0;
}
Output-
Enter 5 elements for the array
1
2
3
4
5
Enter 1 for sum of even or odd elements
0
Sum=15
29.RECORD:Write a program to keep count of objects created using static
members.
#include<iostream.h>
#include<conio.h>
class counter
{
static int c;
public:
counter()
{
c++;
}
void display()
{
cout<<"Number of objects created="<<c<<endl;
}
};
int counter ::c=0;
void main()
{
clrscr();
char ch;
cout<<"Enter 1,2,3 to create 1,2,3 objects respectively"<<endl;
cin>>ch;
if(ch=='1')
{
counter A;
cout<<"Object called A created"<<endl;
A.display();
}
else if(ch=='2')
{
counter B;
counter C;
cout<<"Objects called B and C created"<<endl;
C.display();
}
else if(ch=='3')
{
counter D;
counter E;
counter F;
cout<<"Objects called D,E,F created"<<endl;
F.display();
}
else
cout<<"Invalid input"<<endl;
getch();
}
Output-
Enter 1,2,3 to create 1,2,3 objects respectively
1
Object called A created
Number of objects created=1
30.RECORD:Write a program to perform various operations on a string class
without
using language supported biult-in string functions. Operations are read
a string, display, reverse, copy to empty, concatenate.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class string
{
char str[25],str2[25],r[25],cop[25],con[50];
public:
read();
disp();
rev();
copy();
concat();
}s;
string ::read()
{
cout<<"Enter string"<<endl;
gets(str);
}
string ::disp()
{
cout<<"String=";
puts(str);
cout<<endl;
}
string ::rev()
{
for(int i=0;str[i]!='\0';i++);
for(int k=0,j=i-1;j>=0;j--,k++)
{
r[k]=str[j];
}
cout<<"Reversed string=";
puts(r);
}
string ::copy()
{
for(int i=0;i<25;i++)
cop[i]=str[i];
cout<<"Copied string=";
puts(cop);
cout<<endl;
}
string ::concat()
{
cout<<"Enter second string"<<endl;
gets(str2);
for(int i=0;str[i]!='\0';i++);
for(int j=0;str2[j]!='\0';j++);
for(int k=0;k<i;k++)
con[k]=str[k];
for(int l=0;l<j;l++)
con[l+i]=str[l];
cout<<"Concatenated strings=";
puts(cop);
}
void main()
{
clrscr();
s.read();
s.disp();
s.rev();
s.copy();
s.concat();
getch();
}
Output-
Enter string
Computer
String=Computer
Reversed string=retupmoC
Copied string=Computer
Enter second string
Science
Concatenated strings=ComputerScience
31.RECORD:Define a class Applicant that accepts admission no, name, aggregate
marks, calculates grade and displays all the details.
#include<iostream.h>
#include<conio.h>
class Applicant
{
long int ANo;
char Name[25];
float Agg;
char Grade;
void GradeMe()
{
if(Agg>=80)
Grade='A';
else if(Agg<80 && Agg>=65)
Grade='B';
else if(Agg<65 && Agg>=50)
Grade='C';
else
Grade='D';
}
public:
void ENTER()
{
cout<<"Enter Admission number"<<endl;
cin>>ANo;
cout<<"Enter name"<<endl;
cin>>Name;
cout<<"Enter Aggregate marks"<<endl;
cin>>Agg;
GradeMe();
}
void RESULT()
{
cout<<"Admission number="<<ANo<<endl;
cout<<"Name="<<Name<<endl;
cout<<"Aggregate marks="<<Agg<<endl;
cout<<"Grade="<<Grade<<endl;
}
}A;
void main()
{
clrscr();
A.ENTER();
A.RESULT();
getch();
}
Output-
Enter Admission number
1
Enter name
Rahul
Enter Aggregate marks
100
Admission number=1
Name=Rahul
Aggregate marks=100
Grade=A
32.RECORD:Define a class ticbooth which counts number of people who pass by
and
whether they buy a ticket or not, and displays all the details.
#include<iostream.h>
#include<conio.h>
class ticbooth
{
int totp;
double totm;
public:
ticbooth()
{
totp=0,totm=0;
}
void incp()
{
totp++;
}
void incm()
{
totm=totm+2.5;
}
void display()
{
cout<<"Number of people who visited="<<totp<<endl;
cout<<"Amount collected="<<totm<<endl;
}
void ticket()
{
cout<<"Tickets sold="<<totm/2.5<<endl;
}
}t;
void main()
{
clrscr();
cout<<"Ticket booth"<<endl;
char ch='0',buy;
while(ch=='0')
{
cout<<"A person passes by"<<endl;
cout<<"Enter 1 if he buys a ticket"<<endl;
cin>>buy;
if(buy=='1')
{
t.incp();
t.incm();
}
else
t.incp();
cout<<"Enter 0 to continue"<<endl;
cin>>ch;

}
t.display();
t.ticket();
getch();
}
Output-
Ticket booth
A person passes by
Enter 1 if he buys a ticket
1
Enter 0 to continue
0
A person passes by
Enter 1 if he buys a ticket
0
Enter 0 to continue
1
Number of people who visited=2
Amount collected=2.5
Tickets sold=1
33.RECORD:Define a class TravelPlan, use constructor to initialise plan code,
place, number of travellers and number of buses. Allow user to enter the
values, calculate number of buses needed and display the information.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class TravelPlan
{
long int PlanCode;
char Place[25];
int Number_of_travellers;
int Number_of_buses;
public:
TravelPlan()
{
PlanCode=1001;
strcpy(Place,"Agra");
Number_of_travellers=5;
Number_of_buses=1;
}
void NewPlan()
{
cout<<"Enter plan code"<<endl;
cin>>PlanCode;
cout<<"Enter place"<<endl;
gets(Place);
cout<<"Enter number of travellers"<<endl;
cin>>Number_of_travellers;
if(Number_of_travellers<20)
Number_of_buses=1;
else if(Number_of_travellers>=20 && Number_of_travellers<40)
Number_of_buses=2;
else
Number_of_buses=3;
}
void ShowPlan()
{
cout<<"Plan code="<<PlanCode<<endl;
cout<<"Place=";
puts(Place);
cout<<"Number of travellers="<<Number_of_travellers<<endl;
cout<<"Number of buses="<<Number_of_buses<<endl;
}
}tp;
void main()
{
clrscr();
tp.NewPlan();
tp.ShowPlan();
getch();
}
Output-
Enter plan code
1
Enter place
Bangalore
Enter number of travellers
10
Plan code=1
Place=Bangalore
Number of travellers=10
Numbers of buses=1
34.RECORD:Define a class Serial with information like serial code, title, duration
and no of episodes. Initialise using constructors and member functions
and then display.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Serial
{
int Serialcode;
char Title[20];
float Duration;
int Noofepisodes;
public:
Serial()
{
Duration=30;
Noofepisodes=10;
}
void Newserial()
{
cout<<"Enter serial code"<<endl;
cin>>Serialcode;
cout<<"Enter title"<<endl;
gets(Title);
}
void Otherentries(float d,int n)
{
Duration=d;
Noofepisodes=n;
}
Dispdata()
{
cout<<"Serial code="<<Serialcode<<endl;
cout<<"Title=";
puts(Title);
cout<<"Duration="<<Duration<<endl;
cout<<"No of episodes="<<Noofepisodes<<endl;
}
}s;
void main()
{
clrscr();
float d;
int n;
s.Newserial();
cout<<"Enter duration and no of episodes"<<endl;
cin>>d>>n;
s.Otherentries(d,n);
s.Dispdata();
getch();
}
Output-
Enter serial code
1
Enter title
Tom and Jerry
Enter duration and no of episodes
30
100
Serial code=1
Title=Tom and Jerry
Duration=30
No of episodes=100
35.RECORD:Define a class Clothing to accept code, type, size and material,
calculate
price and display.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class Clothing
{
char Code[25],Type[25];
int Size;
char Material[25];
float Price;
Calc_Price()
{
if(!strcmp(Material,"COTTON"))
{
if(!strcmp(Type,"TROUSER"))
Price=1500;
else if(!strcmp(Type,"SHIRT"))
Price=1200;
else
{}
}
else
{
if(!strcmp(Type,"TROUSER"))
Price=1500-1500*0.25;
else if(!strcmp(Type,"SHIRT"))
Price=1200-1200*0.25;
else
{}
}
}
public:
Clothing()
{
strcpy(Code,"NOT ASSIGNED");
strcpy(Type,"NOT ASSIGNED");
strcpy(Material,"NOT ASSIGNED");
Size=0;
Price=0;
}
void Enter()
{
cout<<"Enter code, type, size, material"<<endl;
gets(Code);
gets(Type);
cin>>Size;
gets(Material);
Calc_Price();
}
void Show()
{
cout<<"Code=";
puts(Code);
cout<<"Type=";
puts(Type);
cout<<"Size="<<Size<<endl;
cout<<"Material=";
puts(Material);
cout<<"Price="<<Price<<endl;
}
}c;
void main()
{
clrscr();
c.Enter();
c.Show();
getch();
}
Output-
Enter code, type, size, material
1
SHIRT
42
COTTON
Code=1
Type=SHIRT
Size=42
Material=COTTON
Price=1200
36.RECORD:Write a function to accept time of 2 clocks in hours, minutes,
seconds
and output time since last struck 12 in seconds and difference between
the two in seconds.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void time(int h1,int m1,int s1,int h2,int m2,int s2);
void main()
{
clrscr();
int h1,m1,s1,h2,m2,s2;
cout<<"Enter first clock's time in hours, minutes,seconds"<<endl;
cin>>h1>>m1>>s1;
cout<<"Enter second clock's time in hours, minutes, seconds"<<endl;
cin>>h2>>m2>>s2;
time(h1,m1,s1,h2,m2,s2);
getch();
}
void time(int h1,int m1,int s1,int h2,int m2,int s2)
{
int t1=h1*3600+m1*60+s1;
int t2=h2*3600+m2*60+s2;
cout<<"Seconds since first clock struck 12="<<t1<<endl;
cout<<"Seconds since second clock struck 12="<<t2<<endl;
int t3=(fabs)(t1-t2);
cout<<"Difference between 2 clocks in seconds="<<t3<<endl;
}
Output-
Enter first clock's time in hours, minutes, seconds
1
10
20
Enter second clocks' time in hours, minutes, seconds
3
20
4
Seconds since first clock struck 12=4220
Seconds since second clock struck 12=12004
Difference between two clocks in seconds=7784

Das könnte Ihnen auch gefallen