0% found this document useful (0 votes)
10 views6 pages

Programming For Problem Solving: Experiment 2 - 2

The document outlines several programming tasks related to problem-solving in C++. It includes evaluating expressions, calculating distances between points, finding the largest of three numbers, displaying sizes of data types, and predicting outputs of code snippets without execution. Each task is accompanied by sample code and expected outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Programming For Problem Solving: Experiment 2 - 2

The document outlines several programming tasks related to problem-solving in C++. It includes evaluating expressions, calculating distances between points, finding the largest of three numbers, displaying sizes of data types, and predicting outputs of code snippets without execution. Each task is accompanied by sample code and expected outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAMMING FOR PROBLEM SOLVING

EXPERIMENT 2 - 2
Task 1:
Evaluate the following expression without using code blocks.
int i =9, j=6;
float x =0.5, y =0.1;
char a ='a', b ='b';
Expression Output (With Justification)
a. (3 * i – 2 * j) % (2 * a - b) 15
b. (x>y) && (i>0) && (j>5) 10320
c. a==99 0
d. for a = 5 and b = 10 102
z = (a < b)? a+b : a - b
e. i = 10; 21
i++;
a = i + 10;
f. for i = 10, j = 5 10
z = (i + 10 < j)? 100:10
g. with a=1, b=2, c=12, d=2, e=5, 10
f=2
a = b += c++ − d + −−e/−f;
h. with x=12, y=7, z 1
z= x!=4 || y==2;
i. with int i=-3, j=2, k=0, m 1
m = ++i || ++j && ++k;

Task 2:
Write a C++ program that accepts coordinates of two-dimensional points A and B and prints
out (using two decimal places) the distance between A and B. It also prints out the
coordinates (using two decimal places) of the midpoint of A and B.
Test data and expected output:
Enter coordinates of points A: (-1, 3)
Enter coordinates of points B: (2, -1)
Distance between A and B is 5.00
The coordinates of midpoint of A and B are (0.50,1.00)
Program:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double distance, x1, y1, x2, y2, x, y;
cout<<”\n Enter the values of x1,y1:”;
cin>>x1>>y1;
cout<<”\n Enter the values of x2,y2:”;
cin>>x2>>y2;
distance=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
cout<<”\n Distance between A and B is = “<<distance;
x=(x1+x2)/2;
y=(y1+y2)/2;
cout<<”\nThe coordinates of midpoint x and y“<<x<<”,“<<y;
return 0;
}
Output:
Enter the values of x1, y1: -1 3
Enter the values of x2, y2: 2 -1
Distance between A and B is = 5 The coordinates are 0.5, 1
Task 3:
Write a program to find largest of three numbers using conditional operator
Program:
#include<iostream>
using namespace std;
int main()
{
int a,b,c,max;
cout<<”Enter a,b,c:”;
cin>>a>>b>>c;
max=(a>b)?(a>c)?a:c:(b>c)?b:c;
cout<<”The greatest among “<<a<<”,”<<b<<”,”<<c<<” is:: “<<max;
return 0;
}
Output:
Enter a,b,c:2,3,5
The greatest among 2,3,5 is:: 5

Task 4:
Write a program to display size of data types like int, float, char, double
Program:
#include<iostream>
using namespace std;
int main()
{
int I;
sizeof(int);
sizeof(float);
sizeof(char);
sizeof(double);
cout<<”\n The sizeof(int) = “<<sizeof(int) <<” Bytes”;
cout<<”\n The sizeof(float) = “<<sizeof(float) <<” Bytes”;
cout<<”\n The sizeof(char) = “<<sizeof(char) <<” Bytes”;
cout<<”\n The sizeof(double) = “<<sizeof(double) <<” Bytes”;
return 0;
}
Output:
The sizeof(int) = 4 Bytes
The sizeof(float) = 4 Bytes
The sizeof(char) = 1 Bytes
The sizeof(double) = 8 Bytes

Task 5:
Find the output of following code snippets without running the code on codeblocks. Justify
your answer.
a. int main()
{
int d=14, m=7, y=4696, c=0, val;
val= (d+m+y+(y/4)+c)%7;
cout<<val;
return 0;
}
Output: 4
b. if(a<b)
if(b>c)
statement1;
else
statement2;
statement 2 will be executed if?
Ans: If statement 1 is not satisfied then statement 2 will be executed.

c. int main()
{
int x, y=25, z=25;
x=y==z;
cout<<x;
}
Output: 1

d. int main()
{
int a=20, b=30, c=40;
if(c>b>a)
cout<<”True”;
else
cout<<”False”;
return 0;
}
Output : False

e. int main()
{
int a=10, b=4, res;
res=a++;
cout<<a<<res;
res=a--;
cout<<a<<res;
res=++a;
cout<<a<<res;
res=--a;
cout<<a<<res;
return 0;
}
Output : :1110101111111010

f. int main()
{
int x=7, y=10;
if (x && y>20)
cout<<x;
else
cout<<y;
return 0;
}
Output : 10

You might also like