0% found this document useful (0 votes)
185 views67 pages

C++ Programs

This document contains 24 programs written by Jaganpreet Singh. Each program is preceded by a roll number and includes the C++ code to solve a programming problem, such as calculating the area of a circle or determining if a number is prime. After each code example is the output for the program and a footer noting the computer application department. The programs cover a range of basic programming concepts like loops, functions, conditional statements and arrays.

Uploaded by

Jaganpreet Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
185 views67 pages

C++ Programs

This document contains 24 programs written by Jaganpreet Singh. Each program is preceded by a roll number and includes the C++ code to solve a programming problem, such as calculating the area of a circle or determining if a number is prime. After each code example is the output for the program and a footer noting the computer application department. The programs cover a range of basic programming concepts like loops, functions, conditional statements and arrays.

Uploaded by

Jaganpreet Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 67

Name: Jaganpreet Singh

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

DEPARTMENT OF COMPUTER APPLICATION

01

Name: Jaganpreet Singh

Roll no:

2. Write a program to find the circumference of a circle


#include<iostream.h> #include<conio.h> void main() { clrscr(); int r; float cir; cout<<"enter the radius of circle"; cin>>r; cir=2*22/7*r; cout<<"the circumference of circle is="<<cir; getch(); } Output:

DEPARTMENT OF COMPUTER APPLICATION

02

Name: Jaganpreet Singh

Roll no:

3. Write a program to whether know a number is even or odd

#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:-

DEPARTMENT OF COMPUTER APPLICATION

03

Name: Jaganpreet Singh

Roll no:

4. Write a program to find Fibonacci series

#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

Name: Jaganpreet Singh

Roll no:

z = x+y; cout<<z; x=y; y=z; } Output is :-

DEPARTMENT OF COMPUTER APPLICATION

05

Name: Jaganpreet Singh

Roll no:

5. Write a program to find Factorial of a given number

#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(); }

DEPARTMENT OF COMPUTER APPLICATION

06

Name: Jaganpreet Singh

Roll no:

6. Write a program to find whether a year is leap year or not.

#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(); }

DEPARTMENT OF COMPUTER APPLICATION

07

Name: Jaganpreet Singh

Roll no:

output is :-

DEPARTMENT OF COMPUTER APPLICATION

08

Name: Jaganpreet Singh

Roll no:

7. Write a program to find whether a number is prime or not

#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

Name: Jaganpreet Singh

Roll no:

output is:-

DEPARTMENT OF COMPUTER APPLICATION

10

Name: Jaganpreet Singh

Roll no:

8. Write a program to swap two numbers using third variable

#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(); }

DEPARTMENT OF COMPUTER APPLICATION

11

Name: Jaganpreet Singh

Roll no:

output is:-

DEPARTMENT OF COMPUTER APPLICATION

12

Name: Jaganpreet Singh

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); } }

int main() { unsigned int i, j=0;

cout<<"\n Enter the fibonnaci number : "; cin>>i;


DEPARTMENT OF COMPUTER APPLICATION 13

Name: Jaganpreet Singh

Roll no:

for(j=1; j<=i; j++) cout<<fibonacci(j)<<" ";

getch(); }

DEPARTMENT OF COMPUTER APPLICATION

14

Name: Jaganpreet Singh

Roll no:

10. Write a program to show the use of break statement

#include<iostream.h> #include<conio.h> main() { clrscr();

cout<<"\n ******** Break Statement *********"<<endl;

for(int count=1;count<=10;count++) { if(count==5) break;

cout<<" "<<count; }

cout<<"\n\n Counting was broken at count = "<<count<<endl; getch(); return 0; }


DEPARTMENT OF COMPUTER APPLICATION 15

Name: Jaganpreet Singh

Roll no:

output is:-

DEPARTMENT OF COMPUTER APPLICATION

16

Name: Jaganpreet Singh

Roll no:

11. Write a program to print the table of 5 ?


#include<iostream.h> #include<conio.h> void main() { int m,n,j; clrscr(); cout<<"enter the required number="; cin>>m; for(j=1;j<=10;j++) { n=m*j; cout<<"\t"<<m<<"*"<<j<<"="<<n<<"\n"; } cout<<"\n"; getch();

DEPARTMENT OF COMPUTER APPLICATION

17

Name: Jaganpreet Singh

Roll no:

Output is:

DEPARTMENT OF COMPUTER APPLICATION

18

Name: Jaganpreet Singh

Roll no:

12. Write a program to convert the temperature from degree Celsius to

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:

DEPARTMENT OF COMPUTER APPLICATION

19

Name: Jaganpreet Singh

Roll no:

13. Write a program to add two numbers

#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:

DEPARTMENT OF COMPUTER APPLICATION

20

Name: Jaganpreet Singh

Roll no:

14. Write a program to find factorial of a number using for loop

#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); }

DEPARTMENT OF COMPUTER APPLICATION

21

Name: Jaganpreet Singh

