Skip to main content

Posts

Showing posts from February 27, 2012

Write a program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol.

  Characters ASCII Values A – Z 65 – 90 a – z 97 – 122 0 – 9 48 – 57 special symbols 0 - 47, 58 - 64, 91 - 96, 123 – 127   #include<iostream.h> #include<conio.h> int main () { char ch; cout<<" Enter any character: "; cin>>ch; if (ch>=65 && ch<=90) cout<<" Character is a capital letter "; else if (ch>=97 && ch<=122) cout<<" Character is a small letter "; else if (ch>=48 && ch<=57) cout<<" Character is a digit "; else if ((ch>0 && ch<=47)||(ch>=58 && ch<=64)||(ch>=91 && ch<=96)||(ch>=123 && ch<=127)) cout<<" Character is a special symbol "; getch(); return 0; }

Write a program to calculate the division obtained by the student.

  The marks obtained by a student in 5 different subjects are input by the user. The student gets a division as per the following rules: Percentage above or equal to 60 - First division Percentage between 50 and 59 - Second division Percentage between 40 and 49 - Third division Percentage less than 40 – Fail #include<iostream.h> #include<conio.h> int main() { int sub1,sub2,sub3,sub4,sub5,percentage; cout<<" Enter marks of five subjects : "; cin>>sub1>>sub2>>sub3>>sub4>>sub5; percentage=(sub1+sub2+sub3+sub4+sub5)/5; if (percentage>=60) cout<<" Ist division "; else if (percentage>=50) cout<<" IInd division "; else if (percentage>=40) cout<<" IIIrd division "; else cout<<" Fail " ; getch(); return 0; }

WAP to find the roots of a quadratic equation of type ax2+bx+c where a is not equal to zero.

  #include<iostream.h> #include<conio.h> #include<math.h> void main() { float a,b,c,d,root1,root2; cout<<" Enter value of a, b and c : "; cin>>a>>b>>c; d=b*b-4*a*c; if (d==0) { root1=(-b)/(2*a); root2=root1; cout<<" Roots are real & equal "; } else if (d>0) { root1=-(b+sqrt(d))/(2*a); root2=-(b-sqrt(d))/(2*a); cout<<" Roots are real & distinct "; } else { root1=(-b)/(2*a); root2=sqrt(-d)/(2*a); cout<<" Roots are imaginary "; } cout<<" \nRoot 1= "<<root1<<" \nRoot 2= "<<root2; getch(); } Output: Enter value of a, b and c : 1.0 -10.0 25.0 Roots are real & equal Root 1= 5 Root 2= 5

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

WAP in C++ to check whether a triangle is valid or not, when the three angles of the triangle are entered by the user. A triangle is valid if the sum of all the three angles is equal to 180 degrees.

  #include<iostream.h> #include<conio.h> int main() { int ang1,ang2,ang3; cout<<" Enter the three angles of triangle: "; cin>>ang1>>ang2>>ang3; if (ang1+ang2+ang3==180) cout<<" Triangle is valid "; else cout<<" Triangle is not valid "; getch(); return 0; }

If the ages of Raj, Sumit and Amit are input by the user, write a program to determine the youngest of the three.

  #include<iostream.h> #include<conio.h> void main() { int raj_age,sumit_age,amit_age; cout<<" Enter Raj age: "; cin>>raj_age; cout<<" Enter Sumit age: "; cin>>sumit_age; cout<<" Enter Amit age: "; cin>>amit_age; if (raj_age<sumit_age && raj_age<amit_age) cout<<" Raj is youngest "; else if (sumit_age<raj_age && sumit_age<amit_age) cout<<" Sumit is youngest "; else cout<<" Amit is youngest "; getch(); }

