Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Example 1 :
#include<stdio.h>
#include<conio.h>
void main()
{
int avg=346;
float per=69.2;
printf("\nAverage : %d\nPercentage : %f",avg,per);
}
Example 2 :
#include<iostream.h>
#include<conio.h>
void main()
{
int sum=0,ch,i,j,num,no;
char ans='y';
clrscr();
cout<<"\nEnter a number : ";
cin>>no;
num=no;
do
{
sum=sum+(no%10)*(no%10)*(no%10);
no=no/10;
}
while(no!=0);
if(sum==num)
cout<<"\nThe number is an Armstrong number ";
else
cout<<"\nThe number is not an Armstrong number ";
getch();
}
{
int i , j , n = 1 ;
for(i=1 ; i<5 ;)
{
for (j=1 ; ; j++)
{
cout<<n<<" ";
n++ ;
if (j >= 5)
break;
}
i++;
}
}
Example 4 :
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0,j=6,k=0;
char x='A';
clrscr();
while((j/2)>=0)
{
for(i=0;i<=(j/2);i++)
cout<<" "<<char(x+i);
i=i-2;
for(;i>=0;i--)
cout<<" "<<char(x+i);
j=j-2;
k=k+1;
cout<<"\n";
for(i=0;i<=k;i++)
cout<<" ";
}
getch();
}
#include <iostream.h>
void main()
{
char c ;
cout<<"Enter any character :\n";
cin>>c ;
if ((c >= 'a') && (c <= 'z'))
cout<<"The character is lower case.\n";
else if ((c >= 'A') && (c <= 'Z'))
cout<<"The character is upper case.\n";
else if ((c >= '0') && (c <= '9'))
cout<<"The character is numeric.\n";
else
cout<<"The character is a symbol.\n";
}
Example 6 :
# include <iostream.h>
#include<conio.h>
void main ( )
{
char c ;
clrscr ( ) ;
cout<<"Enter any character :";
cin>>c;
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '9':cout<<"It is a digit ";
break ;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':cout<<"It is an alphabet ";
break ;
default :cout<<"It is a special character";
}
}
Example 7 :
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
for(k=1;k<=3;k++)
cout<<i<<" "<<j<<" "<<k<<endl;
getch();
}
Example 8 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,n;
char a[7];
clrscr();
cout<<"Enter the number : ";
cin>>a;
i=0;
while(a[i]!='\0')
{
if(a[i]==',')
{
int j=i;
while(a[j]!='\0')
{
a[j]=a[j+1];
j++;
}
}
else
i++;
}
cout<<"\nThe number after removing ',' is : "<<a;
getch();
}
}
}
}
Example 10 :
#include<iostream.h>
#include<conio.h>
void main()
{
int x, p = 0, count = 0;
clrscr();
cout<<"Enter a number : ";
cin>>x;
L1 : if (x%2==0)
x=x/2;
else
x=x*3+1;
p=x;
count=count+1;
if(p!=1)
goto L1;
else if (p==1)
cout<<"\nFunction converged to 1";
cout<<"\nNumber of iterations : "<<count;
getch();
}
Example 15 :
#include<stdio.h>
#include<conio.h>
void main()
{
int height=6;
clrscr();
printf("\nHeight is %d feet",height);
printf("\nHeight is %2d feet",height);
printf("\nHeight is %4d feet",height);
printf("\nHeight is %6d feet",height);
printf("\nHeight is %-6d feet",height);
getch();
}
Example 16 :
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n%f%f%f",5.0,13.5,133.9);
printf("\n%f%f%f",305.5,1200.9,3005.3);
getch();
}
Example 17 :
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n%10.1f%10.1f%10.1f",5.0,13.5,133.9);
printf("\n%10.1f%10.1f%10.1f",305.5,1200.9,3005.3);
getch();
}
#include <iostream.h>
void main()
{
int i , j ;
for (i=1 ; i<=3 ; i++)
/* Outer For Loop */
for (j=1 ; j<=2 ; j++)
/* Inner For Loop */
cout<<"Hello!"<<endl;
}
cin>>num;
if (num % 2 == 0)
cout<<"The number "<<num<<" is EVEN\n";
else
cout<<"The number "<<num<<" is ODD\n";
cout<<"Do you want to continue (Y or N) ? : " ;
cin>>ans;
if ((ans == 'y') || (ans == 'Y'))
goto again ;
else
cout<<"Thank You !!\n";
}
Example 36 : Program : The If-else statement - find minimum and maximum no.
#include <iostream.h>
#include<conio.h>
void main()
{
int a , b , c , min , max ;
cout<<"Enter three numbers : " ;
cin>>a>>b>>c ;
if (a < b)
min = a ;
else
min = b ;
if (c < min)
min = c ;
cout<<"The minimum number is "<<min ;
getch () ;
if (a > b)
max = a ;
else
max = b ;
if (c > max)
max = c ;
cout<<"The maximum number is "<<max ;
getch () ;
}
#include <iostream.h>
void main()
{
int x , temp ;
cout<<"Enter a number : " ;
cin>>x ;
temp = x % 3 ;
if (temp == 0) ;
else
cout<<"The number is not divisible by 3" ;
}
#include <iostream.h>
void main ()
{
int n1 , n2 , n3 , n4 , n5 , max ;
cout<<"Enter any five integer values :\n";
cin>>n1>>n2>>n3>>n4>>n5;
if (n1 > n2)
max = n1 ;
else
max = n2 ;
if (n3 > max)
max = n3 ;
if (n4 > max)
max = n4 ;
if (n5 > max)
max = n5 ;
cout<<"The maximum number of the given five numbers is "<<max;
}
Example 41 :
#include<iostream.h>
#include<conio.h>
main()
{
int n=0;
clrscr();
cout<<"\nEnter the Month Number(1-12) : ";
cin>>n;
switch(n)
{
case 1:
cout<<"\nMonth is Jan";
break;
case 2:
cout<<"\n Month is Feb";
break;
case 3:
cout<<"\nMonth is March";
break;
case 4:
cout<<"\nMonth is April";
break;
case 5:
cout<<"\nMonth is May";
break;
case 6:
cout<<"\nMonth is June";
break;
case 7:
cout<<"\nMonth is July";
break;
case 8:
cout<<"\nMonth is Aug";
break;
case 9:
cout<<"\n Month is September";
break;
case 10:
cout<<"\n Month is Oct";
break;
case 11:
cout<<"\nMonth is November";
break;
case 12:
cout<<"\n month is Dec";
break;
default:
cout<<"\n Wrong input!!!";
}
getch();
return(0);
}
Example 44 : Program to print the grade for an input test score: using the else if
construct
#include <iostream.h>
void main()
{
int score;
cout << "Enter your test score: ";
cin >> score;
if (score > 100)
cout << "Error: that score is out of range.\n";
else
if (score >= 90)
cout << "Your grade is an A.\n";
else
if (score >= 80)
cout << "Your grade is a B.\n";
else
if (score >= 70)
cout << "Your grade is a C.\n";
else
if (score >= 60)
cout << "Your grade is a D.\n";
else
if (score >= 0)
cout << "Your grade is an F.\n";
else
Example 45 : Program to prints the grade for an input test score: Using the else if
construct
#include <iostream.h>
void main()
{
int score;
cout << "Enter your test score: ";
cin >> score;
if (score > 100)
cout << "Error: that score is out of range.\n";
if((score >= 90)&&(score<=100))
cout << "Your grade is an A.\n";
if((score >= 80)&&(score <= 90))
cout << "Your grade is a B.\n";
if((score >= 70)&&(score <= 80))
cout << "Your grade is a C.\n";
if((score >= 60)&&(score <= 70))
cout << "Your grade is a D.\n";
if((score >= 0)&&(score <= 60))
cout << "Your grade is an F.\n";
}
Example 48 :
#include<iostream.h>
#include<conio.h>
void main()
{
int y;
float i,x;
clrscr();
cout<<"i\ty\tx"<<endl;
cout<<"--------------------"<<endl;
for(y=1;y<=2;y++)
for(x=5.5;x<=12.5;x=x+0.5)
{
i=2+(y+0.5*x);
cout<<i<<"\t"<<y<<"\t"<<x<<endl;
}
getch();
}
Example 51 :
#include<iostream.h>
#include<conio.h>
void main()
{
int i,num;
clrscr();
cout<<"\nEnter a number : ";
cin>>num;
i=2;
while(i<=num-1)
{
if(num%i==0)
{
cout<<"\nNot a prime no.";
break;
}
i++;
}
if(i==num)
cout<<"\nPrime number";
getch();
}
}
cout<<"Do you want to continue (Y or N) ? :\n";
cin>>ans;
}
while ((ans == 'y') || (ans == 'Y')) ;
}
Example 53 :
#include<iostream.h>
#include<conio.h>
void main()
{
int ch,i,j;
char ans='y';
do
{
clrscr();
cout<<"Menu";
cout<<"\n1.Whole Screen";
cout<<"\n2.Half Screen";
cout<<"\n3.Top Three Lines";
cout<<"\n4.Bottom Three Lines";
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1: clrscr();
for(i=0;i<25;i++)
{
for(j=0;j<80;j++)
cout<<"*";
cout<<"\n";
}
break;
case 2: clrscr();
for(i=0;i<13;i++)
{
for(j=0;j<80;j++)
cout<<"*";
cout<<"\n";
}
break;
case 3: clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<80;j++)
cout<<"*";
cout<<"\n";
}
break;
case 4: clrscr();
for(i=0;i<44;i++)
cout<<"\n";
for(i=0;i<3;i++)
{
for(j=0;j<80;j++)
cout<<"*";
cout<<"\n";
}
break;
default:cout<<"\nWrong choice!!!!";
}
cout<<"\nDo you want to continue?(y/n) : ";
cin>>ans;
}
while(ans=='y'||ans=='Y');
getch();
}
Example 55 :
#include<iostream.h>
#include<conio.h>
void main()
{
int ch,i,j,in=1;
char ans='y';
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i&&i<=3;j++)
cout<<"\t";
//to leave space before stars for upper triangle
for(j=1;j<=i-1&&i>3;j++) //to leave space before stars for lower triangle
cout<<"\t";
for(j=1;j<=in;j++)
//to display stars
cout<<"*\t";
if(i+1<=3)
in=in+2;
else
in=in-2;
cout<<"\n\n";
}
getch();
}
{
temp = num % 10 ;
sum = sum + temp ;
num = num / 10 ;
}
while (num != 0) ;
cout<<"The sum of individual digits is "<<sum;
}
#include <iostream.h>
#include <conio.h>
void main()
{
int rn;
cout<<"Enter Room number : ";
cin>>rn;
switch (rn)
{
case 1 :
cout<<"Hi! This is Room 1.\n";
break ;
case 2 :
cout<<"Hi! This is Room 2.\n";
break ;
case 3 :
cout<<"Hi! This is Room 3.\n";
break ;
case 4 :
cout<<"Hi! This is Room 4.\n";
break ;
default :
cout<<"Wrong Way !!\n";
}
getch () ;
}
cout<<"1. Addition\n";
cout<<"2. Subtraction\n";
cout<<"3. Division\n";
cout<<"4. Multiplication\n";
cout<<"5. Modulus\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1 :
cout<<"Addition of "<<x<<" and "<<y<<" : "<<x+y;
break ;
case 2 :
cout<<"Subtraction of "<<x<<" and "<<y<<" : "<<x-y;
break ;
case 3 :
cout<<"Division of "<<x<<" and "<<y<<" : "<<float(x)/float(y);
break ;
case 4 :
cout<<"Multiplication of "<<x<<" and "<<y<<" : "<<x*y;
break ;
case 5 :
cout<<"Modulus of "<<x<<" and "<<y<<" : "<<x%y;
break ;
default :
cout<<"Wrong choice !!";
}
}
Example 60 : Program : The switch statement
#include <iostream.h>
#include <conio.h>
void main()
{
int p ;
cout<<"Enter any value between 1 to 5 :\n";
cin>>p;
switch (p)
{
case 4 :
cout<<"Welcome to the world of C++!!\n";
break ;
case 5 :
cout<<"This is section two.\n";
break ;
case 3 :
cout<<"This happens to be section three.\n";
break ;
case 1 :
cout<<"Oops! This is section four.\n";
break ;
case 2 :
cout<<"This is the last chance u have...\n";
break ;
default:
cout<<"I am sorry! Wrong Way !!\n";
}
getch () ;
}
case 'w':
cout<<"I am in case w\n";
break ;
case 'O':
case 'o':
cout<<"I am in case o\n";
break ;
case 'R':
case 'r':
cout<<"I am in case r\n";
break ;
case 'Y':
case 'y':
cout<<"I am in case y\n";
break ;
default:cout<<"U entered the wrong letter !!\n";
}
}
cout<<"\tMenu-Arithmatic operations\n";
cout<<"1. Addition\n";
cout<<"2. Subtraction\n";
cout<<"3. Division\n";
cout<<"4. Multiplication\n";
cout<<"5. Modulus\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1 :
cout<<"Addition of "<<x<<" and "<<y<<" : "<<x+y;
break ;
case 2 :
cout<<"Subtraction of "<<x<<" and "<<y<<" : "<<x-y;
break ;
case 3 :
cout<<"Division of "<<x<<" and "<<y<<" : "<<float(x)/float(y);
break ;
case 4 :
cout<<"Multiplication of "<<x<<" and "<<y<<" : "<<x*y;
break ;
case 5 :
cout<<"Modulus of "<<x<<" and "<<y<<" : "<<x%y;
break ;
default :
cout<<"Wrong choice !!";
}
break ;
case 2 :
cout<<"\tMenu-Relational opeations\n";
cout<<"1. Greater than\n";
cout<<"2. Less than\n";
cout<<"3. Equal to\n";
cout<<"4. Less than or equal to\n";
cout<<"5. Greater thn or equal to\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1 :
cout<<"Result of x greater than y : "<<(x>y);
break ;
case 2 :
cout<<"Result of x less than y : "<<(x<y);
break ;
case 3 :
cout<<"Result of x equal to y : "<<(x==y);
break ;
case 4 :
cout<<"Result of x less than or equal to y : "<<(x<=y);
break ;
case 5 :
cout<<"Result of x greater than or equal to y : "<<(x>=y);
break ;
}
break ;
case 3 :
cout<<"\tMenu-Logical operations\n";
cout<<"1. Logical AND\n";
cout<<"2. Logical OR\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1 :
cout<<"Result of x AND y : "<<(x&&y);
break ;
case 2 :
cout<<"Result of x OR y : "<<(x||y);
break ;
}
break ;
}
getch () ;
}
Example 64 :
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0,n;
clrscr();
cout<<"\nTable of 4 :\n";
for(i=1;i<=10;i++)
cout<<i<<"*4 = "<<i*4<<endl;
getch();
}
Example 65 :
#include<iostream.h>
#include<conio.h>
#include<dos.h>
void main()
{
clrscr();
cout<<"Christmas Tree:\n\n\n\n";
cout<<"\t\t\t\t\t /\\\n";
cout<<"\t\t\t\t\t /\\\n";
cout<<"\t\t\t\t\t/ \\\n";
cout<<"\t\t\t\t
/\\\n";
cout<<"\t\t\t\t
/________\\\n";
cout<<"\t\t\t\t
| | \n";
cout<<"\t\t\t\t
| | \n";
cout<<"\t\t\t\t
| | \n";
getch();
}
void main()
{
int n , tri_num ;
cout<<"Enter the row number :\n";
cin>>n;
tri_num = n * (n+1) / 2 ;
cout<<"The value of the triangular number is "<<tri_num;
}
while (i<11)
{
sum = sum + i ;
i++ ;
}
cout<<"The sum of first ten numbers is : "<<sum;
}
#include <iostream.h>
#include<conio.h>
void main()
{
int i = 1 , j , n ;
cout<<"Enter the number of rows (from 1 to 10) : " ;
cin>>n;
while (i<=n)
{
j=1;
while (j<=i)
{
cout<<"*"<<"\t" ;
j++ ;
}
cout<<"\n" ;
i++ ;
}
getch () ;
}