Sei sulla pagina 1di 20

//May June 2010 Q-3c //Write a C Program to find the sum of the digits of a number (minimum #include<iostream.h> #include<conio.

.h> void main() { clrscr(); int i,n,sum=0; cout<<"\nEnter the number: "; cin>>n; if(n/10000==0) //For checking if five digits are entered cout<<"\n\n\t>>Wrong input given (Minimum five digits only)<<"; else { for(i=0;n>0;i++) { sum+=n%10; n=n/10; } cout<<"\nThe sum of the digits is: "<<sum; } getch(); } Output:

//May June 2010 Q4a //Write a simple C++ program to accept a string and counts the number of alphabets, digits and special symbols present in a given string. #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char s[80]; int i,a=0,d=0,sp=0; cout<<"\nEnter the string: "; gets(s); for(i=0;s[i]!='\0';i++) { if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')) a++; else if(s[i]>='0'&&s[i]<='9') d++; else sp++; } cout<<"\nThe number of alphabets are: "<<a; cout<<"\nThe number of digits are: "<<d; cout<<"\nThe number of special symbols are: "<<sp; getch(); } Output:

//Dec Jan 2010 Q-6b //Write a C++ program to include a class time which has hours, minutes and seconds as data, a constructor without argument to initialize data to 0, a constructor with three arguments to be assigned to hours, minutes and seconds respectively, a constructor with one float argument and converts it to hours, minutes and seconds, display and destructor function. #include<iostream.h> #include<conio.h> class time { private: int hours,minutes,seconds; public: time() { hours=0; minutes=0; seconds=0; } time(int x,int y,int z) { hours=x; minutes=y; seconds=z; } time(float a) { int b; b=a; a=a-b; seconds=a*100.0000+1.0; minutes=b%60; hours=b/60; } void display()

{ cout<<"\n"; "<<seconds; } ~time() { cout<<"\nDestructing"; } }; void main() { clrscr(); time p; time q(12,34,60); time r(1120.20); p.display(); q.display(); r.display(); getch(); } Output: cout<<"Hours: "<<hours<<" Minutes: "<<minutes<<" Seconds:

//Dec Jan 2010 Q-4b //Write a C++ program to find the largest element of an array, using a function #include<iostream.h> #include<conio.h> void lar(int a[],int); void main()

{ clrscr(); int a[20],size,i; cout<<"\nEnter the size of array (<20): "; cin>>size; for(i=0;i<size;i++) { cout<<"\nEnter the "<<i+1<<" element: "; cin>>a[i]; } lar(a,size); getch(); } void lar(int a[],int s) { int i,num=0; for(i=0;i<s;i++) { if(num<=a[i]) num=a[i]; } cout<<"\nThe largest element of the array is: "<<num; } Output:

//Dec Jan 2010 Q-4c //Write a C++ recursive function to find the greatest common divisor of two integers #include<iostream.h> #include<conio.h> int gcd(int a,int b) { int c=b%a; if(c==0) return a; else return gcd(c,a); } void main() { clrscr(); int x,y,g; cout<<"\nEnter the two numbers: "; cin>>x>>y; g=gcd(x,y); cout<<"\nGCD of the two numbers is: "<<g; getch(); } Output:

//Jan 2011 Q-3b //Write a C++ recursive program to find the factorial of a number #include<iostream.h> #include<conio.h> int fact(int a) { int n; if(a==1) return 1; n=fact(a-1)*a; return n; } void main() { clrscr(); int x,f; cout<<"\nEnter the number: "; cin>>x; f=fact(x); cout<<"\nThe factorial of the number is: "<<f; getch(); } Output:

//Jan 2011 Q-4a //Write a C++ program to swap two numbers using call by address. #include<iostream.h> #include<conio.h> void swp(int *p,int *q) { int t; t=*p; *p=*q; *q=t; } void main() { clrscr(); int a,b,*t,*r; cout<<"\nEnter the two numbers: "; cin>>a>>b; cout<<"\nThe numbers before swapping are: "<<a<<" "<<b; t=&a; r=&b; swp(t,r); cout<<"\nThe numbers after swapping are: "<<a<<" "<<b; getch(); } Output:

//Jan 2011 Q-6b //Write a C++ program to calculate the surface area and volume of a sphere using equations 4r2 and 4/3 r3 where r is the radius of the sphere using class sphere and object mysphere and member functions as vol() and s_area() #include<iostream.h> #include<conio.h> #define pi 3.142 class sphere { public: void vol(float r) { float v; v=4*pi*r*r*r/3; cout<<"\n The volume of the sphere is: "<<v; } void s_area(float r) { float s; s=4*pi*r*r; cout<<"\n The surface area of the sphere is: "<<s; } }; void main() { clrscr();

float r; sphere mysphere; cout<<"\n Enter the radius of the sphere: "; cin>>r; mysphere.s_area(r); mysphere.vol(r); getch(); } Output:

//June 2009 Q-4b //Write a recursive function to find the nth Fibonacci number #include<iostream.h> #include<conio.h> int i,a[10],x; int fib(int v) { i++; if(i==x) return 0; else { a[i]=v; cout<<" "<<a[i]; fib(a[i]+a[i-1]); } } void main() { clrscr(); cout<<"\n Enter the length of the series (Min length=1): "; cin>>x; i=0; a[0]=0; cout<<"\n The fibonacci series is: "; cout<<a[0]; fib(1); getch(); } Output:

// June 2009 Q-4c //Write an inline function to find the largest of two integers #include<iostream.h> #include<conio.h> inline lar(int x,int y) { return (x>y?x:y); } void main() { clrscr(); int x,y,l; cout<<"\n Enter the two numbers: "; cin>>x>>y; l=lar(x,y); cout<<"\n The largest number is: "<<l; getch(); } Output: //Using conditional operator

// June 2009 Q-3b //Write a program in C++ to count the number of vowels in a given string #include<iostream.h> #include<stdio.h> #include<conio.h> void main() { clrscr(); char s[80]; int i,v=0; cout<<"\n Enter the string: "; gets(s); for(i=0;s[i]!='\0';i++) { switch(s[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U':v++;break; default:break; } } cout<<"\n The number of vowels is: "<<v; getch(); } Output:

//Jan 2012 Q3c //Write a C++ program to accept a string from the keyboard, count the different vowels and display each count separately. Use the switch statement #include<iostream.h> #include<stdio.h> #include<conio.h> void main() { clrscr(); char s[80]; int j,a=0,e=0,i=0,o=0,u=0; cout<<"\n Enter the string: "; gets(s); for(j=0;s[j]!='\0';j++) { switch(s[j]) { case 'a':a++;break; case 'e':e++;break; case 'i':i++;break; case 'o':o++;break; case 'u':u++;break; case 'A':a++;break; case 'E':e++;break; case 'I':i++;break; case 'O':o++;break;

case 'U':u++;break; default:break; } } cout<<"\n The number of vowels is: "<<(a+e+i+o+u); cout<<"\n No. of a's: "<<a; cout<<"\n No. of e's: "<<e; cout<<"\n No. of i's: "<<i; cout<<"\n No. of o's: "<<o; cout<<"\n No. of u's: "<<u; getch(); } Output:

