1) Input 3 numbers and find the smallest
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers:";
cin>>a>>b>>c;
if(a<b&&a<c)
cout<<a<<" is smallest";
else if(b<a&&b<c)
cout<<b<<" is smallest";
else
cout<<c<<" is smallest";
return 0;
}
Output
Enter three numbers:29 45 10
10 is smallest
2) Write a program using switch to display the name of the day for a given day number.
#include <iostream>
using namespace std;
int main()
{
int day;
cout<<"Enter day number:";
cin>>day;
switch(day)
{
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 day";
break;
}
return 0;
}
Output
Enter day number:4
Wednesday
3) Write a program to print 10 to 1
#include <iostream>
using namespace std;
int main()
{
cout<<" Numbers from 10 to 1\n";
for(int i=10 ; i>=1 ; i--)
{
cout<<i<<"\t";
}
return 0;
}
Output
Numbers from 10 to 1
10 9 8 7 6 5 4 3 2 1
4) Write program to print even numbers from 1 to 100
#include <iostream>
using namespace std;
int main()
{
cout<<"Even numbers from 1 to 100\n";
for(int i=1 ; i<=100 ; i++)
{
if(i%2==0)
{
cout<<i<<"\t";
}
}
return 0;
}
Output
Even numbers from 1 to 100
2 4 6 8 10 12 14 16 18 20 22 24 26
28 30 32 34 36 38 40 42 44 46 48 50
52 54 56 58 60 62 64 66 68 70 72 74
76 78 80 82 84 86 88 90 92 94 96 98
100
5) Write program to print odd numbers from 100 to 1
#include <iostream>
using namespace std;
int main()
{
cout<<"Odd numbers from 100 to 1\n";
for(int i=100 ; i>=1 ; i--)
{
if(i%2!=0)
{
cout<<i<<"\t";
}
}
return 0;
}
Output
Odd numbers from 100 to 1
99 97 95 93 91 89 87 85 83 81 79 77 75
73 71 69 67 65 63 61 59 57 55 53 51
49 47 45 43 41 39 37 35 33 31 29 27
25 23 21 19 17 15 13 11 9 7 5 3
1
6) Write a program to find factorial of a number
#include <iostream>
using namespace std;
int main()
{
int n,f=1;
cout<<"Enter a number:";
cin>>n;
for(int i=1;i<=n;i++)
{
f=f*i;
}
cout<<"Factorial of "<<n<<"="<<f;
return 0;
}
Output
Enter a number:5
Factorial of 5=120
7) Input 3 numbers and find the largest
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers:";
cin>>a>>b>>c;
if(a>b&&a>c)
cout<<a<<" is largest";
else if(b>a&&b>c)
cout<<b<<" is largest";
else
cout<<c<<" is largest";
return 0;
}
Output
Enter three numbers:78 6 45
78 is largest
8) Input a digit and display the corresponding word using switch statement
#include <iostream>
using namespace std;
int main()
{
int digit;
cout<<"Enter a digit:";
cin>>digit;
switch(digit)
{
case 0 : cout<<"zero";
break;
case 1 : cout<<"One";
break;
case 2 : cout<<"Two";
break;
case 3 : cout<<"Three";
break;
case 4 : cout<<"Four";
break;
case 5 : cout<<"Five";
break;
case 6 : cout<<"Six";
break;
case 7 : cout<<"Seven";
break;
case 8 : cout<<"Eight";
break;
case 9 : cout<<"Nine";
break;
default : cout<<"Invalid digit";
break;
}
return 0;
}
Output
Enter a digit:3
Three
9) Input a number and check whether it is positive, negative or zero
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a number:";
cin>>n;
if(n>0)
cout<<"the number is positive";
else if(n<0)
cout<<"the number is negative";
else
cout<<"the number is zero";
return 0;
}
Output
Enter a number:-7
the number is negative
10) Display the multiplication table of a number having 10 rows
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a number:";
cin>>n;
for(int i=1; i<=10; i++)
{
cout<<i<<"*"<<n<<"="<<i*n<<"\n";
}
return 0;
}
Output
Enter a number:5
1*5=5
2*5=10
3*5=15
4*5=20
5*5=25
6*5=30
7*5=35
8*5=40
9*5=45
10*5=50