0% found this document useful (0 votes)
19 views7 pages

Example Programs

Uploaded by

zeytunistan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Example Programs

Uploaded by

zeytunistan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Programming in C++ First Class

Mosul University
Engineering College
Mechatronics Department

Example Programs in C++

By
Assistant Lecturer: Osamah A. Taha

1
Programming in C++ First Class

1- Write a program to read three integer numbers and calculate the average then print it on
screen.
# include <iostream>
using namespace std;
int main() {
int a,b,c;
float avg;
cout<<"Enter the first number : \n";
cin>>a;
cout<<"Enter the second number : \n";
cin>>b;
cout<<"Enter the third number : \n";
cin>>c;
avg = (a+b+c)/3.0;
cout<<"The average is : "<<avg;
return 0;
}

2- Write a program to calculate the results of the five arithmetic operators (+,-,*,/,%) for any
two integer numbers.

# include <iostream>
using namespace std;
int main() {
int x,y;
cout<<"Please enter two integer numbers : "<<endl;
cin>>x>>y;
cout<<"x+y = "<<x+y<<endl;
cout<<"x-y = "<<x-y<<endl;
cout<<"x*y = "<<x*y<<endl;
cout<<"x/y = "<<x/y<<endl;
cout<<"x%y = "<<x%y<<endl;

return 0;
}

2
Programming in C++ First Class

3- Write a program to check a given integer if its divisible by 7 or not?


# include <iostream>
using namespace std;
int main() {
int x;
cout<<"Enter integer number : ";
cin>>x;
if(x%7==0)
cout<<"the number is divisible by 7";
else
cout<<"the number is not divisible by 7";
return 0;
}
4- Write a program to calculate and print the area and circumstance of any circle
(Hint : the radius must be read from keyboard and define PI as constant)
# include <iostream>
using namespace std;
int main() {
int radius;
float area, circum;
const float PI = 3.14;
cout<<"Enter the radius : ";
cin>>radius;

area = radius * radius * PI;


circum = 2 * radius * PI;
cout<<"The area of the circle is : "<<area<<endl;
cout<<"The circumstance of the circle is : "<<circum<<endl;

return 0;
}

3
Programming in C++ First Class

5- Write a complete C++ program to solve the following equation: z = (a𝒙𝟑+ bx ÷ c)


(Hint: Enter the value of a, x, b, c from keyboard).
# include <iostream>
#include<math.h>
using namespace std;
int main() {
float a,b,c,x,z;
cout<<"Enter vlues of a x b c : ";
cin>>a>>x>>b>>c;
z= (a*pow(x,3)) + (b*x/c);
cout<<"The result of the equation is : "<<z;
return 0;
}

6- Write a program to swap the values of two integer variables.

# include <iostream>
#include<math.h>
using namespace std;
int main() {
int x,y,z;
cout<<"Enter x : ";
cin>>x;
cout<<"Enter y : ";
cin>>y;

z=x;
x=y;
y=z;

cout<<"vlaue of x is : "<<x<<endl;
cout<<"value of y is : "<<y<<endl;
return 0;
}

4
Programming in C++ First Class

7- Write a program to define and enter values of three integer numbers then find
the maximum number of them using if/else if
# include <iostream>
using namespace std;

int main(){
int x,y,z;
cout<<"Enter number 1 : ";
cin>>x;
cout<<"Enter number 2 : ";
cin>>y;
cout<<"Enter number 3 : ";
cin>>z;

if(x>y && x>z)


cout<<"Max is : "<<x;
else if(y>x && y>z)
cout<<"Max is : "<<y;
else
cout<<"Max is : "<<z;

return 0;
}

5
Programming in C++ First Class

8- ‫طريقة اخرى في حل السؤال السابع‬

# include <iostream>
using namespace std;

int main(){
int x,y,z;
int max;
cout<<"enter number 1 : ";
cin>>x;
cout<<"enter number 2 : ";
cin>>y;
cout<<"enter number 3 : ";
cin>>z;

max=x;
if(max<y)
max=y;
if(max<z)
max=z;
cout<<"Max number is : "<<max;
return 0;
}

6
Programming in C++ First Class

9- Write a program to enter two numbers then enter one of the arithmetic
operations (+,-,*,/,%) the program should perform the arithmetic operation
between the two numbers.
# include <iostream>
using namespace std;

int main(){
int num1,num2;
char x;
cout<<"Enter number 1 : ";
cin>>num1;
cout<<"Enter number 2 : ";
cin>>num2;

cout<<"Enter Arithmetic operation : ";


cin>>x;

if(x=='+'){
cout<<num1+num2;
}
else if(x=='-'){
cout<<num1-num2;
}
else if(x=='*'){
cout<<num1*num2;
}
else if(x=='/'){
cout<<num1/num2;
}
else if(x=='%'){
cout<<num1%num2;
}

return 0;
}

You might also like