Professional Documents
Culture Documents
18 February 2021
/* This is a comment */
XI. What are the differences between machine language and high-level
language?
Machine language, or machine code, consists of binary code and is the only
language that is directly understood by the computer. Both machine code and
assembly languages are hardware specific. A high-level language is a
programming language that uses English and mathematical symbols in its
instructions.
1) int*act=42
2) int*act/2=23
3) 3/2=1.54
4) 3/2*act=1.55
5) ink*act/2+3/2*act=3.56
6) int*act/2+3/2*act+2=5.57
7) int*act/2+3/2*act+2+tig=8.7
1. a*b=402
2. a*b/2=203
3. 3/2=1.54
4. 3 / 2 * b=7.55
5. a * b / 2 - 3 / 2 * b=12.56
6. a* b / 2 - 3 / 2 * b+2=14.57
7. a* b / 2 - 3 / 2 * b+2-c=10.4
(e) s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g; (a = 4, g =
3, assume s to be an int)
1. 1/3=0.3332
2. 1/3*a=1.3323
3. 1/3*a/4=0,3334
4. 6/2=35
5. 2/3=0.6666
6. 2/3*6=0.1117
7. 2/3*6/g=0.0378
8. 1 / 3 * a / 4 - 6 / 2=-2.6679
9. 1 / 3 * a / 4 - 6 / 2+2/3*6/g=-2.63
(f) x = 1 / 3 * a / 4 * 6 / 2 + 2 / 3 + 6 / g; (a = 7, g =
6, assume x to be an int)
1. 1/3=0.3332
2. 1/3*a=2.3313
3. 1/3*a/4=0.582754
4. 1/3*a/4*6=3.49655
5. 1/3*a/4*6/2=1.748256
6. 2/3=0.6667
7. 6/G=18
8. 1/3*a/4*6/2+2/3=2.414259
9. 1/3*a/4*6/2+2/3+6/G=2.41425
A. corresponding C statement:
a*a*a+b*b*b+c*c*c+3*a*b*c
B. corresponding C statement:
A=a(x-1)x*x/2*a*b
C. corresponding C statement:
Z = ( 8.8 * ( a + b ) * 2 / c - 0.5 +2 * a / ( q + r ) ) / ( ( a + b ) * ( 1 / m ) )
D.corresponding C statement:
R =( 2 * v + 6.22 * ( c + d ) ) / ( ( g + v ) )
E.corresponding C statement:
A = ( 7.7 * b * ( x * y + a ) / c - 0.8 ) / ( ( x + a ) * ( 1 / y ) )
Problem No 07: What would be the output of the following programs.
Q.1
Suppose x, y, z, and w are int variables. What value is assigned to
each of these variables after the last statement executes?
x = 5; z = 3; y = x - z;
z = 2 * y + 3;
w = x - 2* y + z;
z = w - x; w++;
SOLUTION:
#include<iostream>
using namespace std;
int main ()
{
int x,y,z,w;
cout<<"value of x ="<<x<<endl;
y=x-z;
cout<<"value of y="<<y<<endl;
z = 2 *y + 3;
cout<<"value of z="<<z<<endl;
w = x - 2 * y + z;
cout<<"value of w="<<w<<endl;
z = w - x;
cout<<"final value of z="<<z<<endl;
cout<<w++;
}
Q:2 Suppose x, y, and z are int variables and w and t are double variables. What
value is assigned to each of these variables after the last statement executes? x = 17;
y = 15; x = x + y / 4; z = x % 3 + 4; w = 17 / 3 + 6.5; t = x / 4.0 + 15 % 4 - 3.5;
#include <iostream>
using namespace std;
int main()
{
int x,y,z; double w,t;
x=17; y=15;
x = x + y / 4;
cout<<"value of x="<<x<<endl;
z = x % 3 + 4;
cout<<"value of z="<<z<<endl;
w = 17 / 3 + 6.5;
cout<<"value of w="<<w<<endl;
t = x / 4.0 + 15 % 4 - 3.5;
cout<<"value of t="<<t<<endl;
}
Q:3 #include<iostream> #include<conio.h> using namespace std; int main() { int i
= 2, j = 3, k, l ; float a, b ; k = i / j * j ; l = j / i * i ; a = i / j * j ; b = j / i * i ;
cout<<k<<"\t"<<l<<"\t"<<a<<"\t"<<b; getche(); }
Q:4Suppose x, y, and z are int variables and x = 2, y = 5, and z = 6. What is the
output of each of the following statements? cout << "x = " << x << ", y = " << y
<< ", z = " << z << endl; cout << "x + y = " << x + y << endl; cout << "Sum of "
<< x << " and " << z << " is " << x + z << endl; cout << "z / x = " << z / x <<
endl; cout << "2 times " << x << " = " << 2 *x << endl;
Problem No 08: Write the complete code in C++ for the given problems.
Q:1
#include <iostream>
using namespace std;
int main ()
{
cout<<”HELLO WORLD_”;
}
Q:2
#include<iostream>
Using namespace std;
Int main( )
{
cout<<"\\"<<"\""<<"\\""\""<<"I"<<"\\""\""<<"\\""\""<<endl;
cout<<"\t"<<"\\"<<"\""<<"\\""\""<<"Love"<<"\\""\""<<"\\""\""<<endl;
cout<<"\t""\t"<<"\\"<<"\""<<"\\""\""<<"Pakistan"<<"\\""\"";
Q:3
#include<iostream> using
namespace std; int main()
cout<<" I "<<endl;
cout<<" Love "<<endl;
cout<<"pakistan_"<<endl;
}
Q:4
#include<iostream> using
cout<<"I"<<endl;
cout<<" Love "<<endl;
cout<<"
pakistan"<<endl;
Solution;
#include<iostream>
using namespace std;
int main()
{
int x=1,sum=0;
int result=x+sum;
cout<<" the sum is="<<result;
}
Problem No 12: State the values of each of these int variables after the
calculation is performed. Assume that, when each statement begins
executing, all variables have the integer value 5.
(a) product *= x++;
(b) quotient /= ++x;
Solution;
#include<iostream>
using namespace std;
int main() {
int product=5,quotient=5,x=5;
cout<<"before formula the product is="<<product<<endl;
cout<<"before formula the quotient ="<<quotient<<endl;
cout<<"before increment the value of x is="<<x<<endl;
product*=x++;
quotient/=++x;
cout<<"after formula the product is="<<product<<endl;
cout<<"after formula the quotient is="<<quotient<<endl;
cout<<"after increment the value of x is="<<x; return 0;
}
Problem No 13: What, if anything, prints when each of the following C++
statements is performed? If nothing prints, then answer “nothing.”
Assume x= 2 and y=3
cout<<x; 2
cout<<x+x; 4
cout<<"x="; X=
) cout<<"\n"; error
Problem No 14: 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 num1, num2;
cout<< "Enter two integers: "; //
cin>> num1 >> num2;
cout<< "The sum is " << num1 + num2;
cout << "\nThe product is " << num1 * num2;
cout<< "\nThe difference is " << num1 - num2;
cout<< "\nThe quotient is " << num1 / num2 << endl;
}
Problem No 15: Write a program that inputs three integers from the
keyboard and prints the sum, average, product, smallest and largest of
these numbers.
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3, ;
cout << "Input three different integers: ";
cin>> num1 >> num2 >> num3;
cout << "Sum is " << num1 + num2 + num3
<< "\nAverage is " << (num1 + num2 + num3) / 3
<< "\nProduct is " << num1 * num2 * num3 << endl;
if((num1 >= num2) && (num1 >=
num3))
cout << "Largest number: " <<
n1;
else if ((num2 >= num1) && (num2
>= num3))
cout << "Largest number: " <<
num2;
else
cout << "Largest number: " <<
num3;
return 0;
}
Problem No 16: Write a program that prompts the user to enter the
weight of a person in kilograms and outputs the equivalent weight in
pounds. Output both the weights rounded to two decimal places. (Note
that 1 kilogram = 2.2 pounds.) Format your output with two decimal
places.
# include <iostream>
using namespace std;
int main() {
double weight;
double poundWeight;
cout<<"Enter the weight in kilogram"<<endl;
cin>>weight;
poundWeight=(weight*2.2);
cout<<"The weight in kilograms is:"<< weight<<endl;
cout<<"The weight in pounds is:"<<poundWeight<<endl;
}
Problem No 18: Enter three angles of a triangle and find if the triangle is
valid or not. (Hint: if the sum of all angles is equal to 180, the triangle will
be considered as valid triangle)
#include<iostream>
using namespace std;
int main() {
int angle1,angle2,angle3;
cout<<"Enter the three angles of triangle:";
cin>>angle1>>angle2>>angle3;
if (angle1+angle2+angle3==180){
cout<<"Triangle is valid";
}
else{
cout<<"Triangle is not valid"; return 0;
}
}
#include <iostream>
using namespace std; int main
() {
// Part A
cout << "1 2 3 4\n";
cout << "1 " << "2 " << "3 " << "4\n"; << "1 ";
cout << "2 ";
cout << "3 ";
cout << "4" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if ( num % 2 == 0 )
cout << "The number " << num << " is even."
<<endl;
if ( num % 2 != 0 ) cout << "The number " << num
<< " is odd." << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a five-digit number: ";
cin >> num;
cout << num / 10000 << " ";
num = num % 10000;
cout << num / 1000 << " ";
num = num % 1000;
cout << num / 100 << " ";
num = num % 100;
cout << num / 10 << " ";
num = num % 10; cout <<
num << endl; return 0;
}
Problem No 22: Receive two integers from user at run time and swap
their values.
#include <iostream>
using namespace std; int
main() {
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b <<
endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b <<
endl;
return 0;
}
Problem No 23: Fakhar’s basic salary is input through the keyboard. His
dearness allowance is 40% of basic salary, and house rent allowance is
20% of basic salary. Write a program to calculate his gross salary.
# include<iostream>
using namespace std;
int main() {
int bs, gs, da, hr;
cout<<("Enter the basic salary:");
cin>>bs;
da=40*bs/100;
hr =20*bs/100;
gs= bs + da + hr;
cout<<"The gross salary is="<<gs;
return 0;
}
#include<iostream>
using namespace std;
int main() {
int length , breadth , radius ;
cout<<"Enter Length Of Rectangle = ";
cin>>length;
cout<<"Enter Breadth Of Rectangle = ";
cin>>breadth;
cout<<"Enter radius Of Rectangle = ";
cin>>radius;
float area;
area = length*breadth;
cout<<"Area of rectangle ="<<area<<endl;
float perimeter;
perimeter = length + breadth;
perimeter = perimeter * 2;
cout<<"Perimeter of rectangle =
"<<perimeter<<endl;
float circleArea;
circleArea = 3.14 * radius * radius;
cout<<"Area of Circle = "<<circleArea<<endl;
float circumference ;
circumference = 2 * 3.14 * radius;
cout<<"circumference Of circle = "<<circumference;
#include <iostream>
using namespace std;
int main() {
int x, y;
cin>>x;
cin>>y;
int i=2;
cout<<++i;
}
Problem No 26: Write a program that asks the user to enter two integers,
obtains the numbers from the user, and 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 num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
if ( num1 == num2 ) cout << "These numbers are equal." << endl;
if ( num1 > num2 )
cout << num1 << " is larger." << endl;
if ( num2 > num1 ) cout << num2 << "
is larger." << endl;
return 0;
}
Problem No 27: Write a program that reads two integers and determines
and prints if the first is a multiple of the second. [Hint: Use the modulus
operator.]
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
if ( num1 % num2 == 0 ) {
cout << num1 << " is a multiple of " << num2 <<
endl;
}
if ( num1 % num2 != 0 ) {
cout << num1 << " is not a multiple of " << num2
<< endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main() {
int i;
for(i=1;i<=1024;i*=2){
cout<<i<<" ";
}
return 0;
}
#include<iostream>
using namespace std;
int main() {
int num, factorial=1;
cout<<" Enter Number To Find Its
Factorial:";
cin>>num;
for(int a=1;
a<=num;a++) {
factorial=factorial*a;
cout<<"Factorial of Given Number is
="<<factorial<<endl;
}
return 0;
}
Problem No 30: Write a program to display all even numbers till 150.
#include<iostream>
using namespace std;
int main(){
int i=1;
while(i<=50){
if(i%2==0)
{
cout<<i<<endl;
}
i++;
}
}
Problem No 31: Write a program to display all odd numbers till 100.
#include<iostream>
using namespace std;
int main(){
int i=1;
while(i<=100){
if(i%2!=0)
{
cout<<i<<endl;
}
i++;
}
Problem No 32: Write a program to display all the devisors of 7 till 150.
#include<iostream>
using namespace std;
int main() {
int x,y,z;
x=7;y=1; z=7; while(x<147){
x=z;
x=x*y;
cout<<x<<","; y=y+1;
}
}
#include<iostream>
using namespace std;
int main()
{
int num, d;
cout<<"Enter the number to find the devisors: "<<endl;
cin>>num;
cout<<"Factors of number are "<<endl;
for(d = 1 ; d <= num; ++d)
{
if(num % d == 0)
cout<<d<<endl;
}
return 0;
}
Problem No 34: Find the youngest brother among Ali, Asad & Fakhir,
where user will input the age of them in years.
#include<iostream>
using namespace std;
int main(){
int a, as, f;
cout<<"Age of Ali: "<<endl;
cin >> a;
cout << "Age of Asad: "<<endl;
cin >> as;
cout<<"Age of Fakhir: "<<endl;
cin >> f;
if (a <= as && a <= f){
cout<<"Ali is the youngest brother. "<<endl;
}
else if(as <= a && as <= f){
cout<<"Asad is youngest. "<<endl;
}
else cout<<"Fakhir is youngest. "<<endl;
}
#include<iostream>
int main(){
cout<<"The series is
1+2+3+4+5+6.....+n.
"<<endl;
cout<<"Enter the
value of n: "<<endl;
cin>>s;
for(count=1;
count<=s; count++)
sum+=count;
cout<<"Sum
"<<sum;
#include<iostream>
int main(){
int marks;
cout<<"Enter the
obtained marks out of
100: "<<endl;
cin>>marks;
if(marks>=80)
{
cout<<"Grade
A"<<endl;
}
else if(marks>=70
&& marks<80)
{
cout<<"Grade
B"<<endl;
}
else if(marks>=60
&& marks<70)
{
cout<<"Grade
C "<<endl;
}
else if(marks>=50
&& marks<60){
cout<<"Grade
D"<<endl;
}
else {
cout<<"Fail"<<endl;
}
return 0;
}
Problem No 37: Write a program to display 2X where user will input the
value in x.
#include<iostream>
main() {
int x, y=1;
cout<<"Enter the
number=";
cin>>x;
while(x>0) {
y=x*2; x--;
}
cout<<y;
}
Problem No 38: Write a program to display yX where user will input the
value in x & y.
#include<iostream>
main()
{
int c=1; int
x,y,z;
cin>>x;
cin>>y;
while(y>0)
{
z=x*c; c=z;
y=y-1;
}
cout<<z;
}