write a program that asks the user to enter two numbers

10
Q=2.16: Write a program that asks the user to enter two numbers, obtains the two numbers from the user and prints the sum, product, difference, and quotient of the two numbers #include <iostream> Using namespace std; Int main() { Int x , y ; Cout << “enter the numbers “; Cin >> x >> y; Cout << “sum = “ << x+y; Cout << “product= “<<x*y; Cout <<”difference =”<< x-y ; Cout <<”quotient =””<< x/y ; Return 0; } Q NO 2.17: Write a program that prints the numbers 1 to 4 on the same line with each pair of adjacent numbers separated by one space.Writethe program using the following methods: a) Using one output statement with one stream insertion operator. #include <iostream> Using namespace std; Int main() { Cout << “1 2 3 4 “; b) Using one output statement with four stream insertion operators. Cout <<”1”<<”2”<<”3”<<”4” ; c) Using four output statements. Cout <<”1”;

Upload: khangminh22

Post on 21-Jan-2023

0 views

Category:

Documents


0 download

TRANSCRIPT

Q=2.16: Write a program that asks the user to enter two numbers, obtains the two

numbers from the user and prints the sum, product, difference, and quotient of the two

numbers

#include <iostream>

Using namespace std;

Int main()

{

Int x , y ;

Cout << “enter the numbers “;

Cin >> x >> y;

Cout << “sum = “ << x+y;

Cout << “product= “<<x*y;

Cout <<”difference =”<< x-y ;

Cout <<”quotient =””<< x/y ;

Return 0;

}

Q NO 2.17: Write a program that prints the numbers 1 to 4 on the same line with each pair

of adjacent numbers separated by one space.Writethe program using the following methods:

a) Using one output statement with one stream insertion operator.

#include <iostream>

Using namespace std;

Int main()

{

Cout << “1 2 3 4 “;

b) Using one output statement with four stream insertion operators.

Cout <<”1”<<”2”<<”3”<<”4” ;

c) Using four output statements.

Cout <<”1”;

Cout<<”2”;

Cout<<”3”;

Cout<<”4”;

Return 0;

}

Q 2.18: write a program that asks the user to enter two integers, obtains the

numbers from the user, then prints the larger number followed by the words

"is larger." If the numbers are equal, print the message “These numbers are

equal.” #include <iostream>

Using namespace std;

int main()

{

int i, j;

cout << "Enter two numbers: ";

cin >> i >> j;

if(i > j)

cout << "is larger " << i;

else if(i == j)

cout << "These numbers are equal.";

else

cout << "is larger " << j;

return 0;

}

Q NO 2.19: Write a program that inputs three integers from the keyboard

and prints the sum, average, product, smallest and largest of these numbers.

The screen dialogue should appear as follows:

ANS

#include <iostream>

Using namespace std;

int main()

{

int x, y, z;

int sum, average, product, smallest, largest;

cout << "Input three different integers: ";

cin >> x >> y >> z;

sum = x + y + z;

average = (x + y + z)/3;

product = x * y * z;

cout << "Sum is " << sum << endl;

cout << "Average is " << average << endl;

cout << "Product is " << product << endl;

if ((x<y)&&(x<z))

cout << "Smallest is " << x << endl;

if ((y<x)&&(y<z))

cout << "Smallest is " << y << endl;

if ((z<x)&&(z<y))

cout << "Smallest is " << z << endl;

if ((x>y)&&(x>z))

cout << "Largest is " << x << endl;

if ((x<y)&&(z<y))

cout << "Largest is " << y << endl;

if ((z>y)&&(z>x))

cout << "Largest is " << z << endl;

return 0;

}

Q NO 2.20 :Diameter, Circumference and Area of a Circle) Write a program that reads in the radius of a circle as an integer and prints the circle’s diameter, circumference and area. Use the constant value 3.14159 for π. Do all calculations in output statements. [Note: In this chapter, we’ve discussed only integer constants and variables. In Chapter 4 we discuss floating-point numbers, i.e., values that can have decimal points.] #include <iostream>

Using namespace std;

int main()

