0% found this document useful (0 votes)
7 views7 pages

Programming 2

The document provides C++ programs for various tasks: finding the largest and average of three numbers, checking if a number is positive/negative/zero and even/odd, grading a student's result, and printing the day of the week based on a number input. Each task includes pseudocode, flowcharts, and the corresponding C++ code. The programs demonstrate basic input/output operations, conditional statements, and arithmetic calculations.

Uploaded by

NEFA. JR
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)
7 views7 pages

Programming 2

The document provides C++ programs for various tasks: finding the largest and average of three numbers, checking if a number is positive/negative/zero and even/odd, grading a student's result, and printing the day of the week based on a number input. Each task includes pseudocode, flowcharts, and the corresponding C++ code. The programs demonstrate basic input/output operations, conditional statements, and arithmetic calculations.

Uploaded by

NEFA. JR
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

1.

Write a C++ program to read any three numbers and print the largest and the average value of
it.

Pseudocode:

− Input three numbers a, b, c.


− Declare one of variable as the largest.
− If b > a , declare b the largest.
− If c > b, declare c the largest.
− Print the largest.
− Declare a new variable, average = (a+b+c)/3
− Print average.

Flowchart:
Code:

#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter 3 numbers: ";
cin >> a >> b >> c;
int largest = a;
if (b > largest) {
largest = b;
}
if (c > largest) {
largest = c;
}
float average = (a + b + c) / 3;
cout << "The largest number is " << largest << endl;
cout << "The average is " << average << endl;
return 0;
}
2. Write a C++ program to read a number and check if it’s positive, negative or zero, even or odd
and print a word that tells us above the number, add it to a total, decrement it by 2 and display
suitable messages in each case (total and new value after decrement).

Pseudocode:

− Input number
− If number > 0 then
print “positive”
Else if number < 0 then
Print “negative”
Else print “Zero”
− If number % 2 = 0 then
Print “even”
Else print “odd".
− Declare new variable total = 0
− Add number to total
− Decrement number by 2
− Print total
− Print number
Flowchart:

Code:
#include <iostream>
using namespace std;
int main() {
int num, total = 0;
cout << "Enter a number: ";
cin >> num;
if (num > 0) {
cout << "Positive" << endl; }
else if (num < 0) {
cout << "Negative" << endl; }
else {
cout << "Zero" << endl; }
if (num % 2 == 0) {
cout << "Even" << endl; }
else {
cout << "Odd" << endl; }
total += num;
num -= 2;
cout << "Total after addition: " << total << endl;
cout << "New value after decrement: " << num << endl;
return 0;
}
3. Write a C++ program to read a student result, and check if it’s result greater than or equal to
90, then print A+, greater than or equal to 75, then print A, greater than or equal to 50, then
print A-, otherwise print fail.

Pseudocode:

− Input result
− If result >= 90 then
Print “A+”
Else if result >= 75 them
Print “A”
Else if result >= 50
Print “A-”
Else
Print “fail

Flowchart:
Code:

#include <iostream>
using namespace std;
int main() {
int result;
cout << "Enter student's result:" << endl;
cin >> result;
if (result >= 90) {
cout << "A+"; }
else if (result >= 75) {
cout << "A"; }
else if (result >= 50) {
cout << "A-"; }
else {
cout << "fail"; }
return 0;
}
4. Write a C++ program to read a number from 1 to 7, and print the day of the week from Sunday
to Monday.

Pseudocode:

− Input day
− If day = 1 then
Print Sunday
Else if day = 2 then
Print Monday
Else if day = 3 then
Print Tuesday
Else if day = 4 then
Print Wednesday
Else if day = 2 then
Print Thursday
Else if day = 2 then
Print Friday
Else if day = 2 then
Print Saturday
Else
Print Invalid input.
Flowchart:

Code:
#include <iostream>
using namespace std;
int main() {
int day;
cout << "Enter a number between 1 & 7: ";
cin >> day;
switch (day) {
case 1: cout << "Sunday"; break;
case 2: cout << "Monday"; break;
case 3: cout << "Tuesday"; break;
case 4: cout << "Wednesday"; break;
case 5: cout << "Thursday"; break;
case 6: cout << "Friday"; break;
case 7: cout << "Saturday"; break;
default: cout << "Invalid input. ";
}
return 0;
}

You might also like