Download - C++ basic programs
![Page 1: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/1.jpg)
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";}
![Page 2: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/2.jpg)
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";
}
![Page 3: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/3.jpg)
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";}
![Page 4: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/4.jpg)
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;}
![Page 5: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/5.jpg)
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))
cout<<c;cout<<endl;
}
![Page 6: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/6.jpg)
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";}
}
![Page 7: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/7.jpg)
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";
}
![Page 8: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/8.jpg)
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;}
![Page 9: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/9.jpg)
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;}
![Page 10: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/10.jpg)
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;
}
![Page 11: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/11.jpg)
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;
}
![Page 12: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/12.jpg)
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; }
}
![Page 13: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/13.jpg)
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;
}
![Page 14: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/14.jpg)
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;}
![Page 15: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/15.jpg)
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 ???
![Page 16: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/16.jpg)
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;
}
![Page 17: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/17.jpg)
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; }
}
Nested for
![Page 18: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/18.jpg)
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; }
}
![Page 19: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/19.jpg)
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;
}
![Page 20: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/20.jpg)
Problem : Print the word “Amman” five times. Program
#include <iostream.h>void main() {
int i=1;while (i<=5)
{cout<<"Amman\n";i++;}
}
![Page 21: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/21.jpg)
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 output.
![Page 22: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/22.jpg)
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 have to do to produce the same output.
![Page 23: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/23.jpg)
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; }}
![Page 24: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/24.jpg)
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;}
![Page 25: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/25.jpg)
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++; }
cout<<"\nSum="<<Sum;}
![Page 26: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/26.jpg)
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;}
![Page 27: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/27.jpg)
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;}
}
1 2 3 41 2 3 41 2 3 41 2 3 4
![Page 28: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/28.jpg)
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++; }
}
![Page 29: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/29.jpg)
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.
![Page 30: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/30.jpg)
Problem : Print the word “Amman” five times. Program
#include <iostream.h>void main() {
int i = 1;do {
cout<<"Amman\n";i++;
} while (i <= 5);}
![Page 31: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/31.jpg)
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);}
![Page 32: C++ basic programs](https://reader035.vdocuments.mx/reader035/viewer/2022062306/577c7c641a28abe0549a6925/html5/thumbnails/32.jpg)
11 21 2 41 2 4 8
for (int i=1;i<=4;i++){int x=1; for(int j=1;j<=i;j++)
{cout<<x<<“ “; x=x*2;} cout<<endl;}