0% found this document useful (0 votes)
8 views

C++ Assignment Solutions - Fundamentals of Programming - 1 - Week2

Uploaded by

ans78hu
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)
8 views

C++ Assignment Solutions - Fundamentals of Programming - 1 - Week2

Uploaded by

ans78hu
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

C++ Assignment Solutions | Fundamentals of Programming -1 | Week2

Take 2 integers input and print the greatest of them

Input 1: a = 5 b = 7

Output 1: second number 7 is the largest.

Solution:

#include <iostream>

using namespace std;

int main() {
int num1, num2;
cout << "Enter first number:";
cin >> num1;
cout << "Enter second number:";
cin >> num2;
if (num1 > num2) {
cout << "First number " << num1 << " is the largest";
} else {
cout << "Second number " << num2 << " is the largest";
}
return 0;
}

Given the radius of the circle, predict whether numerically the area of this circle is larger than the
circumference or not.

Input 1: radius = 4

Output 1: Area is greater than circumference.

Explanation: area = 3.14 * 4 * 4 = 50.24 unit2

Perimeter = 2 * 3.14 * 4 = 25.12 unit

Therefore area > perimeter.


Solution:

#include <iostream>

using namespace std;

int main() {
int radius;
cout << "Enter the radius : ";
cin >> radius;

float area = 3.14 * radius * radius;


float circumference = 2 * 3.14 * radius;
if (area > circumference) cout << "Area is greater than circumference." << endl;
else cout << "Circumference is greater than area." << endl;
return 0;
}

Any year is input through the keyboard. Write a program to determine whether the year is a leap year
or not. (Considering leap year occurs after every 4 years)

Input 1: 1976

Output: yes

Input 2: 2003

Output: no

Solution:
#include <iostream>

using namespace std;

int main() {
int year;
cout << "Enter a year: ";
cin >> year;

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
cout << year << " is a leap year.";
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
cout << year << " is not a leap year.";
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
cout << year << " is a leap year.";
}
// all other years are not leap years
else {
cout << year << " is not a leap year.";
}

return 0;
}

Given the length and breadth of a rectangle, write a program to find whether numerically the area of
the rectangle is greater than its perimeter.

Input 1: length = 5 breadth = 7

Output 1: Area is greater than perimeter.

Solution:
#include <iostream>

using namespace std;

int main() {
int length, breadth;
cout << "Enter the length and breadth of the rectangle respectively : ";
cin >> length >> breadth;

int area = length * breadth;


int perimeter = 2 * (length + breadth);
if (area > perimeter) cout << "Area is greater than perimeter.";
else cout << "Perimeter is greater than area.";
return 0;
}

Write a program to input sides of a triangle and check whether a triangle is equilateral, scalene or
isosceles triangle.

Input : side1 = 5 side2 = 4 side3 = 4

Output: This is an Isosceles triangle.

Solution:

#include<iostream>

using namespace std;

int main() {
int side1, side2, side3;

cout << "Please Enter Three Sides of a Triangle = ";


cin >> side1 >> side2 >> side3;

if (side1 == side2 && side2 == side3) {


cout << "This is an Equilateral Triangle";
} else if (side1 == side2 || side2 == side3 || side1 == side3) {
cout << "This is an Isosceles Triangle";
} else
cout << "This is a Scalene Triangle";

return 0;
}

If the marks of A, B and C are input through the keyboard, write a program to determine the student
scoring least marks.

Input 1: A = 23 , B = 34 , C = 71

Output : A scores the least marks


Solution:

#include <bits/stdc++.h>

using namespace std;

int main() {
cout << "Enter marks of the students : ";
int a, b, c;
cin >> a >> b >> c;

if (a <= b && a <= c)


cout << "A scores the least marks";

else if (b <= a && b <= c)


cout << "B scores the least marks";

else
cout << "C scores the least marks";

return 0;
}

Given a point (x, y), write a program to find out if it lies on the x-axis, y-axis or at the origin, viz. (0, 0).

Input 1: 2 0

Output 1: the point lies on the x - axis.

Solution:

#include<iostream>

using namespace std;

int main() {
float x, y;
printf("Enter the x-y coordinates of the point : ");
cin >> x >> y;

if (x == 0 && y == 0)
cout << "The point is on the origin.";
if (x == 0 && y != 0)
cout << "The point lie on the y-axis.";
if (x != 0 && y == 0)
cout << "The points lie on the x-axis.";
if (x != 0 && y != 0)
cout << "The points lie on the plane.";
return 0;
}
Given three points (x1, y1), (x2, y2) and

(x3, y3), write a program to check if all the three points fall on one straight line.

Input 1: x1 = 1 , y1 = 2 , x2 = 2 , y2 = 3 , x3 = 3 , y3 = 4

Output 1: All 3 points lie on the same line.

Solution:

#include <iostream>

using namespace std;


int main() {
float x1, y1, x2, y2, x3, y3, slope1, slope2;

cout << "Enter points (x1, y1)" << endl;


cin >> x1 >> y1;

cout << "Enter points (x2, y2)" << endl;


cin >> x2 >> y2;

cout << "Enter points (x3, y3)" << endl;


cin >> x3 >> y3;

slope1 = (y2 - y1) / (x2 - x1);


slope2 = (y3 - y2) / (x3 - x2);

if (slope1 == slope2) {
cout << "All 3 points lie on the same line";
} else {
cout << "All 3 points do not lie on the same line";
}

return 0;
}

Write a C++ program to input any character and check whether it is the alphabet, digit or special
character.

Input 1: ch = ‘9’

Output 1: digit

Solution:
#include<iostream>

using namespace std;

int main() {
char ch;
cout << "Enter any character : ";
cin >> ch;

// Alphabet checking condition


if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
cout << ch << " is an Alphabet";
} else if (ch >= '0' && ch <= '9') {
cout << ch << " is a Digit";
} else {
cout << ch << " is a Special Character";
}
return 0;
}

Predict the output of below code

#include<iostream>

using namespace std;


int main() {
int a = 500, b, c;
if (a >= 400)
b = 300;
c = 200;
cout << "value of b and c are respectively " << b << " and " << c;
return 0;
}

Solution:

value of b and c are respectively 300 and 200

You might also like