0% found this document useful (0 votes)
67 views

C++ Laboratory Exam: 1 Write A C++ Program That Would Input A Year Code and Output Year

The document contains 10 programming problems related to C++ laboratory exam. The problems cover topics like inputting and outputting values, if/else statements, switch statements, arrays, functions, mathematical operations like factorials, averages etc. Complete C++ programs with explanations are provided as solutions for each problem.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

C++ Laboratory Exam: 1 Write A C++ Program That Would Input A Year Code and Output Year

The document contains 10 programming problems related to C++ laboratory exam. The problems cover topics like inputting and outputting values, if/else statements, switch statements, arrays, functions, mathematical operations like factorials, averages etc. Complete C++ programs with explanations are provided as solutions for each problem.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C++ Laboratory Exam

1 Write a C++ program that would input a year code and output year.
Using if..else statement and compare with switch method.

Year Code Year Level


1 Freshman
2 Sophomore
3 Junior
4 Senior

1 #include<iostream>
2 using namespace std;
3 main()
4 {
5 int i;
6 cout <<"Please enter the year code: ";
7 cin >>i;
8 if (i==1)
9 cout <<"\n"<<"Freshman";
10 else if(i==2)
11 cout <<"\n"<<"Sopomore";
12 else if(i==3)
13 cout <<"\n"<<"Junior";
14 else if (i==4)
15 cout <<"\n"<<"Senior";
16 else
17 cout <<"\n"<<"invalid";
18
19 }

1 #include<iostream>
2 using namespace std;
3 main()
4 {
5 int i;
6 cout <<"Please enter the year code: ";
7 cin >>i;
8 switch (i)
9 {
10 case 1:
11 cout <<"\n"<<"Freshman";
12 break;
13 case 2:
14 cout <<"\n"<<"Sopomore";
15 break;
16 case 3:
17 cout <<"\n"<<"Junior";
18 break;
19 case 4:
20 cout <<"\n"<<"Senior";
21 break;
22 default:
23 cout <<"\n"<<"invalid";
24 }
25 }
26

2 Write a C++ program that would input 10 grades and output the
highest and lowest score as well the total passed and total failed.
1 #include<iostream>
2 using namespace std;
3 main()
4 {
5 int degree[10] = {25,35,45,50,12,17,2,7,8,48};
6 int i, small, large;
7 int sum=0,no_failed=0,no_passed=0;
8 float avg;
9 small = degree[0];
10 large = degree[0];
11 for (i=0; i<10; i++)
12 {
13 if(degree[i]<small)
14 small = degree[i];
15 if(degree[i]>large)
16 large = degree[i];
17 }
18 cout <<"The largest value is: " <<large;
19 cout <<"\n"<<"The smallest value is: "<<small;
20 for(i=0; i<10;i++)
21 {
22 sum = sum + degree[i];
23 }
24 avg = sum/10;
25 cout <<"\n"<<"The average value is: "<<avg;
26 for(i=0; i<10;i++)
27 {
28 if(degree[i]>avg)
29 no_passed = no_passed+1;
30 if(degree[i]<avg)
31 no_failed = no_failed+1;
32 }
33 cout <<"\n"<<"The no of passed student is: "<<no_passed;
34 cout <<"\n"<<"The no of failed student is: "<<no_failed;
35 }

3 Write a C++ program that would input a number and output


its factorial.
1 #include<iostream>
2 using namespace std;
3 main()
4 {
5 int n, factorial,i;
6 cout <<"Please enter a positive integer number: ";
7 cin >> n;
8 for (i = 1; i <= n; i++)
9 {
10 factorial = factorial * i;
11 }
12 cout <<"\n"<<"Factorial of "<<n<<"="<< factorial;
13
14 }

4 Write a C++ program to read 10 numbers then calculate the average


then print how many numbers are greater than the average.
1 #include<iostream>
2 using namespace std;
3 main()
4 {
5 int degree[10]={1,2,5,7,9,3,7,5,8,1};
6 int s,i,avg,sum;
7 sum = 0;
8 s = 10;
9 for (i=0;i<10;i++)
10 {
11 sum = sum + degree[i];
12 }
13 avg = sum/s;
14 cout <<"The average value is: " <<avg;
15 }

