C++ Programs
C++ Programs
Roll no:
1. Write a program to find the area of circle: #include<iostream.h> #include<conio.h> void main() { int r; float area; clrscr(); cout<<"enter the radius of circle"; cin>>r; area=22/7*(r*r); cout<<"area of circle is="<<area; getch(); } Output
01
Roll no:
02
Roll no:
#include<iostream.h> #include<conio.h> void main() { int n; cout<<"enter the number"; cin>>n; if(n%2==0) { cout<<"number is even"; } else { cout<<"number is odd"; }getch(); } output is:-
03
Roll no:
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a, b, c, n; cout<<"enter the range"; cin>>n; cout<<a<<"\t"<<b<<"\t"; a=0; b=1; cout<<a<<b; int fib(int,int); getch(); } int fib(int x, int y) { int i,z; for(i=1;i<=n-2,i++) {
DEPARTMENT OF COMPUTER APPLICATION 04
Roll no:
05
Roll no:
#include<iostream.h> #include<conio.h> void main() { int f,fact=1; clrscr(); cout<<"enter the no to find factorial"; cin>>f; for(int i=1;i<=f;i++) { fact=fact*i; } cout<<"factorial of number is="<<fact; getch(); }
06
Roll no:
#include<iostream.h> #include<conio.h> void main() { int n; clrscr(); cout<<"enter the no."; cin>>n; if(n%4==0) { cout<<"\n leap year"; } else { cout<<"\n not a leap year"; } getch(); }
07
Roll no:
output is :-
08
Roll no:
#include<iostream.h> #include<conio.h> void main() { int i,n; clrscr(); cout<<"enter the values of number"; cin>>n; for(i=2;i<=n;i++) { if(n%i==0) cout<<"number is not a prime"; else { cout<<"number is a prime"; } } getch(); }
DEPARTMENT OF COMPUTER APPLICATION 09
Roll no:
output is:-
10
Roll no:
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,c; cout<<"enter the no. which u want to swap"; cin>>a>>b; cout<<"before swaping numbers are "<<"a="<<a<<endl<<"b="<<b<<endl; c=a; a=b; b=c; cout<<"after swapping the numbers are "<<"a="<<a<<endl<<"b="<<b; getch(); }
11
Roll no:
output is:-
12
Roll no:
9. Write a program to find Fibonacci series using recursion #include <iostream.h> #include <conio.h> Void main() unsigned int fibonacci (unsigned int n) { if (n <= 2) { return 1; } else { return fibonacci(n-1) + fibonacci(n-2); } }
Roll no:
getch(); }
14
Roll no:
cout<<" "<<count; }
Roll no:
output is:-
16
Roll no:
17
Roll no:
Output is:
18
Roll no:
degree Fahrenheit
#include<iostream.h> #include<conio.h> void main() { float cen,f; cout<<"enter the value of tem. in degree centigrade"; cin>>cen; f=(1.8*cen)+32; cout<<"fahrenheit"<<f; getch(); } Output:
19
Roll no:
#include<iostream.h> #include<conio.h> void main() { int a,b,sum; cout<<"enter the value of a"; cin>>a; cout<<"enter the value of b"; cin>>b; sum=a+b+c; cout<<"sum is="<<sum; getch(); } Output:
20
Roll no:
#include<iostream.h> #include<conio.h> int main() { clrscr(); int i, n; int fact=1; cout<<"\nEnter the Value of n \n"; cin>>n; for(i=n ; i>=1 ; i--) fact = fact*i; cout<<"\nFactorial => "<<fact; getch(); return(0); }
21
Roll no:
output is:-
22
Roll no:
#include<iostream.h> #include<conio.h> void main() { int f,n,i,fact=1; clrscr(); cout<<"enter the number"; cin>>n; i=2; while(i<=n) { fact=fact*i; i=i+1; }cout<<"factorial of number is="<<n<<"is"<<fact; getch(); }
DEPARTMENT OF COMPUTER APPLICATION 23
Roll no:
Output:
24
Roll no:
#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,n,sum=0; cout<<"How many numbers? "; cin>>n; for(i=1;i<=n;++i) { sum+=i; cout<<"\nSUM="<<sum<<endl; } getch(); }
25
Roll no:
output is :-
26
Roll no:
#include<iostream.h> #include<conio.h>
void main() { clrscr(); int n,i, sumodd=0; cout<<"Enter value of n:"; cin>>n;
27
Roll no:
output is: -
28
Roll no:
#include<iostream.h> #include<conio.h> void main() { int a; cout<<"Enter the no:"; cin>>a; while(a!=0) { cout<<a%10; a=a/10; } getch(); } output is :-
29
Roll no:
30
Roll no:
19. Write a program to find the square root of the given number
#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); float sq,n; cout<<"Enter any number:"; cin>>n; sq=sqrt(n); cout<<"Square root of "<<n<<" is "<<sq; getch(); }
31
Roll no:
20. Write a program to find the sum of the diagonal elements of a matrix
#include<iostream.h> #include<conio.h> class sumdiag { int a[3][3],i,j; public: void get(); void sum(); }; void sumdiag::get() { cout<<"enter elements of matrix"; for(i=0;i<3;i++) { for (j=0;j<3;j++) { cin>>a[i][j]; } }
DEPARTMENT OF COMPUTER APPLICATION 32
Roll no:
} void sumdiag::sum() { int s=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(i==j) s=s+a[i][j]; } } cout<<"sum of diagonal elements are="<<s; } void main() { clrscr(); sumdiag ss; ss.get(); ss.sum(); getch(); }
DEPARTMENT OF COMPUTER APPLICATION 33
Roll no:
output is :-
34
Roll no:
#include<iostream.h> #include<conio.h> void main() { int n,b,rev=0,temp; clrscr(); cout<<"enter the value of n="; cin>>n; temp=n; while(n>0) { b=n%10; n=n/10; rev=rev*10+b;} if(temp==rev) cout<<"num is palindrome"<<rev<<endl; else cout<<"num is not palindrome"<<rev; getch();}
35
Roll no:
output is :-
36
Roll no:
#include<iostream.h> #include<conio.h> void main() { clrscr(); unsigned long i,p,n,sum=0; cout<<"Enter any number:"; cin>>n; while(n!=0) { p=n%10; sum+=p; n=n/10; } cout<<endl<<"Sum of its digits is:"<<sum; getch(); }
37
Roll no:
output is:-
38
Roll no:
#include<iostream.h> #include<conio.h> void main() { int i,j,n; clrscr(); cout<<"enter how many lines"; cin>>n; for(i=0;i<=n;i++) { for(j=0;j<=i;j++) { cout<<"*"; } cout<<"\n"; } getch(); }
39
Roll no:
output is: -
40
Roll no:
Roll no:
42
Roll no:
output is:-
43
Roll no:
26. Write a program to swap two numbers without using third variable
#include<iostream.h> #include<conio.h> class swapno { int a,b; public: void swap() { cout<<"Enter two numbers : "; cin>>a>>b; cout<<"Before swapping : "<<endl; cout<<"a="<<a<<"\n"<<"b="<<b; } void display() { a=a+b; b=a-b; a=a-b; cout<<"After swapping : "<<endl; cout<<"a="<<a<<endl<<"b="<<b;
DEPARTMENT OF COMPUTER APPLICATION 44
Roll no:
45
Roll no:
#include<iostream.h> #include<conio.h> void main() { int a; clrscr(); cout<<"enter the days no.=\n"; cin>>a; switch(a) { case 1: cout<<"sunday\n"; break; case 2: cout<<"monday\n"; break; case 3: cout<<"tuesday\n"; break;
DEPARTMENT OF COMPUTER APPLICATION 46
Roll no:
case 4: cout<<"wednesday\n"; break; case 5: cout<<"thursday\n"; break; case 6: cout<<"friday\n"; break; case 7: cout<<"saturday\n"; break; default: cout<<"error"; } getch(); }
47
Roll no:
#include<iostream.h> #include<conio.h> class matrix { int a[][],i,j,n,sum=0; public: void get() { cout<<"enter the value of n"; cin>>n; } void in() { for(i=0;i<=n;i++) { for(j=0;j<=n;j++) { cout<<"enter the matrix:"; cin>>a[i][j]; }
DEPARTMENT OF COMPUTER APPLICATION 48
Roll no:
void result() { cout<<"the sum of matrix is :"<<sum; } }; void main() { matrix m; clrscr();
DEPARTMENT OF COMPUTER APPLICATION 49
Roll no:
} output is :-
50
Roll no:
51
Roll no:
52
Roll no:
53
Roll no:
54
Roll no:
55
Roll no:
56
Roll no:
57
Roll no:
58
Roll no:
59
Roll no:
60
Roll no:
61
Roll no:
40. Write a program to swap the two no. using third variable?
#include<iostream.h> #include<conio.h> void main() { int a,b,c; clrscr(); cout<<"enter the value of a="; cin>>a; cout<<endl<<"enter th value of b="; cin>>b; c=a; a=b; b=c; cout<<endl<<"after the swapping the numbers"<<endl; cout<<a is =<<a<<endl<<b is =<<b; getch(); } output is:-
62
Roll no:
63
Roll no:
64
Roll no:
43. Write a program to print the function having return statement without arguments ?
#include<iostream.h> #include<conio.h> void main() { int tab; tab=table(); cout<<tab<<\n; getch(); } int table() { int i,t,n; cout<<enter the number; cin>>n; for(i=1;i<=n;i++) { t=n*i; return(t); } } output is :
65
Roll no:
Roll no:
void main() { students[5]; int i; cout<<"enter the following info"; for(i=0;i<5;i++) s[i].getdata(); s[i].putdata(); getch(); }
67