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

Computer 2

research article
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Computer 2

research article
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

University of central

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;
}

 Output:  Abdullah bin Talib


 BS-Computer Science
 Roll no: OIF24UBSCS038
 Program no 2:
Write a program that inputs age in years and displays age in
days and months and draw flowchart.

 Solution:

#include <iostream>
using namespace std;

int main() {
int ageInYears, ageInMonths, ageInDays;
cout << "Enter your age in years: ";
cin >> ageInYears;

ageInMonths = ageInYears * 12;


ageInDays = ageInYears * 365;
cout << "Your age in months: " << ageInMonths << endl;
cout << "Your age in days: " << ageInDays << endl;

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 << "Enter total number of students in the class: ";


cin >> totalStudents;
cout << "Enter fee per student: ";
cin >> feePerStudent;

totalFee = totalStudents * feePerStudent;

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;

cout << "Enter three numbers: ";


cin >> num1 >> num2 >> num3;
cout << "Enter radius: ";
cin >> radius;
cout << "Enter your choice (3, 5, or others): ";
cin >> choice;

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:

 Output with choice 5:


 Output with Other choice:

You might also like