{

double pi = 3.14159;

int radius;

cout << "Please enter the radius: ";

cin >> radius;

cout << "The diameter is " << 2 * radius << endl;

cout << "Circumference is " << 2 * pi * radius << endl;

cout << "The area of circle is " << pi * radius * radius << endl;

return 0;

}

Q NO 2.23: Largest and Smallest Integers) Write a program that reads in five integers and determines and prints the largest and the smallest integers in the group. Use only the programming techniques you learned in this chapter. #include <iostream>

Using namespace std;

int main()

{

int num1, num2, num3, num4, num5;

cout << "Enter the five integers: " << endl;

cin >> num1;

cin >> num2;

cin >> num3;

cin >> num4;

cin >> num5;

cout << "The entered integers: " << num1 << ", " << num2 << ", " <<

num3 << ", " << num4 << ", " << num5 << ".\n";

{

if(num1 < num2)

if(num1 < num3)

if(num1 < num4)

if(num1 < num5)

cout << "Smallest is " << num1 << endl;

if(num1 > num2)

if(num2 < num3)

if(num2 < num4)

if(num2 < num5)

cout << "Smallest is " << num2 << endl;

if(num3 < num2)

if(num3 < num1)

if(num3 < num4)

if(num3 < num5)

cout << "Smallest is " << num3 << endl;

if(num5 < num2)

if(num5 < num3)

if(num5 < num4)

if(num5 < num1)

cout << "Smallest is " << num4 << endl;

if(num4 < num2)

if(num4 < num3)

if(num4 < num1)

if(num4 < num5)

cout << "Smallest is " << num5 << endl;

}

{

if(num1 > num2)

if(num1 > num3)

if(num1 > num4)

if(num1 > num5)

cout << "Largest is " << num1 << endl;

if(num2 > num1)

if(num2 > num3)

if(num2 > num4)

if(num2 > num5)

cout << "Largest is " << num2 << endl;

if(num3 > num2)

if(num3 > num1)

if(num3 > num4)

if(num3 > num5)

cout << "Largest is " << num3 << endl;

if(num4 > num2)

if(num4 > num3)

if(num4 > num1)

if(num4 > num5)

cout << "Largest is " << num4 << endl;

if(num5 > num2)

if(num5 > num3)

if(num5 > num4)

if(num5 > num1)

cout << "Largest is " << num5 << endl;

}

return 0;

}