5 Write a C++ program that would input Employee first letter of his
name, Rate per hour, No. of hours worked and will compute the
daily wage of an employee. If the number of hours worked exceeds
eight hours add 30% to each excess hours as overtime rate.
Formula: Daily Wage = Rate per hour * No. of hours worked + OT
pay.

1 #include<iostream>
2 using namespace std;
3 main()
4 {
5 char Employee_name;
6 int rate, no_hours, daily_wage;
7 cout << "Please enter the first letter of the employee";
8 cin >> Employee_name;
9 cout << "\n" << "Please enter the rate per hour";
10 cin >> rate;
11 cout << "\n" << "Please enter the no. of hour";
12 cin >> no_hours;
13 daily_wage = rate * no_hours;
14 if(no_hours >= 8)
15 {
16 daily_wage = daily_wage + (30/100) * daily_wage;
17 cout <<"\n"<<"The daily wage is: "<<daily_wage;
18 }
19 else
20 cout <<"\n"<<"The daily wage is: "<<daily_wage;
21
22 }

6 Create a C++ program that would input value of the Radius of a Circle
and output the Diameter, Circumference and Area of a Circle. Pi =
3.1416. Formula: Diameter = 2 * Radius, Circumference = 2 * Pi *
Radius, Area = Pi * Radius * Radius using functions.
1 #include<iostream>
2 using namespace std;
3 float diameter(float x)
4 {
5 int y;
6 y = 2*x;
7 return y;
8 }
9 float circumference(float x)
10 {
11 int y;
12 y = 2*3.1416*x;
13 return y;
14 }
15 float area(float x)
16 {
17 int y;
18 y = 3.1416*x*x;
19 return y;
20 }
21
22 main()
23 {
24 int y;
25 cout <<"Please enter the radius of the circle: ";
26 cin >> y;
27 cout << "\n" << diameter(y);
28 cout << "\n" << circumference(y);
29 cout << "\n" << area(y);
30 }

7 Create a C++ program that would input four integer numbers and
compute and display the sum, difference and product.
1 #include<iostream>
2 using namespace std;
3 int sum(int x,int x1, int x2, int x3)
4 {
5 int y = x+x1+x2+x3;
6 return y;
7 }
8
9 int sub(int x,int x1, int x2, int x3)
10 {
11 int y = x-x1-x2-x3;
12 return y;
13 }
14
15 int prod(int x,int x1, int x2, int x3)
16 {
17 int y = x*x1*x2*x3;
18 return y;
19 }
20
21 main()
22 {
23 int a,b,c,d;
24 cout <<"Please enter four integer numbers: ";
25 cin >>a>>b>>c>>d;
26 cout << "\n" << "The summation of numbers is:
"<<sum(a,b,c,d);
27 cout << "\n" << "The subtraction of numbers is:
"<<sub(a,b,c,d);
28 cout << "\n" << "The production of numbers is:
"<<prod(a,b,c,d);
29 }

8 Write a C++ program that would input the base and power and
display the result: Example:
Base is 4 Power is 2.
Base is 2 Power is 5.
Base is 0 Power is any no.
Base is any no. Power is 0.
#include<iostream>
#include<cmath>
using namespace std;
main()
{
double a,b,c,d,e;
a = pow(4,2);
b = pow(2,5);
c = pow(0,1);
d = pow(15,0);
cout <<a;
cout <<"\n"<<b;
cout <<"\n"<<c;
cout <<"\n"<<d;
}
9 Write a C++ program that would input a number and evaluate if the
number is a “Positive” or “Negative” or “Zero”. If the number is
positive display the message “POSITIVE NUMBER”, if negative
“NEGATIVE NUMBER, and if zero display the message “ZERO”.
1 #include<iostream>
2 using namespace std;
3 main()
4 {
5 int a;
6 cout <<"Please enter an integer numbers: ";
7 cin >>a;
8 if (a>0)
9 cout << "\n" << "The number is positive: ";
10 else if (a<0)
11 cout << "\n" << "The number is negative: ";
12 else
13 cout << "\n" << "The number is zero: ";
14 }

10 What is the output of the following?

You might also like