You are on page 1of 20

Aim: To form a simple calculator. Code: #include<iostream.h> #include<conio.

h> void main() { Clrscr(); float a,b,c,d,f; cout<< Enter any two numbers: ; cin>>a>>b; c=a+b; d=a-b; e=a*b; f=a/b; cout<< The addition of the two number is: << c <<endl; cout<< The subtraction of the tow number is: << d <<endl; cout<< The multiplication of the tow number is: << e <<endl; cout<< The division of the tow number is: << f <<endl; getch(); }

Output: Enter any two numbers: 7 1 The addition of the two number is: 8 The subtraction of the tow number is: 6 The multiplication of the tow number is: 7 The division of the tow number is: 7

Aim: To know whether the number is odd or even. Code: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a; cout<<"Enter any integer: "; cin>>a; if ( a % 2 == 0 ) cout<<The entered integer is even; else cout<<The entered integer is odd; getch(); } Output: Enter any integer: 4 The entered integer is even

Aim: To check whether the number is prime or not. Code: #include<iostream.h> #include<conio.h> #include<process.h> int main() { clrscr(); int num, i ; cout << "\n Enter the number: " ; cin >> num ; for(i = 2 ; i <= num/2 ;++i) { If ( num % i == 0 ) cout << "\n Not a prime number !!!"; exit(0); } cout <<"\n It is a prime number." ; getch(); } Output: Enter the number: 21 Not a prime number !!!

Aim: To print the factorial of a number. Code: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a; cout<< Enter a number: ; cin>>a; int c=a; for ( int i = 1; i <= a; i++) { c*=( a i ); if ( a i == 1 ) break; else continue; } cout<<"The factorial of " <<a<< "is "<< c; getch(); } Output: Enter a number: 5 The factorial of 5 is 120

Practical No. 7 Aim: To print the reverse of a string. Code: #include<iostream.h> #include<conio.h> #include<string.h> char* reverse(char*); void main() { clrscr(); char *a; cout<<"Enter a number of 15 digits: "; cin>>a; reverse(a); cout<<"The reverse of the number is "<<a; getch();

} char* reverse(char *a) { strrev(a); return a; } Output: Enter a number of 15 digits: 0123456789abcde The reverse of the number is: edcba9876543210

Aim: To compare any two numbers and change the smaller number to zero. Codes: #include<iostream.h> #include<conio.h> void smallo(int &, int &); void main() { clrscr(); int a, b; cout << "Enter any two numbers a and b: "; cin >> a >> b; smallo(a,b); cout << The result is a = << a << and b = << b; getch(); } void smallo( int &x, int &y) { If ( x > y ) y = 0; else x = 0; } Output: Enter any two numbers a and b: 2 9 The result is a = 0 and b = 9

Aim: To compare any two numbers and set the larger numbers to its square. Codes: #include<iostream.h> #include<conio.h> int sqlarge(int &, int &); void main() { clrscr(); int a,b; cout<<"Enter any two numbers: "; cin>>a>>b; sqlarge(a,b); cout<<"The result is a ="<<a<<"and b ="<<b; getch(); } int sqlarge( int &x, int &y ) { If ( x > y )x = x*x; else y = y*y; } Output: Enter any two numbers: 1 2 The result is a =1 and b =4

Aim: To raise a number m to the power n. Code: #include<iostream.h> #include<conio.h> #include<math.h> int power(int &, int &); void main() { clrscr(); int a,b; cout<<"Enter a numbers m: "; cin>>a; cout<<"Enter a number n: "; cin>>b; cout<<"The result is "<<power(a,b); getch(); } int power(int &x,int &y) { return pow(x,y);

} Output: Enter a numbers m: 2 Enter a number n: 3 The result is 8

Aim: To count the number of words in a string as well as the number of characters other than a space. Codes: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { clrscr(); char str1[51]; int x=0,y,z=0; cout<<"Enter a string of length 50: "; cin.getline(str1,50); strcat(str1 , " "); for (int i=0;i<50;i++) { y=i+1; if (str1[i] == ' ') z+=1; else continue; } for (char j='a';j<='z';j++) { for (int k=0;k<=50;k++) { if (str1[k] == j) x+=1; else continue; }

} cout<<"Total number of words in the string are: "<<z<<endl; cout<<"Total number of character are: "<<x; getch(); } Output: Enter a string of length 50: shubham goel Total number of words in the string are: 2 Total number of character are: 11

Aim: To reverse a string i. ii. Codes: i. #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char str1[50],str2[50]; int a=0; cout<<"Enter a string: "; gets(str1); for(int l=0;l<=49;l++) { if(str1[l] != '\0')a++; else break; } for(int i=0,j=a;i<=a,j>=0;i++,j--) { str2[i]=str1[j]; } cout<<"The reverse of the string is"; for(int k=0;k<=a;k++)cout<<str2[k]; getch(); } Output: Enter a string: vikram meena The reverse of the string is aneem markiv using another array, without using another array

ii. #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char str1[50],str2[50]; int a=0; cout<<"Enter a string: "; gets(str1); for(int i=0;i<=49;i++) { if(str1[i] != '\0')a++; else break; } cout<<"The reverse of the string is"; for(int j=a-1;j>=0;j--) cout<<str[j]; getch(); } Output: Enter a string: vikramjeet meena The reverse of the string is aneem teejmarkiv