//Jan 2012 Q-6b //Create a class called EMPLOYEE. The data members are: employee name, employee id and salary per month. 10% bonus will be added at the end of the year. Write member functions to read the details, compute ross salary per annum and display the details. In the main() program, make this record for 3 employees. #include<iostream.h> #include<conio.h> #include<stdio.h> class employee { char emp_name[80]; long int emp_id; float spm; public: void read() { cout<<"\n\n\n Enter the name of the employee: "; gets(emp_name); cout<<"\n Enter the employee id number: "; cin>>emp_id; cout<<"\n Enter the salary per month of the employee: "; cin>>spm; } void compute() { float gross; gross=spm*12; gross=gross+(gross/10.0);

display(gross); } void display(float gross) { cout<<"\n The employee name is: "<<emp_name; cout<<"\n The employee id number is: "<<emp_id; cout<<"\n Salary per month of the employee is: "<<spm; cout<<"\n Gross Salary for employee: "<<gross; } }; void main() { clrscr(); employee a; employee b; employee c; a.read(); a.compute(); b.read(); b.compute(); c.read(); c.compute(); getch(); }

Output:

//Find the roots of equation ax2+bx+c=0 using SWITCH and CASE

#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); float a,b,c,real,img,temp,r1,r2; int flag; cout<<"\nEnter the values of a,b,c in format ax2+bx+c=0: "; cin>>a>>b>>c; temp=((b*b)-(4*a*c)); real=((-b)/(2*a)); if(temp<0) flag=0; else flag=1; switch(flag) { case 0:cout<<"\nResult is imaginary"; temp=temp*-1; img=sqrt(temp)/(2*a); cout<<"\nResult: "<<real<<" +j"<<img; cout<<"\nResult: "<<real<<" -j"<<img; break; case 1:cout<<"\nResult is real"; r1=real+(sqrt(temp)/(2*a)); r2=real-(sqrt(temp)/(2*a)); cout<<"\nResult is: "<<r1; cout<<"\nResult is: "<<r2; break; } getch();

Potrebbero piacerti anche