Sei sulla pagina 1di 18

// Write a program To find the sum of two Numbers display its result

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,c; cout<<"ENTER FIRST NUMBER = "; cin>>a; cout<<"ENTER SECOND NUMBER = "; cin>>b; c=a+b; cout<<"THE RESULT IS = "<< c; getch(); }

OUTPUT ENTER FIRST NUMBER = 5 ENTER SECOND NUMBER = 6 THE RESULT IS = 11

// Write a program To print the days of the week using switch


#include<iostream.h> #include<conio.h> void main() { clrscr(); char v; cout<<"ENTER THE NUMBER FOR DAYS IN A WEEK : "; cin>>v; switch(v) { case '1': cout<<"SUNDAY "; break; case '2': break; case '3: break; case '4': break; case '5': break; cout<<" MONDAY "; cout<<"TUESDAY "; cout<<" WEDNESDAY "; cout<<"THURSDAY";

case '6': break; case '7':

cout<<"FRIDAY"; cout<<"SATURDAY";

break; default: cout<<"NO MATCH"; } getch(); }

OUTPUT ENTER THE NUMBER FOR DAYS IN A WEEK : 1 SUNDAY

// Program to find greatest among four numbers.


#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,c,d; int large; cout<<"Enter A = "; cin>>a; cout<<"Enter B = "; cin>>b; cout<<"Enter C = "; cin>>c; cout<<"Enter D = "; cin>>d; if(a>b &&a>c &&a>d) large=a; else if(b>a &&b>c &&b>d) large=b; else if(c>a &&c>b &&c>d) large=c; else large=d; cout<<"THE LARGEST OF THE FOUR IS = "<<large; getch(); } Output Enter the numbers- a=2,b=7,c=4 & d=6 THE LARGEST OF THE FOUR IS b

// Write a program To print the fibonacci series


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,f1,f2,f3; cout<<"ENTER HOW MANY NUMBERS SERIES YOU NEED = "; cin>>n; f1=1; f2=1; cout<<f1<<endl; cout<<f2<<endl; int c=2; while(c<n) { f3=f1+f2; F1=f2; F2=f3; cout<<f3<<endl; c++; } getch(); }

OUTPUT ENTER HOW MANY NUMBERS SERIES YOU NEED =4 1123

// Write a program to print sum of two numbers & assin the value to a third variable.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,c,s; cout<<"Enter the values of any three numbers"; cin>>a>>b>>c; s=a+b; cout<<"The sum of the first two numbers is "<<s; c=s; cout<<"\nThe value of the new variable c is"<<c; getch(); } Output :Enter the values of any three numbers 5 4 6 The sum of the first two numbers is 9 The value of the new variable c is 9

// Program to swap values of two variables using call by value mechanism


#include<iostream.h> #include<conio.h> void swap(int x,int y) { int temp; temp=x; x=y; y=temp; cout<<"\nin functiona="<<x<<"b="<<y; } void main() { clrscr(); int a=5,b=7; cout<<"\nBefore funtion call a="<<a<<"b"<<b; swap(a,b); cout<<"\nfter funtion call a="<<a<<"b"<<b; getch(); } // Factorial calculator using recursive function. #include<iostream.h> #include<conio.h> long factorial (long a) { if (a > 1) return (a * factorial (a-1)); else

return (1); } void main () { long number; cout << "Please type a number: "; cin >> number; cout << number << "! = " << factorial (number); getch(); }

// Write a program to swap two numbers using functions.


#include<iostream.h> #include<conio.h> int main() { clrscr(); void swap(int,int); int a,b; a=7;b=4; cout<<"\nOriginal value are:"; cout<<"a="<<a<<",b="<<b<<"\n"; swap(a,b); cout<<"The values after swap are: \n"; cout<<"a="<<a<<",b="<<b<<"\n"; return 0; } void swap(int x,int y) { int z;z=x; x=y;y=z; cout<<"swapped values are:"; cout<<"a="<<x<<",b="<<y<<"\n"; } Output :Original value are: a=7 ,b=4 swapped values are: a=4 ,b=7

// Write a program to print the factorial of a number.


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,a=1; cout<<"enter the number whose factorial is required"; cin>>n; while(n>0) { a=a*n; n--; } cout<<"the factorial is"<<a; getch(); }

Output: enter the number whose factorial is required 5 the factorial is 120

// Write a program to check whether the number is a pelindrome.


#include<iostream.h> #include<conio.h> void main() {clrscr(); int n,r=0,z; cout<<"enter the number to b tested "; cin>>n; z=n; while(n>0) {r=r*10+n%10; n=n/10;} cout<<"the reverse of the number is"<<r; if (r==z) {cout<<"the number is a pelindrome"; }else {cout<<" the number is not a pelindrome"; }getch();} Output:enter the number to b tested 123 the reverse of the number is 321 the number is not a pelindrome

// Program to find whether the string is palindrome or not.


#include<iostream.h> #include<conio.h> void main() { clrscr(); char st[100]; int c=0,f=1,i,j; cout<<"enter the string"; cin.getline(st,100); for (int l=0;st[l]!='\0';l++) c=c+1; for (i=0,j=l-1;i<c/2;i++,j--) { if (st[i]!=st[j]) { f=0; break; } } if (f==1) cout<<" the string is palindrom"; else cout<<"the string is not palindrom"; getch();

// Write a program to enter information of employees class


#include<iostream.h> Class employee { Private: int empid; char name[30]; public: void enterdata() { Cout<<\n Enter the employee id:; Cin>>empid; Cout<<\n Enter employee name:; Cin>>name; } Void display() { Cout<<\n Employee ID : <<empid; Cout<<\n Employee name : <<name; } }; Void main() { Employee emp[5]; Int i; For(i=0;i<5;i++) emp [i].enterdata(); for(i=0;i<5;i++) emp[i].display(); }

Output Enter the employee id Enter employee name Enter the employee id Enter employee name Enter the employee id Enter employee name Enter the employee id Enter employee name Enter the employee id Enter employee name Employee Employee Employee Employee Employee Employee Employee Employee Employee Employee ID name ID name ID name ID name ID name : : : : : : : : : :

: : : : : : : : : :

1 Vishal 2 Harpreet 3 Gurdip 4 Sunil 5 Gaurav

1 Vishal 2 Harpreet 3 Gurdip 4 Sunil 5 Gaurav

//Write a program to show the concept of multiple inheritance.


#include<iostream.h> #include<conio.h> class sum { protected: int a,b,c; public: void enter() { cout<<"enter the first number:-"; cin>>a; cout<<"\n enter the second number:-"; cin>>b; } }; class sub { protected: int p,q,r; public: void input() { cout<<"\n enter the third number:-"; cin>>p;

cout<<"\n enter the fourth number:-"; cin>>q; } }; class result:public sum,public sub { public: void output() { cout<<"\n the sum of 1st and 2nd is:-"<<a+b; cout<<"\n\n the subtraction of 3rd and 4rth is:-"<<p-q; } }; void main() { clrscr(); result obj; obj.enter(); obj.input(); obj.output(); getch(); }

Output: Enter the first number: 2 Enter the second number: 1 Enter the third number :5 Enter the fourth number: 4 the sum of 1st and 2nd is:-3

// Program to find the product of two matrices.


# include <iostream.h> # include <conio.h> void main() { int mat1[10][10], mat2[10][10], mat3[10][10]; int i, j, k,n; clrscr(); cout<<"Enter the order of matrices\n"; cin>>n; cout << "Enter first matrix elements :\n"; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { cin >> mat1[i][j]; } } cout << "Enter second matrix elements :\n"; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { cin >> mat2[i][j]; } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { mat3[i][j]=0; for (k = 0; k < n; k++) {

mat3[i][j] += mat1[i][k] * mat2[k][j]; } } } cout << "The first matrix is :\n"; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { cout << mat1[i][j] << "\t"; } cout << "\n"; } cout<<"The second matrix is :\n"; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { cout << mat2[i][j]<< "\t"; } cout << "\n"; } cout<<"Product of the matrix is :\n"; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { cout << mat3[i][j] << "\t"; } cout << "\n"; } getch();

Potrebbero piacerti anche