WAP to enter cost price and selling price of an item & determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.

  #include<iostream.h> #include<conio.h> void main() { int cp,sp,result; cout<<" Enter cost price of item : "; cin>>cp; cout<<" Enter selling price of item : "; cin>>sp; result=sp-cp; if (result>0) cout<<" Profit : "<<result; else if (result<0) cout<<" Loss : "<<-(result); else cout<<" No profit no loss "; getch(); }

Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 7000.

  #include<iostream.h> #include<conio.h> void main() { int totalexp, qty, price, discount; cout<<" Enter quantity: "; cin>>qty; cout<<" Enter price: "; cin>>price; totalexp=qty*price; if (totalexp>7000) { discount=(totalexp*0.1); totalexp=totalexp-discount; } cout<<" Total Expense is Rs. "<<totalexp; getch(); }

WAP in C++ for a class called time that has hours and minutes as integer.Calculate the sum two time object & return time.

  #include<iostream.h> #include<conio.h> class time { private : int hours; int minutes; public : void settime( int h, int m) { hours=h; minutes=m; } time sum(time); void showtime(); }; time time::sum(time TM) { time t; t.minutes = minutes + TM.minutes; t.hours=t.minutes/60; t.minutes=t.minutes%60; t.hours += hours + TM.hours; return t; } void time::showtime() { cout<<hours<<" hours and "<<minutes<<" minutes "<<endl; } void main() { time T1,T2,T3; T1.settime(2,45); T2.settime(3,30); T3=T1.sum(T2); cout<<" \n Time 1 : ";T1.showtime(); cout<<" \n Time 2 : ";T2.showtime(); cout<<" \n Time 3 : ";T3.showtime(); getch(); }

WAP in C++ for a class called Distance that has data member feet as integer and inches as float.

  #include<iostream.h> #include<conio.h> class Distance { private : int feet; float inches; public : void setdist( int ft, float in) { feet=ft; inches=in; } Distance add(Distance); void disp(); }; Distance Distance::add(Distance D) { Distance t; t.inches=inches + D.inches; t.feet =0; if (t.inches>=12.0) { t.inches-=12.0; t.feet++; } t.feet +=feet + D.feet; return t; } void Distance::disp() { cout<<feet<<" \' "<<inches<<" \" "; } void main() { Distance d1,d2,d3; d1.setdist(10,7.1); d2.setdist(23,5.5); d3=d1.add(d2); cout<<" \n distance 1 = ";d1.disp(); cout<<" \n distance 2 = ";d2.disp(); cout<<" \n distance 3 = ";d3.disp(); getch(); }

Write a program in C++ for a Addition of two complex number.

  #include<iostream.h> #include<conio.h> class complex { private : float x; float y; public : void set( float real, float img) { x=real; y=img; } complex sum(complex); void display(); }; complex complex::sum(complex C) { complex t; t.x = x + C.x; t.y = y + C.y; return t; } void complex::display() { cout<<x<<" + j "<<y<<endl; } void main() { complex C1,C2,C3; C1.set(2.5,7.1); C2.set(4.2,5.5); C3=C1.sum(C2); cout<<" \n complex Number 1 = ";C1.display(); cout<<" \n complex Number 2 = ";C2.display(); cout<<" \n complex Number 3 = ";C3.display(); getch(); }

Write a program in C++ for a class called Rectangle that has floating point data members length and width.

  #include<iostream.h> #include<conio.h> class Rectangle { private : float length; float width; public : void setlength( float ); void setwidth( float ); float perimeter(); float area(); void show(); int sameArea(Rectangle); }; void Rectangle::setlength( float len) { length = len; } void Rectangle::setwidth( float wid) { width = wid; } float Rectangle::perimeter() { return (2 * length + 2 * width); } float Rectangle::area() { return length * width; } void Rectangle::show() { cout << " Length: " << length << " Width: " << width; } int Rectangle::sameArea(Rectangle other) { float areaf = length * width; float areas = other.length * other.width; if (areaf == areas) return 1; return 0; } void main() { Rectangle first; Rectangle second; first.setlength(5); first.setwidth(2.5); second.setlength(5); second.setwidth(18.9); cout << " First rectangle: