c tutorial

Sample – Program to enter three integers and output the smallest integer using IF.

February 8, 2011

#include <iostream.h> #include <conio.h> int main() { clrscr(); int x,y,z,smallest; cout << “Enter 3 integers : “; cin>>x>>y>>z; smallest=x; if(y<smallest) smallest=y; if(z<smallest) smallest=z; cout << “The smallest integer out of the 3 integers you typed “; cout << x << “, ” << y << ” & ” << z << ” is : ” [...]

Read the full article →

Sample – Program to find the sum of either of the diagonals of a 4 x 4 matrix.

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); int x; int A[4][4],sum=0; //Reading the matrix. cout << “Enter the elements of the matrix : ” << endl; for(int y=0;y<4;y++) for (int x=0;x<4;x++) { cout << “Element ” << x+1 << “, ” << y+1 << ” : “; cin>>A[x][y]; } //Sum of either of the [...]

Read the full article →

Sample – Program to enter 10 integers in a single-dimension array and then print out the array in ascending order.

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); int array[10],t; for(int x=0;x<10;x++) { cout << “Enter Integer No. ” << x+1 << ” : ” << endl; cin>>array[x]; } for (x=0;x<10;x++) { for(int y=0;y<9;y++) { if(array[y]>array[y+1]) { t=array[y]; array[y]=array[y+1]; array[y+1]=t; } } } cout << “Array in ascending order is : “; for (x=0;x<10;x++) cout [...]

Read the full article →

Sample – Program to enter a sentence and output the number of uppercase & lowercase consonants, uppercase & lowercase vowels in sentence..

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); char line[80]; int number_of_vowels,uc,lc,uv,lv; uc=lc=uv=lv=0; cout << “Enter your sentence : ” << endl; cin.getline(line,80); for(int x=0; line[x]!=’\0′;x++) { if(line[x]==’A’||line[x]==’E’||line[x]==’I’||line[x]==’O’||line[x]==’U’) uv++; else if(line[x]==’a’||line[x]==’e’||line[x]==’i’||line[x]==’o’||line[x]==’u’) lv++; else if(line[x]>+65&&line[x]<=90) uc++; else if (line[x]>=97&&line[x]<=122) lc++; } //Printing the output. cout << “Uppercase Consonants = ” << uc << “.” << endl; [...]

Read the full article →

Sample – Program to convert temperatures from Celsius to Fahrenheit and vice versa.

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); int choice; float ctemp,ftemp; cout << “1.Celsius to Fahrenheit” << endl; cout << “2.Fahrenheit to Celsius” << endl; cout << “Choose between 1 & 2 : ” << endl; cin>>choice; if (choice==1) { cout << “Enter the temperature in Celsius : ” << endl; cin>>ctemp; ftemp=(1.8*ctemp)+32; cout [...]

Read the full article →

Sample – Program to print the first 10 lines of pascal’s triangle.

February 8, 2011

#include <iostream.h> #include <conio.h> #include <iomanip.h> long triangle(int x,int y); int main() { clrscr(); const lines=10; for (int i=0;i<lines;i++) for (int j=1;j<lines-i;j++) cout << setw(2) << ” “; for (int j=0;j<=i;j++) cout << setw(4) << triangle(i,j); cout << endl; getch(); } long triangle(int x,int y) { if(x<0||y<0||y>x) return 0; long c=1; for (int i=1;i<=y;i++,x–) c=c*x/i; [...]

Read the full article →

Sample – Program to enter a character and output its ASCII code.

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); char charac; cout << “Enter the character : ” << endl; cin>>charac; int num1=charac; cout << “The ASCII code for ” << charac << ” is ” << num1 << “.” << endl; getch(); } This program takes in any character charac as a screen input from [...]

Read the full article →

Sample – Program to enter a letter and output the next 2 letters.

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); char charac; cout << “Enter your letter : ” << endl; cin>>charac; cout << “The 2 letters are : ” << endl; cout << (char)(charac-1) << endl; cout << (char)(charac+1) << endl; getch(); } This program takes in a letter charac of the English alphabet as a [...]

Read the full article →

