Note: Try to solve these questions on your own before checking the solutions.
Filter:
Sort:
Exercise 1: Calculate Y
Write a function named GetY(), which receives the value of X and computes and returns the value of Y, as follows:
\[Y = X^2 + 3\sqrt{X} + b\]
Where b is a constant value defined in the program (let b = 7).
In main(), you need to input the value for X and pass it to the function, then receive the value and print it out to the user.
Example:
Enter value for X: 4
The value of Y is: 29.00
(Calculation: \(16 + 3(2) + 7 = 29\))
Solution
#include<stdio.h>
#include<math.h>doubleGetY(doublex){intb=7;doubley=(x*x)+(3*sqrt(x))+b;returny;}intmain(){doubleX,result;printf("Enter value for X: ");scanf("%lf",&X);result=GetY(X);printf("The value of Y is: %.2lf\n",result);return0;}
#include<iostream>
#include<iomanip>
#include<cmath>usingnamespacestd;doubleGetY(doublex){intb=7;doubley=(x*x)+(3*sqrt(x))+b;returny;}intmain(){doubleX,result;cout<<"Enter value for X: ";cin>>X;result=GetY(X);cout<<"The value of Y is: "<<fixed<<setprecision(2)<<result<<endl;return0;}
Exercise 2: Circle Properties
Write a function named CircleArea(), which receives the R (Radius) of the circle and computes both the Area and Perimeter of the Circle.
You need to display the values of Area and Perimeter within the same function.
In main(), you need to input the value for R and pass it to the function, then print out the results within the same function.
Example:
Enter the radius (R): 5
Area of the circle: 78.54
Perimeter of the circle: 31.42
Solution
#include<stdio.h>constdoublePI=3.14159;voidCircleArea(doubler){doublearea=PI*r*r;doubleperimeter=2*PI*r;printf("Area of the circle: %.2lf\n",area);printf("Perimeter of the circle: %.2lf\n",perimeter);}intmain(){doubleradius;printf("Enter the radius (R): ");scanf("%lf",&radius);CircleArea(radius);return0;}
#include<iostream>
#include<iomanip>usingnamespacestd;constdoublePI=3.14159;voidCircleArea(doubler){doublearea=PI*r*r;doubleperimeter=2*PI*r;cout<<fixed<<setprecision(2);cout<<"Area of the circle: "<<area<<endl;cout<<"Perimeter of the circle: "<<perimeter<<endl;}intmain(){doubleradius;cout<<"Enter the radius (R): ";cin>>radius;CircleArea(radius);return0;}
Exercise 3: Calculator Function
Implement a function named Calc(), that receives 2 integer arguments n1, n2 and performs the basic mathematical operations (you need to display the results within the same function):
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%)
In main(), you need to input the value for n1, n2 and pass them to the function, then display the results within the same function.
Note: The solution does not handle division by zero as conditional statements have not been covered yet.
Example:
Enter integer number 1: 10
Enter integer number 2: 5
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulation: 0
Solution
#include<stdio.h>voidCalc(intn1,intn2){printf("Addition: %d\n",n1+n2);printf("Subtraction: %d\n",n1-n2);printf("Multiplication: %d\n",n1*n2);printf("Division: %d\n",n1/n2);printf("Modulation: %d\n",n1%n2);}intmain(){intnum1,num2;printf("Enter integer number 1: ");scanf("%d",&num1);printf("Enter integer number 2: ");scanf("%d",&num2);Calc(num1,num2);return0;}
#include<iostream>usingnamespacestd;voidCalc(intn1,intn2){cout<<"Addition: "<<n1+n2<<endl;cout<<"Subtraction: "<<n1-n2<<endl;cout<<"Multiplication: "<<n1*n2<<endl;cout<<"Division: "<<n1/n2<<endl;cout<<"Modulation: "<<n1%n2<<endl;}intmain(){intnum1,num2;cout<<"Enter integer number 1: ";cin>>num1;cout<<"Enter integer number 2: ";cin>>num2;Calc(num1,num2);return0;}
Exercise 4: Average of Digits
Implement a function named DigitsNum(), that receives a 3-digit integer argument num and returns the average of its digits.
In main(), you need to input the value for num and pass it to the function, then receive the value and print it out to the user.
Example:
Enter a 3-digit integer: 123
The average of the digits is: 2.00
(Calculation: $$ (1+2+3)/3 = 2 $$)
Solution
#include<stdio.h>doubleDigitsNum(intnum){intd1,d2,d3;d1=num%10;d2=(num/10)%10;d3=num/100;doublesum=d1+d2+d3;returnsum/3;}intmain(){intnumber;doubleaverage;printf("Enter a 3-digit integer: ");scanf("%d",&number);average=DigitsNum(number);printf("The average of the digits is: %.2lf\n",average);return0;}
#include<iostream>
#include<iomanip>usingnamespacestd;doubleDigitsNum(intnum){intd1,d2,d3;d1=num%10;d2=(num/10)%10;d3=num/100;doublesum=d1+d2+d3;returnsum/3;}intmain(){intnumber;doubleaverage;cout<<"Enter a 3-digit integer: ";cin>>number;average=DigitsNum(number);cout<<"The average of the digits is: "<<fixed<<setprecision(2)<<average<<endl;return0;}
Exercise 5: Currency Converter
Implement a function named CurrencyConvertor(), which receives an amount of money (double value) in JoD (Jordanian Dinar) then converts it to USD, suppose that:
\[1 \text{ JoD} = 1.4112 \text{ USD}\]
Example:
Enter amount in JOD: 100
Amount in USD: 141.1200
Solution
#include<stdio.h>doubleCurrencyConvertor(doublejod){constdoublerate=1.4112;returnjod*rate;}intmain(){doublemoney_jod,money_usd;printf("Enter amount in JOD: ");scanf("%lf",&money_jod);money_usd=CurrencyConvertor(money_jod);printf("Amount in USD: %.4lf\n",money_usd);return0;}
#include<iostream>
#include<iomanip>usingnamespacestd;doubleCurrencyConvertor(doublejod){constdoublerate=1.4112;returnjod*rate;}intmain(){doublemoney_jod,money_usd;cout<<"Enter amount in JOD: ";cin>>money_jod;money_usd=CurrencyConvertor(money_jod);cout<<"Amount in USD: "<<fixed<<setprecision(4)<<money_usd<<endl;return0;}
Exercise 6: Distance in 2D Space
Write a function named Distance2D() that receives the 2D coordinates $(x, y)$ of two points and computes the Euclidean distance between them.
In main(), you need to input the coordinates for both points, pass them to the function, and print the calculated distance to the user.
Example:
Enter coordinates for Point 1 (x1 y1): 1.5 2.0
Enter coordinates for Point 2 (x2 y2): 4.5 6.0
The distance between the points is: 5.00
Solution
#include<stdio.h>
#include<math.h>doubleDistance2D(doublex1,doubley1,doublex2,doubley2);intmain(){doublex1,y1,x2,y2;printf("Enter coordinates for Point 1 (x1 y1): ");scanf("%lf %lf",&x1,&y1);printf("Enter coordinates for Point 2 (x2 y2): ");scanf("%lf %lf",&x2,&y2);doubledist=Distance2D(x1,y1,x2,y2);printf("The distance between the points is: %.2f\n",dist);return0;}doubleDistance2D(doublex1,doubley1,doublex2,doubley2){returnsqrt(pow(x2-x1,2)+pow(y2-y1,2));}
#include<iostream>
#include<cmath>
#include<iomanip>usingnamespacestd;doubleDistance2D(doublex1,doubley1,doublex2,doubley2);intmain(){doublex1,y1,x2,y2;cout<<"Enter coordinates for Point 1 (x1 y1): ";cin>>x1>>y1;cout<<"Enter coordinates for Point 2 (x2 y2): ";cin>>x2>>y2;doubledist=Distance2D(x1,y1,x2,y2);cout<<"The distance between the points is: "<<fixed<<setprecision(2)<<dist<<endl;return0;}doubleDistance2D(doublex1,doubley1,doublex2,doubley2){returnsqrt(pow(x2-x1,2)+pow(y2-y1,2));}