0% found this document useful (0 votes)
26 views8 pages

Midterm Revsion C++

The document contains examples of C++ programs for calculating the sum and average of numbers, using arrays, if/else statements, and switch statements. It also demonstrates getting input from the user, performing calculations, and outputting results to the screen. The examples progress from basic calculations to storing values in arrays, comparing numbers, and mapping letter grades to text equivalents.

Uploaded by

shamma0721
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views8 pages

Midterm Revsion C++

The document contains examples of C++ programs for calculating the sum and average of numbers, using arrays, if/else statements, and switch statements. It also demonstrates getting input from the user, performing calculations, and outputting results to the screen. The examples progress from basic calculations to storing values in arrays, comparing numbers, and mapping letter grades to text equivalents.

Uploaded by

shamma0721
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Midterm Revsion C++

Lecture 2

Example 1

Find the sum of 2 numbers

#include <iostream>
using namespace std;

int main() {

int num1, num2 ,sum;

cout<<"Enter the first Number: ";


cin >> num1 ;

cout<<"Enter the second Number: ";


cin >> num2 ;

sum = num1 + num2;

cout<<"The sum of "<< num1 << " and "


<< num2 << " is: " <<sum<< endl;

return 0;
}

Example 2
Find the average of 3 numbers.
#include <iostream>
using namespace std;

int main() {

int num1, num2 ,num3 , avg;

cout<<"Enter the first Number: ";


cin >> num1 ;

cout<<"Enter the second Number: ";


cin >> num2 ;

cout<<"Enter the third Number: ";


cin >> num3 ;

avg = (num1 + num2 + num3)/3 ;

cout<<"The average of "<< num1 << " , " << num2 << "
and "<< num3 << " is: " <<avg<< endl;

return 0;
}
Lecture 3

Example 1
• Ask the user to enter two
numbers.
• The program needs to
calculate the sum of both
numbers.
• Both numbers and the sum
need to be saved in one array
#include <iostream>
#include <array>
using namespace std;

int main() {

array<int, 3> numbers;

cout << "Enter the first number: ";


cin >> numbers[0];

cout << "Enter the second number: ";


cin >> numbers[1];

numbers[2] = numbers[0] + numbers[1];

cout << "The sum of " << numbers[0] << " and " <<
numbers[1] << " is " << numbers[2] << endl;

return 0;
}
Example 2

int q[4]= {4,6,9,2};


int r[4]= {1,3,5,8};
q[1]=r[0];
q[2]=r[1];

• What are the new values of the array q and r?


q[4]= {4,1,3,2}
r[4]= {1,3,5,8}

Example 3

int q[4]= {4,6,9,2};


int r[4]= {1,3,5,8};
cout<<q[0]*r[1]<<endl;
cout<<r[3]*q[2];
• What is the output on the screen?
12
72

Example 4

Finding the Highest Value in an Array Finding the Lowest Value in anArray

Lecture 4

Example 1
• Write a code that will ask the user to enter a number.
1. Check if the number is less than 5, greater than 5 or equal to 5.
2. If the number is less than 5, print “the number is less than 5”.
3. If the number is greater than 5, print “the number is greater than 5”.
4. If the number is equal to 5, print “the number is equal to 5”.

#include <iostream>
using namespace std;

int main() {
int number;
cout<<"Enter a number: ";
cin >> number;

if (number < 5 ){
cout<<"The number is less than 5 " << endl;

} else if (number > 5 ){


cout<< "The number is greater than 5 " << endl;

} else {
cout<<"The number is equal to five " << endl;
}
Example 2

• Write a code that will ask the user to enter two numbers.
• Check if the first number is less than the second number, or greater than or equal to it.
• If the first number is less than the second number, print “the first number is less than the second
number”.
• If the first number is greater than the second number, print “the first number is greater than the
second number”.
• If the first number is equal to the second number, print “the two numbers are equal”.

#include <iostream>
using namespace std;

int main() {
int num1,num2;

cout<<"Enter the first number: ";


cin>>num1;

cout<<"Enter the second number: ";


cin>>num2;

if (num1< num2){
cout<<"The first number is less than second number " <<
endl;

}
else if (num1 > num2){
cout<<" The first number is greater than second number " <<
endl;
}
else {
cout<<"The two numbers are equal " <<endl;
}
return 0;

}
Example 3

Write a code that will ask the user to enter a letter grade in capital letters.
The job of the code is to print the “performance” equivalent to that letter grade.
Let A is equivalent to Excellent.
B is equivalent to V.good.
C is equivalent to Good.
D is equivalent to Pass.

#include <iostream>

int main() {
char grade;

std::cout << "Enter the letter grade (A, B, C,


or D): ";
std::cin >> grade;

switch (grade) {
case 'A':
std::cout << "Excellent" << std::endl;
break;
case 'B':
std::cout << "Very Good" << std::endl;
break;
case 'C':
std::cout << "Good" << std::endl;
break;
case 'D':
std::cout << "Pass" << std::endl;
break;
default:
std::cout << "Invalid grade entered." <<
std::endl;
break;
}

return 0;
}

You might also like