Sample – Program to identify if an input is a symbol, digit or character.

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); char charac; cout << “Enter your input : ” << endl; cin>>charac; if(((charac>=’A’)&&(charac<=’Z’))||((charac>=’a’)&&(charac<=’z’))) cout << “Your input ” << charac << ” is a character.” << endl; else if((charac>=’0′)&&(charac<=’9′)) cout << “Your input ” << charac << ” is a digit.” << endl; else cout << “Your [...]

Read the full article →

Sample – Program to compute the fibonacci series.

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); int a,b,x,y,num1,ct; a=0; b=1; cout << “Enter the number of terms (less than 25) : ” << endl; cin>>num1; cout << a << endl; cout << b << endl; for(ct=1;ct<=num1-2;ct++) { x=a+b; cout << x << endl; y=a; a=b; b=x; } getch(); } This program takes in [...]

Read the full article →

Sample – Program to find the total days in the year till date.

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); int day,month,total; int days_per_month[12]={31,28,31,30,31,30,31,31,30,31,30,31}; cout << “Enter the month : ” << endl; cin>>month; cout << “Enter the day : ” << endl; cin>>day; total=day; for(int x=0;x<month-1;x++) total+=days_per_month[x]; cout << “The number of days in this year till date = ” << total << endl; getch(); } [...]

Read the full article →

Sample – Program to convert days into years and weeks.

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); int days,years,weeks,num1; cout << “Enter the number of days : ” << endl; cin>>days; years=days/365; num1=days-(years*365); weeks=days/7; num1=days-(weeks*7); cout << days << ” days = ” << endl; cout << weeks << ” weeks OR ” << endl; cout << years << ” years.” << endl; getch(); [...]

Read the full article →

Sample – Program to convert 2-digit octal number into binary number and print it.

February 8, 2011

#include <iostream.h> #include <conio.h> void octobin(int); void main() { clrscr(); int a; cout << “Enter a 2-digit octal number : “; cin>>a; octobin(a); getch(); } void octobin(int oct) { long bnum=0; int A[6]; //Each octal digit is converted into 3 bits, 2 octal digits = 6 bits. int a1,a2,quo,rem; a2=oct/10; a1=oct-a2*10; for(int x=0;x<6;x++) { A[x]=0; [...]

Read the full article →

Sample – Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..

February 8, 2011

#include <iostream.h> #include <conio.h> int main() { clrscr(); int A[10][10],m,n,x,y,sum=0; //Create a Matrix A cout << “Enter number of rows and columns in Matrix A : \n”; cin>>n>>m; cout << “Enter elements of Matrix A : \n”; for(x=1;x<n+1;++x) for(y=1;y<m+1;++y) cin>>A[x][y]; //Find sum of each row for(x=1;x<n+1;++x) { A[x][m+1]=0; for(y=1;y<m+1;++y) A[x][m+1]=A[x][m+1]+A[x][y]; } //Find sum of each [...]

Read the full article →

Sample – Program to enter an integer and print its total value based on the formula ‘x – 1/3!x3 + 1/5!x5 – 1/7!x7 + 1/9!x9′.

February 8, 2011

#include <iostream.h> #include <conio.h> #include <math.h> int main() { clrscr(); float factorial=1; float num,tot,term,total; int i,n=20,index,j=1; cout << “Enter a single-digit integer : \n”; cin>>num; tot=num; total=num; for(i=2,index=3;i<=n;i++,index+=2) { for(j=1,factorial=1;j<=index;j++) factorial*=j; tot=tot*pow((double)(-1),(double)(2*i-1))*num*num; term=tot/factorial; total+=term; } cout << “Total = ” << total << endl; getch(); return 0; } This program takes in an integer num [...]

Read the full article →

Sample – Program to enter an integer and output it in the reversed form.

February 8, 2011

#include <iostream.h> #include <conio.h> void main() { clrscr(); long int num1,num2,rnum=0; cout << “Enter an integer : ” << endl; cin>>num1; num2=num1; do { rnum=rnum*10; int digit=num1%10; rnum+=digit; num1/=10; } while(num1); cout << “The integer you typed is ” << num2 << “.” << endl; cout << “The reversed integer is ” << rnum << [...]

Related Posts Plugin for WordPress, Blogger...
Read the full article →