Roll no:

output is:-

DEPARTMENT OF COMPUTER APPLICATION

22

Name: Jaganpreet Singh

Roll no:

15. Write a program to find factorial of a number using while loop

#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

Name: Jaganpreet Singh

Roll no:

Output:

DEPARTMENT OF COMPUTER APPLICATION

24

Name: Jaganpreet Singh

Roll no:

16. Write a program to calculate the sum of n natural numbers

#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(); }

DEPARTMENT OF COMPUTER APPLICATION

25

Name: Jaganpreet Singh

Roll no:

output is :-

DEPARTMENT OF COMPUTER APPLICATION

26

Name: Jaganpreet Singh

Roll no:

17. Write a program to calculate the sum of n odd numbers

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

void main() { clrscr(); int n,i, sumodd=0; cout<<"Enter value of n:"; cin>>n;

for(i=1;i<=n;++i) { sumodd+=i; } cout<<"\nSUm of odd Numbers is "<<sumodd; getch(); }

DEPARTMENT OF COMPUTER APPLICATION

27

Name: Jaganpreet Singh

Roll no:

output is: -

DEPARTMENT OF COMPUTER APPLICATION

28

Name: Jaganpreet Singh

Roll no:

18. Write a program to find the reverse of the given number

#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 :-

DEPARTMENT OF COMPUTER APPLICATION

29

Name: Jaganpreet Singh

Roll no:

DEPARTMENT OF COMPUTER APPLICATION

30

Name: Jaganpreet Singh

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(); }

DEPARTMENT OF COMPUTER APPLICATION

31

Name: Jaganpreet Singh

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

Name: Jaganpreet Singh

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

Name: Jaganpreet Singh

Roll no:

output is :-

DEPARTMENT OF COMPUTER APPLICATION

34

Name: Jaganpreet Singh

Roll no:

21. Write a program to find whether the number is palindrome or not.

#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();}

DEPARTMENT OF COMPUTER APPLICATION

35

Name: Jaganpreet Singh

Roll no:

output is :-

DEPARTMENT OF COMPUTER APPLICATION

36

Name: Jaganpreet Singh

Roll no:

22. Write a program to find the sum of digits of a number

#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(); }

DEPARTMENT OF COMPUTER APPLICATION

37

Name: Jaganpreet Singh

Roll no:

output is:-

DEPARTMENT OF COMPUTER APPLICATION

38

Name: Jaganpreet Singh

Roll no:

23. Write a program to show the * start pattern

#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(); }

DEPARTMENT OF COMPUTER APPLICATION

39

Name: Jaganpreet Singh

Roll no:

output is: -

DEPARTMENT OF COMPUTER APPLICATION

40

Name: Jaganpreet Singh

Roll no:

24. Write a program to show the use of static storage class


#include<iostream.h> #include<conio.h> void incr(); void main() { clrscr(); int a; for(a=0;a<3;a++) incr(); getch(); } void incr() { int i=0; static int j; i++ ; j++; cout<<"i="<<i<<endl; cout<<"j="<<j; }
DEPARTMENT OF COMPUTER APPLICATION 41

Name: Jaganpreet Singh

Roll no:

25. Write a program to show the number pattern


#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j,n; cout<<"enter a no:"; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { cout<<i; } cout<<"\n"; } getch(); }

DEPARTMENT OF COMPUTER APPLICATION

42

Name: Jaganpreet Singh

Roll no:

output is:-

DEPARTMENT OF COMPUTER APPLICATION

43

Name: Jaganpreet Singh

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

Name: Jaganpreet Singh

Roll no:

} }; void main() { swapno s; s.swap(); s.display(); } output is:-

DEPARTMENT OF COMPUTER APPLICATION

45

Name: Jaganpreet Singh

Roll no:

27. Write a program to show the use of switch statement

#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

Name: Jaganpreet Singh

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(); }

DEPARTMENT OF COMPUTER APPLICATION

47

Name: Jaganpreet Singh

Roll no:

28. Write a program to find the sum of matrix

#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

Name: Jaganpreet Singh

Roll no:

} } void add() { for(i=0;i<=n;i++) { for(j=0;j<=n;j++) { sum=sum+a[i][j]; } } }

void result() { cout<<"the sum of matrix is :"<<sum; } }; void main() { matrix m; clrscr();
DEPARTMENT OF COMPUTER APPLICATION 49

Name: Jaganpreet Singh

Roll no:

m.get(); m.in(); m.add(); m.result(); getch();

} output is :-

DEPARTMENT OF COMPUTER APPLICATION

50

Name: Jaganpreet Singh

Roll no:

29. Write a program to print the following 1 21 321 4321


#include<iostream.h> #include<conio.h> void main() { int n=4,i,j; clrscr(); for(i=1;i<=n;i++) { for(j=i;j>=1;j--) { cout<<j<<" "; } cout<<"\n"; } getch(); } output is:-

DEPARTMENT OF COMPUTER APPLICATION

