Problem : Read any number from the user,
then print positive if it is positive.
Program
#include <iostream.h>
void main() {
int Num;
cout<<"\n Please Enter an integer number:";
cin>>Num;
if (Num > 0)
cout<<" Positive\n";
}
Write a program that reads a mark,
if mark is 60 or greater, the program
prints PASS, else it will print FAIL
#include <iostream.h>
void main() {
int mark;
cout<<"\n Please Enter your mark: ";
cin>>mark;
if (mark>=60)
cout<<" PASS\n";
else
cout<<"FAIL\n";
}
Problem : Read any number from the user,
then print positive if it is positive and print
negative otherwise.
Program
#include <iostream.h>
void main() {
int Num;
cout<<"\nPlease Enter Number:";
cin>>Num;
if (Num < 0)
cout<<"\nNegative\n";
else
cout<<"\nPositive\n";
}
Problem : Read Two numbers from the user,
then print the greatest one.
Program
#include <iostream.h>
void main() {
int x,y;
cout<<"\nPlease Enter two numbers:";
cin>>x>>y;
cout<<"Max = ";
if (x > y)
cout<<x<<endl;
else
cout<<y<<endl;
Problem : Read three numbers to print the
smallest one.
Program
#include <iostream.h>
void main() {
int a, b, c;
cout<<"\nPlease Enter three numbers:";
cin>>a>>b>>c;
cout<<"\nMin= ";
if ((a < b) && (a < c))
cout<<a;
if ((b < a) && (b < c))
cout<<b;
if ((c < a) && (c < b))
Problem : Read number, if it is positive, Add
number 10 to it and print the Number is positive, but
otherwise, subtract number 10 from it and print the
Number is negative.
Program
#include <iostream.h>
void main() {
int Number;
cout<<"\nPlease enter Number:";
cin>>Number;
if (Number>0) {
Number = Number + 10;
cout<<Number<<" is Positive\n";}
else {
Number = Number - 10;
cout<<Number<<" is Negative\n";}
Problem : Print the word Amman five
times.
Program
#include <iostream.h>
void main( ) {
for (int i=1; i<=5; i++)
cout<<"Amman\n";
}
Another Program
#include <iostream.h>
void main() {
for (int i=5; i>=1; i--)
cout<<"Amman\n";
}
Problem : Print the following numbers.
1
3
5
7
9
11
Program
#include <iostream.h>
void main( ) {
for (int k=1; k<=11; k+=2)
cout<<k<<\t;
cout<<endl;
}
Problem : Print the following numbers.
20 17 14 11 8
5
2
Program
#include <iostream.h>
void main() {
for (int m=20; m>=2; m-=3)
cout<<m<<\t;
cout<<endl;
}
Problem : Print the following numbers.
1
2
3
4
n(entered by user)
Program
#include <iostream.h>
void main() {
int n;
cout<<"\nPlease enter the upper limit:";
cin>>n;
for (int i=1; i<=n; i++)
cout<<i<<\t;
cout<<endl;
}
Problem : Print the following numbers.
L (entered By user) (L+1)
(L+2) U (entered By user)
Program
#include <iostream.h>
void main() {
int L,U;
cout<<"\nPlease enter the Lower limit:";
cin>>L;
cout<<"\nPlease enter the Upper limit:";
cin>>U;
for (int i=L; i<=U; i++)
cout<<i<<\t;
cout<<endl;
Problem : Read five numbers from the user and print
the positive numbers only.
Program
#include <iostream.h>
void main()
{
int num;
for (int i=1; i<=5; i++)
{ cout<<"\nPlease Enter No"<<i<<':';
cin>>num;
if (num > 0)
cout<<num;
}
}
Problem : Compute and print S, Where
Sum = 1+ 3+ 5+ 7+ + n
Program
#include <iostream.h>
void main() {
int Sum=0, n;
cout<<"\nPlease Enter n";
cin>>n;
for (int i=1; i<=n; i+=2)
Sum+=i;
cout<<"\nSum="<<Sum<<endl;
}
Problem : Compute and print the summation
of any 10 numbers entered by the user.
Program
#include <iostream.h>
void main() {
int S=0, N;
for (int i=10; i>=1; i--) {
cout<<"\nPlease Enter the next number:";
cin>>N;
S+=N;}
cout<<"\nS="<<S<<endl;
}
A program to find n!
#include <iostream.h>
void main( ) {
int Fact=1,n;
cout<<Enter an integer to find its factorial;
cin>>n;
for (int j=n; j>=1; j--)
Fact *= j;
cout<<n<<"!= "<<Fact<<endl;
}
Try it on n = 17 ???
Problem : Compute and Print the value of M
where M = 2 * 4 * 6 * * n
Program
#include <iostream.h>
void main( ) {
long M=1;
int n;
cout<<"please enter the upper Limit:";
cin>>n;
for (int i=2; i<=n; i+=2)
M *= i;
cout<<"M= "<<M<<endl;
}
Nested for
Problem : Draw the following shape
*
**
***
****
*****
#include <iostream.h>
void main( ) {
for (int raw=1; raw<=5; raw++)
{
for (int C=1; C<=raw; C++)
cout<<'*';
cout<<endl;
}
Problem : Draw the following shape
*****
****
***
**
*
#include <iostream.h>
void main() {
for (int i=1; i<=5; i++) {
for (int j=i; j<=5; j++)
cout<<'*';
cout<<endl; }
}
Problem : display the multiplication table for
the number 3.
#include <iostream.h>
void main() {
for (int i=1; i<=10; i++)
cout<<"3 x "<<i<<" = "<<3*i<<endl;
}
Problem : Print the word Amman five
times.
Program
#include <iostream.h>
void main() {
int i=1;
while (i<=5)
{
cout<<"Amman\n";
i++;
}
}
Problem : Print the following numbers.
1
3
5
7
9
11
Program
#include <iostream.h>
void main() {
int i=1;
while (i <= 11) {
cout<<i<<'\t';
i+=2; }
}
Remark:
Write ((i+=2) <= 11 ) condition instead of the above one.
What changes you have to do to produce the same
Problem : Print the following numbers.
20 17 14 n
Program
#include <iostream.h>
void main() {
int n, k=20;
cout<<"Enter the lower limit:";
cin>>n;
while ( k >= n) {
cout<<k<<'\t';
k -= 3; }
}
Remark:
Write k-=3 instead of k in the above condition. What you
Problem : Read five numbers from the user and print
the positive numbers only.
Program
#include <iostream.h>
void main() {
int num, j=0;
while ( j++ < 5 ) {
cout<<"Enter the next num:";
cin>>num;
if (num > 0)
cout<<num<<endl; }
}
Problem : Compute and print S, Where
Sum = 1+ 3+ 5+ 7+ + n
Program
#include <iostream.h>
void main() {
int n, Sum=0, i=1;
cout<<"Enter the upper limit:";
cin>>n;
while ( i <= n ) {
Sum += i;
i += 2; }
cout<<"\nSum="<<Sum;
}
Problem : Read 10 numbers by the user and
compute and print the summation of
numbers, which are divisible by 3.
Program
#include <iostream.h>
void main() {
int Num, Sum=0, i=1;
while ( i <= 10 ) {
cout<<"Enter a number:";
cin>>Num;
if (Num % 3 == 0)
Sum += Num;
i++;
}
Problem : Compute and Print the value of M
where M = 2 * 4 * 6 * * n
Program
#include <iostream.h>
void main() {
int N, M=1, i=2;
cout<<"Enter the upper limit:";
cin>>N;
while ( i <= N ) {
M *= i;
i += 2; }
cout<<"\nM="<<M;
}
Nested Loops
#include <iostream.h>
void main() {
int i,j=1;
while(j<=4){
i=1;
while(i<=4){
cout<<i<<"\t";
i++;
}
j++;
cout<<endl;
}
}
1234
1234
1234
1234
Problem : Draw the following shape
*
**
***
****
*****
#include <iostream.h>
void main() {
int i=1;
while (i<=5) {
int j=1;
while (j<=i)
{
cout<<'*';
j++;}
cout<<endl;
i++; }
}
Do .. While Technique
General Form:
do {
.
Statement(s);
.
} while (Condition) ;
Statements will be executed repeatedly while condition is true. When the
condition become false, the loop will be terminated and the execution
sequence will go to the first statement after the loop.
The loop body will be executed at least one.
Problem : Print the word Amman five
times.
Program
#include <iostream.h>
void main() {
int i = 1;
do {
cout<<"Amman\n";
i++;
} while (i <= 5);
}
Execute the following program
#include <iostream.h>
void main() {
int Choice, Num;
do {
cout<<"\n Enter the New Number";
cin>>Num;
if (Num % 2 == 0)
cout<<Num<<" is Even\n";
else
cout<<Num<<" is Odd\n";
cout<<"Select your choice.\n";
cout<<"1- Exit. (Terminate the Run)\n";
cout<<"2- Enter New Number to Check.\n";
cin>>Choice;
} while (Choice != 1);
}
1
12
124
1248
for (int i=1;i<=4;i++)
{int x=1;
for(int j=1;j<=i;j++)
{cout<<x<< ;
x=x*2;}
cout<<endl;}