Aim: To check whether or not two arrays are identical. Codes: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<process.h> void main() { clrscr(); char str1[50],str2[50]; int a=1,b=0,c=0,d; cout<<" Enter the first string: "; gets(str1); cout<<"\n Enter the second string: "; gets(str2); for(int l=0;l<=49;l++) { if(str1[l] != '\0')b++; else break; } for(int k=0;k<=49;k++) { if(str2[k] != '\0')c++; else break; }

if(c>b) d=c; else d=b; for(int i=0;i<d;i++) { if(str1[i]==str2[i])continue; else a=2;break; } switch(a) { case 1: cout<<"\n The strings are identical";break; case 2: cout<<"\n The strings are not identical"; } getch(); } Output: Enter the first string: Vikram Meena Enter the second string: Vikram Meena The strings are identical

Aim: To print the mean entirely of uppercase and lowercase letters. Codes: #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char str1[50]; float u=0,l=0,w=0,a; cout<<"Enter a string: "; gets(str1); for(int i=0;i<50;i++) { if(str1[i] != '\0')w++; else break; } for(char j='a';j<='z';j++) { for(int k=0;k<=w;k++) { if(str1[k]==j)l++; else continue; } } for(char m='A';m<='Z';m++) { for(int n=0;n<=w;n++)

{ if(str1[n]==m)u++; else continue; } } cout<<"Total number of uppercase letters are "<<u<<endl; cout<<"Total number of lowercase letters are "<<l<<endl; a=(u+l)/2; cout<<"The average of uppercase and lowercase letters is: "<<a; cout<<"\n."; getch(); } Output: Enter a string: Vikram Meena Total number of uppercase letters are 2 Total number of lowercase letters are 9 The average of uppercase and lowercase letters is: 5.5 .

Aim: To add, subtract, multiply and divide two complex numbers. Codes: #include<iostream.h> #include<conio.h> struct complex { float x,y; } c1, c2, add, sub, multiply, divide; void main() { clrscr(); cout<<"Enter the real part of the first complex number "; cin>>c1.x; cout<<"Enter the imaginary part of the first complex number "; cin>>c1.y; cout<<"Enter the real part of the second complex number "; cin>>c2.x; cout<<"Enter the imaginary part of the second complex number "; cin>>c2.y; add.x=c1.x+c2.x; add.y=c1.y+c2.y; sub.x=c1.x-c2.x; sub.y=c1.y-c2.y; multiply.x=(c1.x*c2.x)-(c1.y*c2.y); multiply.y=(c1.x*c2.y)+(c2.x*c1.y); divide.x=((c1.x*c2.x)+(c1.y*c2.y))/((c2.x*c2.x)+(c2.y*c2.y));

divide.y=((c1.x*c2.y)-(c1.y*c2.x))/((c2.x*c2.x)+(c2.y*c2.y)); cout<<"Adding both the complex numbers we get "; cout<<add.x<<"+("<<add.y<<"i)"<<endl; cout<<"Subtracting both the complex numbers we get "; cout<<sub.x<<"+("<<sub.y<<"i)"<<endl; cout<<"Multiplying both the complex numbers we get "; cout<<multiply.x<<"+("<<multiply.y<<"i)"<<endl; cout<<"Dividing both the complex numbers we get "; cout<<divide.x<<"-("<<divide.y<<"i)"; getch(); } Output: Enter the real part of the first complex number 2 Enter the imaginary part of the first complex number 1 Enter the real part of the second complex number 1 Enter the imaginary part of the second complex number 1 Adding both the complex numbers we get 3+(2i) Subtracting both the complex numbers we get 1+(0i) Multiplying both the complex numbers we get 1+(3i) Dividing both the complex numbers we get 1.5-(0.5i)

Aim: To print the Gross and Net salary of an employee Codes: #include<iostream.h> #include<conio.h> #include<stdio.h> struct info { int hra_rate, da_rate, cca, pf_rate, it_rate, days_worked; }; struct emp { int empno; char name [21]; double basic; info others; }semp; void main() { clrscr(); double gross_salary, net_salary; semp.others.hra_rate=30; semp.others.da_rate=6; semp.others.cca=300; semp.others.pf_rate=10; semp.others.it_rate=20; cout<<"Enter employee number "; cin>>semp.empno; cout<<"Enter employee name ";

gets(semp.name); cout<<"Enter the basic of the employee "; cin>>semp.basic; cout<<"Enter number of days worked "; cin>>semp.others.days_worked; gross_salary = ( ((semp.basic*semp.others.days_worked)/26) + ((semp.basic*semp.others.hra_rate)/100) + ((semp.basic*semp.others.da_rate)/100) + semp.others.cca ); net_salary = gross_salary - ( ((semp.basic*semp.others.it_rate)/100) + ((semp.basic*semp.others.pf_rate)/100) ); cout<<"The gross salary of the employee is "<<gross_salary<<endl; cout<<"The net salary of the employee is "<<net_salary; getch(); } Output: Enter employ number 1 Enter employ name shubham Enter the basic of the employee 100000 Enter number of days worked 298 The gross salary of the employee is 1182453.846154 The net salary of the employee is 1152453.846154

You might also like