Problem 1: (Multiples) Write a program that inputs two integers and determines a
ID: 3753499 • Letter: P
Question
Problem 1: (Multiples) Write a program that inputs two integers and determines and prints if the first is a multiple of the second. [Hint: Use the modulus operator.]
Problem 2: (Digits of an Integer) Write a program that inputs a five-digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each in reverse order.
[Hint:Use the integer division and modulus operators.]
For example, if the user types in 42339, the program should print 9 3 3 2 4
Problem3: ( Circle Formulas) Write a program that reads the radius of a circle as a positive integer , and computes and prints the diameter, the circumference and the area. Use the value 3.14159 for PI.
Problem4: (Sides of a Right Triangle) Write a program that reads three nonzero integers and determines and prints whether they could be the sides of a right triangle. [ Hint: Use Pythagorean formula , a*a +b*b = c*c
where a and b are side1 and side2 respectively, and c is the hypotenuse.
Provide four examples, two for yes case and two for no case.
Problem 5: ( Geometric Sequence and Series ) Write a program to generate a geometric sequence with first term a=1, and common ratio r = 0.5. Use a while loop to generate 10 numbers in the sequence. Also, find the finite sum of the resulting geometric series.
Explanation / Answer
#include <iostream>
using namespace std;
void Multiples(int first_number, int second_number) //Problem 1
{
if(first_number % second_number == 0)
{
cout<<first_number<<" is a multiple of "<<second_number<<endl;
}
else
{
cout<<first_number<<" is not a multiple of "<<second_number<<endl;
}
}
void DigitsOfAnInteger(int five_digit_number) //Problem 2
{
int arr[5];
int i;
int d=10000;
for(i=0;i<5;i++)
{
arr[i]=five_digit_number/d;
five_digit_number=five_digit_number%d;
d=d/10;
}
for(i=4;i>=0;i--)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void CircleFormulas(int radius) //Problem 3
{
float pi=3.14159;
float diameter=2*radius;
cout<<"Diameter of circle(radius r="<<radius<<") :"<<diameter<<endl;
float circumference=2*pi*radius;
cout<<"Area of circle(radius r="<<radius<<") :"<<circumference<<endl;
float area=pi*radius*radius;
cout<<"Area of circle(radius r="<<radius<<") :"<<area<<endl;
}
void SidesOfARightTriangle(int side1, int side2, int hypotenuse) //Problem 4
{
int side_ab = side1*side1 + side2*side2;
int side_c = hypotenuse*hypotenuse;
if(side_ab == side_c)
{
cout<<"They are sides of a right triangle"<<endl;
}
else
{
cout<<"They are not sides of a right triangle"<<endl;
}
}
void GeometricSequenceAndSeries() //Problem 5
{
int a=1;
float r=0.5;
float sum=0;
float seq=a;
int i=10;
cout<<"Geometric Sequence of a=1 and r=0.5 :"<<endl;
while(i>0)
{
seq = seq*r;
cout<<seq<<endl;
sum = sum + seq;
i--;
}
cout<<"Sum of Geometric Sequence : "<<sum;
}
int main() {
Multiples(4,2); //Problem 1
Multiples(10,3); //Problem 1
DigitsOfAnInteger(12345); //Problem 2
CircleFormulas(10); //Problem 3
SidesOfARightTriangle(3,4,5); //Problem 4, yes case1
SidesOfARightTriangle(12,5,13); //Problem 4, yes case2
SidesOfARightTriangle(2,3,4); //Problem 4, no case1
SidesOfARightTriangle(5,6,7); //Problem 4, no case2
GeometricSequenceAndSeries(); //Problem 5
return 0;
}