Computer 2
Computer 2
PUNJAB
Introduction to Computer
C++ Programming
Professor Abdul-Quddoos
Program no 1:
Write a program that prints a text of 4 lines consisting of
characters integer values and floating-point value using cout statement.
Solution:
#include<iostream>
using namespace std;
int main()
{
char c='A';
int i=100;
float f=3.14;
cout<<"Welcome to programming!"<<endl;
cout<<"Character Value: "<<c<<endl;
cout<<"Integer Value: "<<i<<endl;
cout<<"Floating Point Value: "<<f<<endl;
return 0;
}
Solution:
#include <iostream>
using namespace std;
int main() {
int ageInYears, ageInMonths, ageInDays;
cout << "Enter your age in years: ";
cin >> ageInYears;
return 0;
}
Output:
Flowchart:
Start
Input Age
in Years
Age in Months =
Years * 12
Age in Days =
Years * 365
Program no 3:
Write a program that inputs total number of students
in a class and fee per students. It displays that fee collected from
the class and also writes pseudo-code and algorithm.
Solution:
#include <iostream>
using namespace std;
int main() {
int totalStudents;
float feePerStudent, totalFee;
cout << "Total fee collected from the class: " << totalFee << endl;
return 0;
}
Output:
Algorithm:
1. Start
2. Input totalStudents
3. Input feePerStudent
4. Calculate totalFee = totalStudents * feePerStudent
5. Display totalFee
6. End
Pseudo-code:
START
INPUT totalStudents, feePerStudent
COMPUTE totalFee = totalStudents * feePerStudent
OUTPUT totalFee
END
Program no 4:
Write a program that inputs three numbers and radius and
choice. If user 3 as choice find out the sum, subtract and multiplication of
three numbers. If user enter 5 as choice find out area of circle. If user enters
another choice find out the average of three numbers.
Solution:
#include <iostream>
using namespace std;
int main() {
int choice, num1, num2, num3;
float radius, area;
if (choice == 3) {
cout << "Sum: " << num1 + num2 + num3 << endl;
cout << "Subtraction: " << num1 - num2 - num3 << endl;
cout << "Multiplication: " << num1 * num2 * num3 << endl;
} else if (choice == 5) {
area = 3.14159 * radius * radius;
cout << "Area of the circle: " << area << endl;
} else {
float average = (num1 + num2 + num3) / 3.0;
cout << "Average of three numbers: " << average << endl;
}
return 0;
}
Output with choice 3: