C++ exercises and solutions: arithmetic compound operators


C++ arithmetic and compound operators

1.Write a C++ program to produce the output as shown below:


 
Results:
 
x value y   value   expressions   results
10            | 5         | x=y+3       | x=8
10            | 5         | x=y-2        | x=3
10            | 5         | x=y*5        | x=25
10            | 5         | x=x/y         | x=2
10            | 5         | x=x%y       |  x=0

 
Solution:
#include <cstdlib>
#include <iostream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
int x;
int y;
x=10;
y=5;
cout<<"Result:\n";
cout<<"x value\t"<<"y value\t"<<"Expressions\t"<<"Result\n";
cout<<x<<" |\t"<<y<<" |\t"<<"x=y+3\t"<<"\t|"<<"x="<<y+3<<"\n";
cout<<x<<" |\t"<<y<<" |\t"<<"x=y-2\t"<<"\t|"<<"x="<<y-2<<"\n";
cout<<x<<" |\t"<<y<<" |\t"<<"x=y*5\t"<<"\t|"<<"x="<<y*5<<"\n";
cout<<x<<" |\t"<<y<<" |\t"<<"x=x/y\t"<<"\t|"<<"x="<<(float)(x/y)<<"\n";
cout<<x<<" |\t"<<y<<" |\t"<<"x=x%y\t"<<"\t|"<<"x="<<x%y<<"\n";

system("PAUSE");
return EXIT_SUCCESS;
}
 


2. Write a program to produce the output as shown below:

 
Results:
 
x value y value expressions results
10       | 5         | x+=y      | x=15
10       | 5         | x-=y-2    | x=7
10       | 5         | x*=y*5   | x=250
10       | 5         | x/=x/y     | x=5
10       | 5         | x%=y     | x=0
 
Solution:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x;
int y;
x=10;
y=5;
cout<<"Result:\n";
cout<<"x value\t"<<"y value\t"<<"Expressions\t"<<"Result\n";

cout<<x<<" |\t"<<y<<" |\t"<<"x+=y\t"<<"\t|"<<"x="<<x+y<<"\n";
cout<<x<<" |\t"<<y<<" |\t"<<"x=y-2\t"<<"\t|"<<"x="<<x-(y-2)<<"\n";
cout<<x<<" |\t"<<y<<" |\t"<<"x=y*5\t"<<"\t|"<<"x="<<x*y*5<<"\n";
cout<<x<<" |\t"<<y<<" |\t"<<"x=x/y\t"<<"\t|"<<"x="<<(float)x/(x/y)<<"\n";
cout<<x<<" |\t"<<y<<" |\t"<<"x=x%y\t"<<"\t|"<<"x="<<x%y<<"\n";

system("PAUSE");
return EXIT_SUCCESS;
}



Comments

Usman comment

 Usman

1.write a program that input property in rupees ,number of sons,number of daughter and input Y if the widow is alive and calculate the share of each son and calculate the share of each daugter and widow solve in Dev C++



2017-11-06
ravi comment

 ravi

Develop a class to represent one digit counter. The class must have data member to represent counter. The class should have following function
a. Function to set the value of the counter
b. Function to display value of the counter
c. Function to increment the counter
d. Function to decrement the counter


2017-11-05
Hamish hameed  comment

 Hamish hameed

#include<iostream.h>

main()
{
char asterisk='*';
for(int count=1;count<=8;count++){
for(int i=8;i>=count;i--){
cout<<asterisk;
}cout<<endl;
}



2017-03-16
JasonFromEarth1981 comment

 JasonFromEarth1981

#include <iostream>
int main()
{
char c = { '*' };
for (int i = 9; i > 0; i--)
{
for (int j = i; j > 0; j--)
{
std::cout << c;
}
std::cout << std::endl;
}
}


2017-03-16
desai ramesh comment

 desai ramesh

********
*******
******
*****
****
***
**
*
this is output show this loop.


2017-03-07
Jefferson comment

 Jefferson

@ebessien

#include <iostream>
#include <string>

using namespace std;
int main()
{
string Asterisk = "*";
for (int count = 1; count <= 8; count++) {
for (int i = 8; i >= count; i--){
cout << Asterisk;
}cout << endl;
}
}


2017-01-18
سینا comment

 سینا

ممنون از مثال ها/thanks for example :)


2016-04-30
hacker comment

 hacker

never say thankyou


2016-04-28
ebessien comment

 ebessien

Using for loop, write a write a C++ program be using two
For loops to produce the output shown below

********
*******
******
*****
****
***
**
*


2016-04-06
Kophyo comment

 Kophyo

Thanks!


2016-02-22
nkishor  comment

 nkishor

thanku


2015-08-03



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.