Sie sind auf Seite 1von 6

Sarath

www.creativeminds.co.cc Basic C++ Programs for Technical Interviews

1. Program to add two numbers.


#include<iostream.h> #include<conio.h> void main() { int a,b,sum; clrscr(); cout<<"Enter two numbers"; cin>>a>>b; sum=a+b; cout<<"Sum is:"<<sum; getch(); }

2. Program to swap two numbers without using a third variable.


#include<iostream.h> #include<conio.h> void main() { int a,b; clrscr(); cout<<"Enter the two numbers"; cin>>a>>b; a=a+b; b=a-b; a=a-b; cout<<"A is:"<<a<<endl<<"B is:"<<b; getch(); }

3. Program to find out the greater number.


#include<iostream.h> #include<conio.h> void main() { int a,b; clrscr(); cout<<"Enter the two numbers"; cin>>a>>b; if(a>b)

Creative Minds

Sarath
{ cout<<"A is greater"; } else { cout<<"B is greater"; } getch(); }

www.creativeminds.co.cc

4. Program to find whether a number is palindrome or not.


#include<iostream.h> #include<conio.h>

void main() { int a,temp,d,rev=0; clrscr(); cout<<"\nEnter any number:"; cin>>a; temp=a; while(temp>0) { d=temp % 10; temp /= 10; rev=rev * 10 +d; } if(rev==a) cout<<a<<" is palindrome"; else cout<<a<<" is not palindrome"; getch(); }

5. Program to find out whether number is even or odd.


#include<iostream.h> #include<conio.h> void main() { int a; clrscr(); cout<<"Enter the number";

Creative Minds

Sarath
cin>>a; if(a%2==0) { cout<<"A is even"; } else { cout<<"A is odd"; } getch(); }

www.creativeminds.co.cc

6. Program to implement the friend function.


#include<iostream.h> #include<conio.h> class B; class A { private: int a; public: void geta() { cout<<"enter a:"; cin>>a; } friend void add(A x, B y); };

class B { private: int b; public: void getb() { cout<<"enter b:"; cin>>b; } friend void add(A x, B y); };

Creative Minds

Sarath
void add(A x,B y) { cout<<"sum is:"<<x.a+y.b; } void main() { A x; B y; x.geta(); y.getb(); add(x,y); getch(); } Output:

www.creativeminds.co.cc

enter a:10 enter b:20 sum is:30

7. Program to implement friend classes.


#include<iostream.h> #include<conio.h>

class refbooks; class textbooks { private: int qty; float price; public: void getdata() { cout<<"Enter the number of textbooks:"; cin>>qty; cout<<"enter the average price of each book:"; cin>>price; } void showdata()

Creative Minds

Sarath
{ cout<<"Text books:"<<endl; cout<<"Number of books:"<<qty<<endl; cout<<"average price: "<<price<<endl; } friend refbooks; }; class refbooks { private: int qty; float price; public: void getdata() { cout<<"Enter the number of refbooks:"; cin>>qty; cout<<"enter the average price of each book:"; cin>>price; } void showdata() { cout<<"ref books:"<<endl; cout<<"Number of books:"<<qty<<endl; cout<<"average price: "<<price<<endl; } float totalval(textbooks t1) { float ans; ans=qty*price + t1.qty * t1.price; return ans; } };

www.creativeminds.co.cc

void main() { clrscr(); textbooks t1; refbooks r1; float total; t1.getdata();

Creative Minds

Sarath
r1.getdata(); total=r1.totalval(t1); cout<<"total value of books = Rs. "<<total<<endl; getch(); }

www.creativeminds.co.cc

Output:

Enter the number of textbooks:10 enter the average price of each book:100 Enter the number of refbooks:10 enter the average price of each book:100 total value of books = Rs. 2000

Creative Minds

Das könnte Ihnen auch gefallen