C++ Assignment Solutions - Fundamentals of Programming - 1 - Week2
C++ Assignment Solutions - Fundamentals of Programming - 1 - Week2
Input 1: a = 5 b = 7
Solution:
#include <iostream>
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
#include <iostream>
int main() {
int radius;
cout << "Enter the radius : ";
cin >> radius;
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>
int main() {
int year;
cout << "Enter a year: ";
cin >> 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.
Solution:
#include <iostream>
int main() {
int length, breadth;
cout << "Enter the length and breadth of the rectangle respectively : ";
cin >> length >> breadth;
Write a program to input sides of a triangle and check whether a triangle is equilateral, scalene or
isosceles triangle.
Solution:
#include<iostream>
int main() {
int side1, side2, side3;
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
#include <bits/stdc++.h>
int main() {
cout << "Enter marks of the students : ";
int a, b, c;
cin >> a >> b >> c;
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
Solution:
#include<iostream>
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
Solution:
#include <iostream>
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>
int main() {
char ch;
cout << "Enter any character : ";
cin >> ch;
#include<iostream>
Solution: