Sei sulla pagina 1di 45

Q: WAP To Find The Greatest Of 3 Numbers. Coding: #include<iostream.h> #include<conio.

h> void main() { clrscr(); inta,b,c; cout<<"\n\tEnter the first number :"; cin>>a; cout<<"\n\tEnter the second number :"; cin>>b; cout<<"\n\tEnter the third number :"; cin>>c; if(a>b && a>c) { cout<<"\n\tFirst number is greatest"; } if(b>a && b>c) { cout<<"\n\tSecond number is greatest"; } if(c>a && c>b) { cout<<"\n\tThird number is greatest"; } getch(); }

Screenshot

Q: WAPTo Calculate Factorial Of A Number. Coding: #include<iostream.h> #include<conio.h> void main() { clrscr(); intn,fac=1; cout<<"Please enter a number: "; cin>>n; for (int i=1;i<=n;i++) fac=fac*i; cout<<fac; getch(); }

Screenshot

Q: WAPTo Print Fibonacci Series Of N Numbers,Where N Is Given By The Programmer. Coding: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a=0,b=1,c=0,n; cout<<"Enter the number of terms you wanna see: "; cin>>n; cout<<a<<" "<<b<<" "; for(int i=1;i<=n-2;i++) { c=a+b; a=b; b=c; cout<<c<<" "; } getch(); }

Screenshot

Q: WAP To Check Whether A Number Is Prime Or Not Coding: #include<iostream.h> #include<conio.h> void main() { clrscr(); intn,m; cout<<"Please enter a number: "; cin>>n; for (int i=2;i<=(n-1);i++) { if(n%i==0) { cout<<"Not a prime"; break; } else { cout<<"Prime"; break; } } getch(); }

Screenshot

Q:WAP To Count Characters In A String. Coding: #include <iostream.h> int main(intnNumberofArgs, char* pszArgs[]) { char s[50]; int i; int letters = 0; // letters int numbers = 0; // numerical characters int other = 0; // other characters int total; // total of all characters

cout<< "Enter a continuous string of characters with no blank spaces" <<endl;

cout<<endl; cout<< "Enter your string:"; cin>> s; cout<<endl;

//loop through the string, counting numbers, letters & others

//////////////////////////////////////////////////////////////////// //count letters ////////////////////////////////////////////////////////////////////

i = 0; while (s[i] !=0)

if (( s[i] >= 'a' && s[i] <= 'z') || (s[i] <= 'A' && s[i] >= 'Z')) { letters++; i++; } total = letters ; cout<< "Your string has " << total << " total characters" <<endl; return 0; }

Screenshot

Q: WAP To Read A Set Of Numbers In An Array And To Find The Largest Of Them. Coding: #include <iostream.h> void main (void) { int a[100]; inti,n,larg; cout<< "How many numbers are in the array?" <<endl; cinn>> n; cout<< "Enter the elements" <<endl; for (i=0;i<=n-1;++i) { cin>> a[i]; } cout<< "Contents of the array" <<endl; for (i=0; i<=n-1;++i) { cout<< a[i] << '\t'; } cout<<endl; larg = a[0]; for (i=0;i<=n-1;++i) { if (larg< a[i]) larg = a[i]; } cout<< "Largest value in the array =" <<larg; }

Screenshot

Q: WAPTo Implement Bubble Sort Using Arrays. Coding: #include<conio.h> #include<iostream.h> void main() { clrscr(); int a[100],i,n,j,temp; cout<<"How many element: "; cin>>n; cout<<"Enter the elements of array: "<<endl; for(i=0;i<n;i++) cin>>a[i]; cout<<"The elements of array Before Sorting: "<<endl; //Coding by: SnehilKhanor //http://WapCPP.blogspot.com for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n-1-i;j++) { if(a[j]>a[j+1]) {

temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } cout<<"Elements of array After Sorting: "<<endl; for(i=0;i<n;i++) cout<<a[i]<<" "; getch(); }

