Sie sind auf Seite 1von 15

C++ PROGRAMS //Program to find and display the sum and average of two numbers #include<iostream.h> #include<conio.

h> void main() { int a, b, sum; float avg; cout<<Enter two numbers:; cin>>a>>b; sum=a+b; avg=sum/2; cout<<Sum of <<a<< and <<b<< is <<sum; cout<<\n Average is <<avg; getch(); } //Program to convert time in seconds into Hrs: Mins: Secs format #include<iostream.h> #include<conio.h> void main() { int time, hr, mts, sec; clrscr(); cout<<Enter the time in seconds:\t; cin>>time; hr=time/3600; time=time%3600; mts=time/60; sec=time%60; cout<<Time is \n\t<<hr<< Hrs. <<mts<< Mins. <<sec<< Secs.; getch(); }

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//Program to input the radius of a circle and to find its area and perimeter #include<iostream.h> #include<conio.h> void main() { const float pi=3.14; float r, area, peri; clrscr(); cout<<Enter the radius of the circle:\t; cin>>r; area=pi*r*r; peri=2*pi*r; cout<<Area of the circle is <<area<<\n; cout<<Perimeter of the circle is <<peri; getch(); } //Program to check whether a given number is even or odd #include<iostream.h> #include<conio.h> void main() { int num; clrscr(); cout<<Enter a number:\t; cin>>num; cout<<((num%2==0)?Even:Odd); getch(); } //Program to find the largest of three numbers #include<iostream.h> #include<conio.h> void main() { int a, b, c, big; clrscr(); cout<<Enter three integer numbers:\t; cin>>a>>b>>c;

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

big=(a>b)?((a>c)?a:c):((b>c)?b:c); cout<<Biggest number is <<big; getch(); } //Program to enter two numbers and display the larger number #include<iostream.h> #include<conio.h> void main() { int a,b,big; cout<<Enter two integer numbers\t; cin>>a>>b; if (a>b) big = a; else big = b; cout<< Larger number is <<big; getch(); return; } //Program to enter the day number and display the day of week #include<iostream.h> #include<conio.h> void main() { int n; cout<<Enter the day number\t; cin>>n; cout<<The name of day is ; switch(n) { case 1: cout<<Sunday; break; case 2: cout<<Monday; break; case 3: cout<<Tuesday; break; case 4: cout<<Wednesday; break; case 5: cout<<Thursday; break; case 6: cout<<Friday; break; case 7: cout<<Saturday; break; default: cout<<Invalid number;

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

} getch(); return; }

//Program to find the sum of digits of a number #include<iostream.h> #include<conio.h> void main() { int num, dup, rem, sum = 0; cout<< Enter an integer number\t; cin>>num; dup = num; while(dup>0) { rem=dup%10; dup=dup/10; sum=sum+rem; } cout<< Sum of the digits of <<num<< is <<sum; getch(); return; } //Program to find the HCF of two numbers #include<iostream.h> #include<conio.h> void main() { int a, b, hcf, small, i=1; cout<< Enter two integer numbers\t; cin>>a>>b; small=(a<b)?a:b; do { if (a%i==0 && b%i==0) hcf=i; i++; }while(i<=small); cout<< HCF of <<a<< and <<b<< is <<hcf; getch();

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

