PFund Assignment 1
PFund Assignment 1
Fall 2024
Problem # 1: Take temperature in Fahrenheit as input and convert it into Centigrade and Kelvin. Display the conversion results at the end.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace
std;
int main()
{ float
temp;
char degree = 248;
cout << "Enter Temperature in
Fahrenheit\n"; cin >> temp;
float centigrade = (temp - 32) * 5 / 9;
cout << "Centigrade : " << centigrade << degree<<"C"
<< endl; cout << "Kelvin : " << centigrade + 273 << "
K" << endl;
return 0;
}
OUTPUT :
Problem # 2 : Prompt the user for the diameter of a circle in centimeters and find out its area in square meters.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace
std;
int main() {
Problem # 3: Input a real number from the user and round it.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace
std;
int main()
{ float
num;
cout << "Enter a
number\n"; cin >> num;
cout << static_cast<int> (num + 0.5);
return 0 ;
}
OUTPUT :
Problem # 4: Input two characters char1 and char2 and interchange their values i.e. swap
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace
std;
int main() {
return 0;
}
OUTPUT :
Problem # 5: If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the remainder
operator i.e. %)
SOURCE CODE:
//Name:
Huzaifa
//Roll no: 186
include<iostream>
using namespace
std;
int main() {
int num;
cout << "Enter a five digit
number: \n"; cin >> num;
return 0;
}
OUTPUT :
Problem # 6 : : C++ Program to take a value from the user as input any character and check whether it is the alphabet, digit or special character.
Using if-else.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace
std;
int main() {
char character;
cout << "Enter any character:\n";
cin >> character;
if ((character >= 'a' fifi character <= 'z') || (character >= 'A' fifi character <= 'Z'))
{
cout << "\nThe character is an alphabet " << endl;
}
else if (character >= '0' fifi character <= '9')
{
cout << "\nThe character is a digit " << endl;
}
else
{
cout << "\nThe character is a special character " << endl;
}
}
Problem # 7: C++ Program to take a value from the user as input the month number and print number of days in that month. Using if-else.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace
std;
int main() {
int month;
cout << "Enter the month number (1-12) " <<
endl; cin >> month;
}
else
{
cout << "This month has 28 days and 29 days in a leap year." << endl;
}
OUTPUT :
Problem # 8 : :Write a program to swap the values of two number if values of both variables are not the same using if-else statement
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace
std;
int main() {
if (num1 != num2)
{
int new_num1 = num1;
num1 = num2;
num2 = new_num1;
cout << "The umbers are swapped because the value is
different\n\n"; cout << "After Swapping the First umber
Becomes --> " << num1 << endl; cout << "After Swapping the
} Second umber Becomes --> " << num2 << endl;
els
e
{
cout << " o eed to Swap because both the umbers are Same" << endl;
}
return 0;
}
OUTPUT :
Problem # 9:
Using the if-else, C++ Program to take a value from the user as input electricity unit charges and calculate total electricity bill according to the
given condition: For the first 50 units Rs. 0.50/unit………….For the next 100 units Rs. 0.75/unit…..For the next 100 units Rs. 1.20/unit
……………For unit above 250 Rs. 1.50/unit………An additional surcharge of 20% is added to the bill.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
int units;
double bill = 0;
return 0;
}
OUTPUT :
Problem # 10 : Input the Salary of an employee and make the following deductions before printing it out.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
double salary, totalSalary;
cout << "Enter the salary of the employee in Rs.\n";
cin >> salary;
return 0;
}
OUTPUT :
Problem # 11: Find out whether the input number is even or odd and if the number is even, display its square and if the number is odd display the cube
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
int num;
return 0;
}
OUTPUT :
Problem # 12 :
Ask the user for his/her age and display the message according to the following criteria: If age is less
than 16, display "You can't drive."
age is less than 18, display "You can't vote."
age is less than 25, display "You can't rent a car."
age is 25 or over, display "You can do anything that's legal.".
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
int age;
cout << "fllease enter your age:\n";
cin >> age;
return 0;
}
OUTPUT :
Problem # 13: Write a program in C++ to display n terms of natural numbers and their sum.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
int x, sum = 0;
cout << "Input a number of terms:\n";
cin >> x;
cout << "The natural numbers upto " << x << " terms are: " << endl;
for (int i = 1; i <= x; i++)
{
cout << i << " ";
sum += i;
}
cout << "\nThe sum of the natural numbers is: " << sum << endl;
return 0;
}
OUTPUT :
Problem # 14 : : Write a program in C++ to calculate the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n).
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
int n, sum;
cout << "Enter the value of n\n";
cin >> n;
sum = (1 * 1) + (2 * 2) + (3 * 3) + (4 * 4) + (5 * 5) + (n * n);
cout << "The sum of the given series is " << sum << endl;
return 0;
}
OUTPUT :
Problem # 15: Write a program in C++ to calculate the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n).
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
s
int n, sum;
cout << "Enter the value of n\n";
cin >> n;
sum = (1) + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4) + (1 + 2 + 3 + 4 + n);
cout << "The sum of the given series is " << sum << endl;
return 0;
}
OUTPUT :
Problem # 16 : Write a C++ program to print all numbers between a and b (a and b inclusive) using for loops.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
int a, b, loop;
cout << "Enter two numbers to print all numbers between them\n";
cin >> a;
cin >> b;
for (loop = a; loop <= b; loop++) {
cout << loop << endl;
}
return 0;
}
OUTPUT :
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
int num;
int factorial = 1;
cout << "Enter a positive number\n";
cin >> num;
if (num < 0) {
cout << " o Factorial\n";
}
else {
for (int i = 1; i <= num;i++) {
factorial *= i;
}
cout << "Factorial of " << num << " is " << factorial << endl;
}
return 0;
}
OUTPUT :
Problem # 18 : Write a C++ program to calculate the area of Circle, Square, and Rectangle using switch statement.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
int choice;
float area, length, radius, width;
cout << "Choose a shape to calculate it's area\n";
cout << "flress 1 for Circle\n";
cout << "flress 2 for square\n";
cout << "flress 3 for Rectangle\n";
cin >> choice;
switch (choice) {
case 1: {
cout << "Enter the Radius of the Circle\n";
cin >> radius;
area = radius * radius;
cout << "Area of the Circle : " << area;break;
}
case 2: {
cout << "Enter the length of one side of the square\n";
cin >> length;
area = length * length;
cout << "Area of the Square : " << area;break;
}
case 3: {
cout << "Enter the Length of the Rectangle\n";
cin >> length;
cout << "Enter the Width of the Rectangle\n";
cin >> width;
area = length * width;
cout << "Area of the Rectangle : " << area;break;
}
default: {
cout << "Invalid Choice";break;
}
}
OUTPUT :
Problem # 19: Write a C++ program to input month number and print number of days in that month using switch case structure.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
enum months { jan = 1, feb, march, april, may, june, july, aug, sep, oct, nov, dec };
int month_num;
cout << "Enter month number and I will tell it's days\n";
cin >> month_num;
switch (month_num) {
case jan: case march: case may: case july: case aug: case oct: case dec: {
cout << month_num << " has 31 days\n";break;
}
case feb: {
cout << month_num << " has 28 days\n";break;
}
case april: case june: case sep: case nov: {
cout << month_num << " has 30 days\n";break;
}
}
return 0;
}
OUTPUT :
Problem # 20: Write a C++ program to input day number between 1 and 7 and print day of week using switch case structure.
SOURCE CODE:
//Name: Huzaifa
//Roll no: 186
include<iostream>
using namespace std;
int main() {
enum week_days { mon = 1, tue, wed, thurs, fri, sat, sun };
int days;
cout << "Enter the number of week days and I will tell it's name\n";
cin >> days;
switch (days) {
case mon: {
cout << days << " is Monday" << endl;break;
}
case tue: {
cout << days << " is Tuesday" << endl;break;
}
case wed: {
cout << days << " is Wednesday" << endl;break;
}
case thurs: {
cout << days << " is Thursday" << endl;break;
}
case fri: {
cout << days << " is Friday" << endl;break;
}
case sat: {
cout << days << " is Saturday" << endl;break;
}
case sun: {
cout << days << " is Sunday" << endl;break;
}
}
return 0;
}
OUTPUT :