Screenshot

Q: WAP To Read A Set Of Numbers From Keyboard And To Find Sum Of All Elements Of The Given Array Using A Function Coding: #include<iostream.h> void add(intarr[],int n) { inti,sum=0; for(i=1;i<=n;i++) { sum=sum + arr[i]; } cout<<endl<<"the sum is"<<sum<<endl; } void main(){ int set[10],i,sum=0,limit; cout<<"enter number of entries: "; cin>>limit; for(i=1;i<=limit;i++) { cout<<"enter position "<<i<<" :"; cin>>set[i]; } add(set, limit); }

Screenshot

Q: WAP To Swap Variable Using Call By Value Coding #include<iostream.h> void swap(inta,int b); void main() { intx,y; Coutplease enter elements to be swapped; Cinxy; Coutendl<<the elements are<<x<<&<<y; Swap(x,y); } Void swap(inta,int b) {a=a+b; b=a-b; a=a-b; cout<<endlthe swapped elements are<<a<<&<<b<<endl; }

Screenshot

Q: WAP To Swap Variables Using Call By Reference Coding: #include<iostream.h> void swap(int *a,int *b); void swap(int*a ,int*b) { int temp; temp=*a; *a = *b; *b=temp; cout<<endl<<"the swapped elements are"<< *a<<"&"<< *b<<endl; } void main(){ intx,y; cout<<"please enter elements to be swapped"; cin>>x>>y; cout<<endl<<"the elements are"<<x<<"&"<<y; swap(&x,&y); }

Screenshot

Q:WAP To Find The Sum Of Three Numbers Using Pointer To Function Method Coding

#include<iostream.h> #include<conio.h> int swap(int*a,int*b,int*c) { int s; s = (*a) + (*b) + (*c) ; // s = a+b+c; return s; } void main() { inta,b,c,s; clrscr(); cout<<"enter the no"; cin>>a>>b>>c; s=swap(&a,&b,&c); cout<<"the sum is"<<s; getch(); }

Screenshot

Q:Program to show copy constructor. #include<iostream.h> #include<conio.h> class code { private: int id; public: code() { } code(int a) { id=a; } code(code &x) { id=x.id; } void display(void) { cout<<id; } }; void main()

{ clrscr(); code A(100); code B(A); code C=A; code D; D=A; cout<<"The output is:"; cout<<endl<<endl<<"id of A: ";A.display(); cout<<endl<<endl<<"id of B: ";B.display(); cout<<endl<<endl<<"id of C: ";C.display(); cout<<endl<<endl<<"id of D: ";D.display(); }

OUTPUT

Q:WAP To Display Content Of An Array Using Pointer Arithmetic Coding #include<iostream.h> #include<conio.h> void main() { int n, a[20],i, *p=NULL, t=0; clrscr(); cout<<"enter the no of elements"; cin>>n; cout<<"enter the elements"; for(i=0; i<n;i++) { cin>>a[i]; } p=&a[0]; do { cout<<endl<<*p; p++; t++; } while(t!=n); getch(); }

Screenshot

Q: WAP To Find Area Of Circle ,Rectangle,Square&Triangle Using Function Overloading. Coding: #include<iostream.h> floatcalc(float r,float cons); intcalc(intl,int h); intcalc(int l); floatcalc(int l, int h, float cons); void main() { intlength,height; float radius; cout<<"enter radius of circle: "; cin>>radius; cout<<endl<<"the area of circle is:"<<calc(radius,3.14)<<endl; cout<<"enter the length of rectangle"; cin>>length; cout<<"enter the height of rectangle"; cin>>height; cout<<endl<<"the area of rectangle is:"<<calc(length,height)<<endl; cout<<"enter side of square"; cin>>length; cout<<endl<<"the area of square is:"<<calc(length)<<endl; cout<<"enter base of triangle"; cin>>length; cout<<"enter height of triangle:"; cin>>height;