Q NO 2.24:Write a program that reads an integer and determines and prints whether it’s odd or even. [Hint: Use the modulus operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided by 2 #include <iostream>

Using namespace std;

int main()

{

int x;

cout << "Enter integer :\n";

cin >> x;

if(x % 2 == 0)

cout << "The integer is even.\n";

if(x % 2 == 1)

cout << "The integer is odd.\n";

return 0;

}

Q NO 2.25: Write a program that reads in two integers and determines and prints f the first is a multiple of the second. [Hint: Use the modulus operator.] #include <iostream> Using namespace std; int main() { int x, y; cout << "Enter the two integers.\n"; cin >> x >> y; if (x % y == 0) cout << "first is a multiple of the second.\n"; else cout << "first is not a multiple of the second.\n"; return 0; }

Q NO 2.26:Display the following checkerboard pattern with eight output statements, then display the same pattern using as few statements as possible. #include <iostream> Using namespace std; int main() { cout << "* * * * * * * * \n"; cout << " * * * * * * * *\n"; cout << "* * * * * * * * \n"; cout << " * * * * * * * *\n"; cout << "* * * * * * * * \n"; cout << " * * * * * * * *\n"; cout << "* * * * * * * * \n"; cout << " * * * * * * * *\n"; cout << endl; cout << "* * * * * * * * \n * * * * * * * *\n* * * * * * * * \n * * * * * * * *\n* * * * * * * * \n * * * * * * * *\n* * * * * * * * \n * * * * * * * *\n"; return 0; }

Q NO 2.27 :Here is a peek ahead. In this chapter you learned about integers and the type int. C++ can also represent uppercase letters, lowercase letters and a considerable variety of special symbols. C++ uses

small integers internally to represent each different character. The set of characters a computer uses and the corresponding integer representations for those characters are called that computer’s character set. You can print a character by enclosing that character in single quotes….. #include <iostream> Using namespace std; int main() { cout << 'A' << " = " << static_cast< int >( 'A' ) << endl << 'B' << " = " << static_cast< int >( 'B' ) << endl << 'C' << " = " << static_cast< int >( 'C' ) << endl << 'a' << " = " << static_cast< int >( 'a' ) << endl << 'b' << " = " << static_cast< int >( 'b' ) << endl << 'c' << " = " << static_cast< int >( 'c' ) << endl << '0' << " = " << static_cast< int >( '0' ) << endl << '1' << " = " << static_cast< int >( '1' ) << endl << '2' << " = " << static_cast< int >( '2' ) << endl << '$' << " = " << static_cast< int >( '$' ) << endl << '*' << " = " << static_cast< int >( '*' ) << endl << '+' << " = " << static_cast< int >( '+' ) << endl << '/' << " = " << static_cast< int >( '/' ) << endl << ' ' << " = " << static_cast< int >( ' ' ) << endl; return 0; }

Q NO 2.28: Write a program that inputs a five-digit integer, separates the integer into itsdigits and prints them separated by three spaces each. [Hint: Use the integer division and modulus operators.] For example, if the user types in 42339, the program should print: #include <iostream>

Using namespace std;

int main()

{

int fivedigit;

int first, second, third, fourth, fifth;

cout << "Please enter the five digit integer: ";

cin >> fivedigit;

if(fivedigit < 10000)

{

cout << "Incorrect digit! Try again!";

}

if(fivedigit > 99999)

{

cout << "Incorrect digit! Try again!";

}

first = fivedigit/10000;

second = (fivedigit/1000)%10;

third = (fivedigit/100)%10;

fourth = (fivedigit/10)%10;

fifth = fivedigit%10;

cout << first;

cout << " " << second;

cout << " " << third;

cout << " " << fourth;

cout << " " << fifth;

return 0;

}

Q NO 2.30:Using the techniques of this chapter, write a program that calculates the squares and cubes of the integers from 0 to 10. Use tabs to print the following neatly formatted table of values: #include <iostream> Using namespace std; int main() { cout << "integer\t\tsquare\t\tcube\n"; cout << 0 << "\t\t" << 0*0 << "\t\t" << 0*0*0 << "\n"; cout << 1 << "\t\t" << 1*1 << "\t\t" << 1*1*1 << "\n"; cout << 2 << "\t\t" << 2*2 << "\t\t" << 2*2*2 << "\n"; cout << 3 << "\t\t" << 3*3 << "\t\t" << 3*3*3 << "\n"; cout << 4 << "\t\t" << 4*4 << "\t\t" << 4*4*4 << "\n"; cout << 5 << "\t\t" << 5*5 << "\t\t" << 5*5*5 << "\n"; cout << 6 << "\t\t" << 6*6 << "\t\t" << 6*6*6 << "\n"; cout << 7 << "\t\t" << 7*7 << "\t\t" << 7*7*7 << "\n"; cout << 8 << "\t\t" << 8*8 << "\t\t" << 8*8*8 << "\n"; cout << 9 << "\t\t" << 9*9 << "\t\t" << 9*9*9 << "\n"; cout << 10 << "\t\t" << 10*10 << "\t\t" << 10*10*10 << "\n"; return 0; }

Q NO 30: BMI……………….

#include<iostream>

using namespace std;

int main()

{

Int double weight, height, BMI;

cout << "Enter your weight (in pounds): ";

cin >> weight;

cout << "\nEnter your height (in inches): ";

cin >> height;

BMI = (weight * 703) / (height * height);

if(BMI < 18.5)

cout << "You are underweight!! Eat More!! \n";

if(BMI >= 18.5 && BMI <= 25)

cout << "You are in optimal shape!! Good Work! \n";

if(BMI > 25)

cout << "You are overweight!! Eat Less!! \n\n";

return 0;

}

return 0;

}

Q NO 2.21 :Write a program that prints a box, an oval, an arrow and a diamond as

follows: }