51

Name: Jaganpreet Singh

Roll no:

30. Write a program to print the following 1 22 333 4444


#include<iostream.h> #include<conio.h> void main() { int n=4,i,j; clrscr(); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { cout<<i<<" "; } cout<<"\n"; } getch(); } output is:-

DEPARTMENT OF COMPUTER APPLICATION

52

Name: Jaganpreet Singh

Roll no:

DEPARTMENT OF COMPUTER APPLICATION

53

Name: Jaganpreet Singh

Roll no:

31. Write a program to show the use of sizeof operator?


#include<iostream.h> #include<conio.h> void main() { int a=4,b; b=sizeof(a); cout<<a is <<a; cout<<size of a is = <<b; getch(); } output is :-

DEPARTMENT OF COMPUTER APPLICATION

54

Name: Jaganpreet Singh

Roll no:

32. Write a program to show the use of increment/decrement operator?


#include<iostream.h> #include<conio.h> void main() { int a,b,c,d; cout<<enter the value of a & c; cin>>a<<c; b=a++; d=c--; cout<<a is <<a<<endl; cout<<b is <<b<<endl; cout<<c is <<c<<endl; cout<<d is <<d<<endl; getch(); } output is :-

DEPARTMENT OF COMPUTER APPLICATION

55

Name: Jaganpreet Singh

Roll no:

33. Write a progam to show the use of bitwise operator?


#include<iostream.h> #include<conio.h> void main() { int a; cout<<enter the value of a; cin>>a; a=a>>2; cout<<a is =<<a; getch(); } output is :-

DEPARTMENT OF COMPUTER APPLICATION

56

Name: Jaganpreet Singh

Roll no:

34. Write a program to show the use of relational operator?


#include<iostream.h> #include<conio.h> void main() { int a,b; cout<<enter the value of a & b; cin>>a>>b; if(a==b) cout<<value of a & b is equal; else cout<<values are not equal; getch(); } output is :-

DEPARTMENT OF COMPUTER APPLICATION

57

Name: Jaganpreet Singh

Roll no:

35. Write a program to show the use of logical operator?


#include<iostream.h> #include<conio.h> void main() { int a,b,c; cout<<enter the value of a & b & c; cin>>a>>b>>c; if(a>b && a>c) { cout<<a is greatest no. among three no.; cout<<a; } getch(); } output is :-

DEPARTMENT OF COMPUTER APPLICATION

58

Name: Jaganpreet Singh

Roll no:

36. Write a program to show the use of return statement?


#include<iostream.h> #include<conio.h> int sum(int,int) void main() { int a,b,c; cout<<enter the values of a & b; cin>>a>>b; c=sum(a,b); cout<<sum is=<<c; getch(); } int sum(int a,int b) { int d; d=a+b; return(d); } output is :-

DEPARTMENT OF COMPUTER APPLICATION

59

Name: Jaganpreet Singh

Roll no:

37. Write a program to show the use of continue statement? 38.


#include<iostream.h> #include<conio.h> void main() { int n,i; cout<<enter the number; cin>>n; for(i=1;i<=n;i++) { if(i==5) continue; cout<<i<<endl; } getch(); } output is :-

DEPARTMENT OF COMPUTER APPLICATION

60

Name: Jaganpreet Singh

Roll no:

39. Write a program to show the use of break statement ?


#include<iostream.h> #include<conio.h> void main() { int n,i; cout<<enter the number; cin>>n; for(i=1;i<=n;i++) { if(i==5) break; cout<<i<<endl; } getch(); } output is:-

DEPARTMENT OF COMPUTER APPLICATION

61

Name: Jaganpreet Singh

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:-

DEPARTMENT OF COMPUTER APPLICATION

62

Name: Jaganpreet Singh

Roll no:

41. Write a program to find factorial of number using do-while loop ?


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

DEPARTMENT OF COMPUTER APPLICATION

63

Name: Jaganpreet Singh

Roll no:

42. Write a program of show the use of conditional operator ?


#include<iostream.h> #include<conio.h> void main() { int a,b,c; cout<<enter the value of a & b; cin>>a>>b; c=(a>b)?a-b:a+b cout<<result is =<<c; getch(); } output is :-

DEPARTMENT OF COMPUTER APPLICATION

64

Name: Jaganpreet Singh

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 :

DEPARTMENT OF COMPUTER APPLICATION

65

Name: Jaganpreet Singh

Roll no:

44. Write a program to show the concept of array


#include<iostream.h> #include<conio.h> class student { int roll no; int age; public: void getdata(); void putdata(); } void student getdata() { cout<<"enter the rollno and age"; cin>>rollno>>age; } void student.putdata() { cout<<"roll no="<<rollno<<endl; cout<<"age="<<endl; }
DEPARTMENT OF COMPUTER APPLICATION 66

Name: Jaganpreet Singh

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(); }

DEPARTMENT OF COMPUTER APPLICATION

67

You might also like