Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

SUPERIOR UNIVERSITY

ASSIGNMENT NO. 4
PROGRAMING FUNDANETAL

Academic Year: 2022 – 2026


BS ROBOTICS

Abdul Muizz | Enrollment No.


PF / SIR AHMED BILAL
Date of Submission: March 11, 2023
Q1: Write a program to print the sum of two numbers entered by user by defining
your own function.
Code:
#include<iostream>

using namespace std;

// function to calculate the sum of two numbers

int sum(int num1, int num2){

return num1 + num2;

int main(){

int a, b;

cout<<"Enter first number: ";

cin>>a;

cout<<"Enter second number: ";

cin>>b;

// calling the function to calculate the sum and storing the result in variable s

int s = sum(a, b);

// displaying the result

cout<<"The sum is: "<<s<<endl;

return 0;

}
Screenshot:
Q2: Define two functions to print the maximum and the minimum number
respectively among three numbers entered by user.
Code:
#include<iostream>

using namespace std;

// function to find the maximum of three numbers

int max(int num1, int num2, int num3){

int m = num1;

if(num2 > m) m = num2;

if(num3 > m) m = num3;

return m;

// function to find the minimum of three numbers

int min(int num1, int num2, int num3){

int m = num1;

if(num2 < m) m = num2;

if(num3 < m) m = num3;

return m;

int main(){

int a, b, c;

cout<<"Enter three numbers: ";

cin>>a>>b>>c;

// calling the functions to find the maximum and minimum of the three numbers and displaying the results

cout<<"Maximum: "<<max(a, b, c)<<endl;

cout<<"Minimum: "<<min(a, b, c)<<endl;

return 0;

}
Screenshot:
Q3: A person is eligible to vote if his/her age is greater than or equal to 18. Define a
function to find out if he/she is eligible to vote.
Code:
#include<iostream>

using namespace std;

// function to check if a person is eligible to vote

bool isEligible(int age){

if(age >= 18) return true;

else return false;

int main(){

int a;

cout<<"Enter your age: ";

cin>>a;

// calling the function to check if the person is eligible to vote and displaying the result

if(isEligible(a)) cout<<"You are eligible to vote."<<endl;

else cout<<"You are not eligible to vote."<<endl;

return 0;

}
Screenshot:
Q4: Write a program which will ask the user to enter his/her marks (out of 100).
Define a function that will display grades according to the marks entered as below:
Marks Grade
91-100 A+
81-90 A
71-80 B
61-70 C
51-60 D
41-50 E

Code:
#include<iostream>

using namespace std;

// function to display the grade based on the marks obtained

void displayGrade(int marks){

if(marks >= 91 && marks <= 100) cout<<"Grade: A+"<<endl;

else if(marks >= 81 && marks <= 90) cout<<"Grade: A"<<endl;

else if(marks >= 71 && marks <= 80) cout<<"Grade: B"<<endl;

else if(marks >= 61 && marks <= 70) cout<<"Grade: C"<<endl;

else if(marks >= 51 && marks <= 60) cout<<"Grade: D"<<endl;

else if(marks >= 41 && marks <= 50) cout<<"Grade: E"<<endl;

else cout<<"Grade: F"<<endl;

int main(){

int marks;

cout<<"Enter your marks: ";

cin>>marks;

// calling the function to display the grade based on the marks obtained

displayGrade(marks);

return 0;

}
Screenshot:

You might also like