cout<<endl<<"the area of triangle is:"<<calc(length,height,0.5)<<endl; } floatcalc(float r, float cons) { return(cons*r*r); } intcalc(int l, int h) { return(l*h); }

intcalc (int l) { return (l*l); } floatcalc(int l, inth,float cons) { return (l*h*cons); }

Screenshot

Q:Program to check whether given number is prime or not. #include<iostream.h> #include<conio.h> #include<process.h> void main() { clrscr(); int num,i; cout<<endl<<"Enter the number: "; cin>>num; for(i=2;i<=num/2;++i) if(num%i==0) { cout<<endl<<endl<<"Not a prime number!"; exit(0); } cout<<endl<<endl<<"Prime number!"; }

OUTPUT

Q:Program to check whether number is palindrome or not. #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,num,digit,rev=0; cout<<endl<<"Input the number: "; cin>>num; n=num; do { digit=num%10; rev=(rev * 10)+digit; num=num/10; } while(num!=0); cout<<"The reverse of number is: "<<rev<<endl; if(n==rev) cout<<"The number is palindrome!"; else cout<<"The number is not palindrome!"; }

OUTPUT

Q:Program to display the first 100 natural numbers and their sum. #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,sum=0; cout<<"The first 100 natural no. are: "<<endl<<endl; for(i=1;i<=100;i++) { cout<<","<<i; sum=sum+i; } cout<<endl<<endl<<"The sum of first 100 natural no. are: "<<sum; }

OUTPUT

Q: Program to check whether given year is leap year or not. #include<iostream.h> #include<conio.h> void main() { clrscr(); int year; cout<<"Enter the year (in 4-digits): "; cin>>year; if(year%100==0) { if(year%400==0) cout<<"Leap Year!"; } else if(year%4==0) cout<<"Leap Year!"; else cout<<"Not a Leap Year!"; }

OUTPUT

Q: WAP a Program to show the implementation of Multilevel Inheritance. Coding #include<iostream.h> #include<conio.h> #include<stdio.h> class person { char name[20],add[100]; int age; public: voidgetdata() { cout<<"Enter the name:"; gets(name); cout<<"Enter the Address:"; gets(add); cout<<"Enter the age:"; cin>>age; } voidshowdata() { cout<<"\nName:"<<name<<"\nAddress:"<<add<<"\nAge:"<<age; } }; classemployee:private person { intemp_no; floatbasic,gross; public: voidgetempdata() { getdata(); cout<<"Enter employee number:"; cin>>emp_no; cout<<"Enter basic and gross salary:"; cin>>basic>>gross; } voidshowempdata() { showdata(); cout<<"\nEmployee Number:"<<emp_no<<"\nBasic="<<basic<<"\nGross Salary="<<gross; }

}; classmanager:public employee { chardeptt[20],desig[20]; int no; public: voidgetmgrdata() { getempdata(); cout<<"Enter no. of employees:"; cin>>no; cout<<"Enter department:"; gets(deptt); cout<<"Enter designation:"; gets(desig); } voidshowmgrdata() { showempdata(); cout<<"\nDepartment:"<<deptt<<"\nDesignation:"<<desig; cout<<"\nNo. of employees="<<no; } }a; void main() { clrscr(); a.getmgrdata(); cout<<"\nEmployee Information:"; a.showmgrdata(); getch(); }

Screenshot

Q: WAP a program to display how (-) operator is overloaded in the Program. Coding #include <iostream> using namespace std; class Distance { private: int feet; // 0 to infinite int inches; // 0 to 12 public: // required constructors Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } voiddisplayDistance() { cout<< "F: " << feet << " I:" << inches <<endl; } // overloaded minus (-) operator Distance operator- () { feet = -feet; inches = -inches; return Distance(feet, inches); } }; int main() { Distance D1(11, 10), D2(-5, 11); -D1; // apply negation D1.displayDistance(); // display D1 -D2; // apply negation D2.displayDistance(); // display D2 return 0; }

Screenshot

Potrebbero piacerti anche