Problem:: Read Any Number From The User, Then Print Positive If It Is Positive
Problem:: Read Any Number From The User, Then Print Positive If It Is Positive
#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;
#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))
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";}
Another Program
#include <iostream.h>
void main() {
for (int i=5; i>=1; i--)
cout<<"Amman\n";
}
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;
}
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;
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;
}
}
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 ???
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;
}
#include <iostream.h>
void main() {
for (int i=1; i<=5; i++) {
for (int j=i; j<=5; j++)
cout<<'*';
cout<<endl; }
}
#include <iostream.h>
void main() {
for (int i=1; i<=10; i++)
cout<<"3 x "<<i<<" = "<<3*i<<endl;
}
#include <iostream.h>
void main() {
int i=1;
while (i<=5)
{
cout<<"Amman\n";
i++;
}
}
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; }
}
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
#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.
#include <iostream.h>
void main() {
int i = 1;
do {
cout<<"Amman\n";
i++;
} while (i <= 5);
}
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;}