return; } //Program to check whether an year is leap year or not #include<iostream.h> #include<conio.h> void main() { int yr; cout<< Enter a year in four digits\t; cin>>yr; if((yr%4==0 && yr%100!=0)||(yr%400==0)) cout<< It is a leap year; else cout<< It is not a leap year; getch(); return; } //Program to find the sum of first N natural numbers #include<iostream.h> #include<conio.h> void main() { int i, n, sum=0; cout<< Enter N\t; cin>>n; for(i=1; i<=n; ++i) sum=sum+i; cout<< Sum of first <<n<< natural numbers is <<sum; return; }

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//Program to find the sum of cubes of N natural numbers #include<iostream.h> #include<conio.h> #include<math.h> //pow() is declared in math.h header file void main() { int n, i, sum=0; cout<< Enter N\n; cin>>n; for(i=1;i<=n;i++) sum=sum+pow(i,3); //pow(a,b) gives ab cout<< Sum of cubes of first <<n<< natural numbers is <<sum; getch(); } //Program to find the area of a triangle #include<iostream.h> #include<conio.h> #include<math.h> void main() { int a,b,c,large,diff; float s,area; cout<< Enter the three sides of a triangle\n; cin>>a>>b>>c; large=(a>b)?((a>c)?a:c):((b>c)?b:c); if(large==a) diff= (b+c)-large; else if(large==b) diff=( a+c)-large; else diff=(a+b)-large; if(diff>0) { s=(a+b+c)/2.0; area=sqrt(s*(s-a)*(s-b)*(s-c)); cout<< Area of the triangle is <<area; else cout<< Invalid sides; getch(); }

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//Program to find the roots of a quadratic equation #include<iostream.h> #include<conio.h> #include<math.h> void main() { int a,b,c,disc; float x; cout<< Enter the three coefficients a,b,c\n; cin>>a>>b>>c; disc=(b*b)-(4*a*c); if(disc==0) { x=(-b)/(2.0*a); cout<< Roots are real and equal\nRoot = <<x; } else if(disc>0) { float y; x=(-b-sqrt(disc)/(2.0*a)); y=(-b+sqrt(disc)/(2.0*a)); cout<< Roots are real and distinct\n Roots are <<x<< and <<y; } else cout<< Roots are imaginary; getch(); } //Program to check whether a number is prime or not #include<iostream.h> #include<conio.h> void main() { int i,n,flag=0; cout<< Enter a positive integer\n; cin>>n; for(i=2;i<=(n/2);i++) { if(n%i==0) { flag=1; break; } } if (flag==0) cout<<n<< is a prime number; else cout<<n<< is not a prime number; getch(); }

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//Program to display all the prime numbers between a range #include<iostream.h> #include<conio.h> void main() { int a,b,i,j,flag; cout<< Enter the limits\n; cin>>a>>b; cout<< The prime numbers between <<a<< and <<b<< are << \n; for(i=a; i<=b; i++) { flag=0; for(j=2; j<=(i/2); j++) { if(i%j==0) { flag=1; break; } } if(flag==0) cout<<i<< \t; } getch(); } //Program to generate Fibonacci series upto N terms #include<iostream.h> #include<conio.h> void main() { int previous, current, next, n, i; cout<< Enter the number of terms\t; cin>>n; previous=0; current=1; cout<< Fibonacci series with <<n<< terms: \n; for(i=1;i<=n;i++) { cout<<previous<< \t; next=previous+current; previous=current; current=next; } getch(); }

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//Program to check whether a number is palindrome or not #include<iostream.h> #include<conio.h> void main() { int rem, num, dup, rev=0; cout<< Enter an integer number\t; cin>>num; dup=num; while(dup>0) { rem=dup%10; rev=(rev*10)+rem; dup=dup/10; } if(rev==num) cout<<num<< is a palindrome number; else cout<<num<< is not a palindrome number; getch(); } //Program to check whether a number is Armstrong or not #include<iostream.h> #include<conio.h> void main() { int d,num,dup,sum=0; cout<< Enter an integer number; cin>>num; dup=num; while(dup>0) { d=dup%10; sum=sum+(d*d*d); dup=dup/10; } if(sum==dup) cout<<num<< is an Armstrong number; else cout<<num<< is not an Armstrong number; getch(); }

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//Program to convert binary number into its equivalent decimal number #include<iostream.h> #include<conio.h> #include<math.h> void main() { int deci=0,d,i=0; long int bino; cout<< Enter a binary number\t; cin>>bino; do { d=bino%10; deci=deci+d*pow(2,i); ++i; bino=bino/10; }while(bino>0); cout<< Decimal equivalent is <<deci; getch(); } //Program to find the largest and smallest number in an array #include<iostream.h> #include<conio.h> void main() { int num[20],n,i,max,min; cout<< Enter the size of the array\t; cin>>n; cout<< Enter the elements\n; for(i=0; i<n; ++i) cin>>num[i]; max=min=a[0]; for(i=1; i<n; ++i) { if (a[i]>max) max=a[i]; if (a[i]<min) min=a[i]; } cout<< The largest number is <<max<< \n; cout<< The smallest number is <<min; getch(); }

10

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//Program to count the number of vowels in a string #include<stdio.h> #include<iostream.h> #include<conio.h> void main() { char str[30]; int i, count=0; clrscr(); cout<< Enter a string\t; gets(str); for(i=0;str[i]!= \0; ++i) { switch(str[i]) { case a: case A: case e: case E: case i: case I: case o: case O: case u: case U: count++; } } cout<< Number of vowels = <<count; getch(); } //Program to find the sring length and count the number of words in a string #include<iostream.h> #include<conio.h> void main() { char line[80]; int words=1, size; cout<< Enter a line of text\n; gets(line); for(size=0; line[size]!= \0;size++); for(int i=0;i<size;i++) { if (line[i]==32) words++; } cout<< Length of string = <<size<< \n; cout<< Number of words = <<words<< \n; getch(); }

11

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//Program to reverse a string #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { char str[20], temp; int i, j, len; cout<< Enter a string \t; gets(str); len=strlen(str); for(i=0, j=len-1; i<len/2; ++i,--j) { temp=str[i]; str[i]=str[j]; str[j]=temp; } cout<< The reversed string is <<str; getch(); } //Program to convert the lower case letters of a string into upper case #include<iostream.h> #include<stdio.h> #include<conio.h> void main() { char word[20], ch; cout<< Enter a word :\t; gets(word); for(int i=0; word[i]!= \0;i++) { ch=word[i]; if (ch>= a && ch<= z) word[i]=ch-32; } cout<< The word in upper case is : <<word; getch(); }

12

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//MATRIX ADDITION #include<iostream.h> #include<conio.h> #include<process.h> void main() { int A[10][10], B[10][10], C[10][10], m, n, p,q,i, j; clrscr(); cout<< Enter the order of the matrix A\n; cin>>m>>n; cout<< \nEnter the order of the matrix B\n; cin>>p>>q; if (m!=p || n!=q) { cout<< Matrix addition is not possible\n; exit(0); } cout<< Enter the elements of matrix A\n; for(i=0;i<m;++i) for(j=0;j<n;++j) cin>>A[i][j]; cout<< Enter the elements of matrix B\n; for(i=0;i<m;++i) for(j=0;j<n;++j) cin>>B[i][j]; for(i=0; i<m; ++i) //Addition loop for(j=0; j<n; ++j) C[i][j]=A[i][j]+B[i][j]; cout<< The sum matrix is \n; for(i=0;i<m;++i) { for(j=0;j<n;++j) cout<<C[i][j]<< \t; cout<< \n; } getch(); }

13

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//MATRIX MULTIPLICATION #include<iostream.h> #include<conio.h> #include<process.h> void main() { int A[10][10], B[10][10], C[10][10], m, n, p,q,i, j; clrscr(); cout<< Enter the order of the matrix A\n; cin>>m>>n; cout<< \nEnter the order of the matrix B\n; cin>>p>>q; if (n!=p) { cout<< Matrix multiplication is not possible\n; exit(0); } cout<< Enter the elements of matrix A\n; for(i=0;i<m;++i) for(j=0;j<n;++j) cin>>A[i][j]; cout<< Enter the elements of matrix B\n; for(i=0;i<m;++i) for(j=0;j<n;++j) cin>>B[i][j]; for(i=0; i<m; ++i) //Multiplication loop for(j=0; j<q; ++j) for(C[i][j]=0,k=0;k<n;++k) C[i][j]=C[i][j]+(A[i][k]*B[k][j]); cout<< The product matrix is \n; for(i=0;i<m;++i) { for(j=0;j<q;++j) cout<<C[i][j]<< \t; cout<< \n; } getch(); }

14

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

//Program to find the largest element in a matrix and its position #include<iostream.h> #include<conio.h> void main() { int A[10][10], m, n, i, j, row=0, col=0; clrscr(); cout<< Enter the order of the matrix \n; cin>>m>>n; cout<< Enter the elements\n; for(i=0;i<m;++i) for(j=0;j<n;++j) cin>>A[i][j]; for(i=0;i<m;++i) for(j=0;j<n;++j) if (A[i][j]>A[row][col]) { row=i; col=j; } cout<< The largest element in the matrix \n; for(i=0;i<m;++i) { for(j=0;j<n;++j) cout<<A[i][j]<< \t; cout<< \n; } cout<< is <<A[row][col]; cout<< \n Its position is at Row = <<row+1<< Column = <<col+1; getch(); }

15

Remedial Classes On Computer Programming College of Engg, Trivandrum Classes conducted by Praveen R S & Organized by Prof. Baiju Sasidharan, Dept Of Mechanical Engg

Das könnte Ihnen auch gefallen