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

PFund Assignment 1

The document outlines an assignment for Programming Fundamentals (CS-116) at Sir Syed University, detailing various programming problems to be solved in C++. Each problem includes a description, source code, and expected output, covering topics such as temperature conversion, area calculation, character checks, and arithmetic operations.

Uploaded by

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

PFund Assignment 1

The document outlines an assignment for Programming Fundamentals (CS-116) at Sir Syed University, detailing various programming problems to be solved in C++. Each problem includes a description, source code, and expected output, covering topics such as temperature conversion, area calculation, character checks, and arithmetic operations.

Uploaded by

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

SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY

Fall 2024

Programming Fundamentals (CS-116) Assignment 1


Semester: I Batch: 2024F
Due Date: Nov/19/2024 Max Marks: 20
Name: Huzaifa Roll Number: 2024F-BCS-186
Section: D

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<iostrea
m> 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<iostrea
m> using
namespace std;

int main() {

float diameter, area;


cout << "Enter the diameter of a circle in centimeters , I will tell it's
area in square meters\n"; cin >> diameter;
area = 3.142 * (diameter / 2) * (diameter / 2);
cout << "Area : " << area / 10000 << " square meter" << endl;
return 0;}
OUTPUT :

Problem # 3: Input a real number from the user and round it.

SOURCE CODE:

//Name: Huzaifa
//Roll no: 186
include<iostrea
m> 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<iostrea
m> using
namespace std;

int main() {

char char1, char2;


cout << "Enter First
Character\n"; cin >>
char1;
cout << "Enter Second
Character\n"; cin >> char2;
std::swap(char1, char2);
cout << "Character 1 : " <<
char1 << endl; cout <<
"Character 2 : " << char2 <<
endl;

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<iostrea
m> using
namespace std;

int main() {

int num;
cout << "Enter a five digit
number: \n"; cin >> num;
int num1 = num % 10;
int num2 = (num / 10)
% 10; int num3 = (num
/ 100) % 10; int num4 =
(num / 1000) % 10; int
num5 = (num / 10000)
% 10;
int sum = num1 + num2 + num3 +

num4 + num5; cout << "\nSum of

digits : " << sum << endl;

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<iostrea
m> 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<iostrea
m> using
namespace std;

int main() {

int month;
cout << "Enter the month number (1-
12) " << endl; cin >> month;

if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 ||


month == 12)
{
cout << "This month has 31 days." << endl;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
cout << "This month has 30 days" << endl;
}
else if (month == 2)
{ cout << "This month has 28 days and 29 days in a leap year." << endl;

}
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<iostrea
m> 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)
{
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 <<
els "After Swapping the Second umber Becomes --> " <<
e num2 << endl;
{

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

cout << "Enter the electricity units consumed by the


user\n"; cin >> units;

if (units <= 50)


{
bill = units * 0.50;
}
else if (units <= 150)
{
bill = 50 * 0.50 + (units - 50) * 0.75;
}
else if (units <= 250)
{

} bill = 50 * 0.50 + 100 * 0.75 + (units - 150) * 1.20;


els
e
{
bill = 50 * 0.50 + 100 * 0.75 + 100 * 1.20 + (units - 250) * 1.50;
}
double surcharge = bill *
0.20; bill += surcharge;
cout << "The surcharge is : " << surcharge << endl;
cout << "The Electricity Bill is : " << bill - surcharge << endl;
cout << "The Total Electricity Bill with Surcharge of the user is : " << bill << endl;

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;

if (salary <= 10000)


{
totalSalary = salary;
}
else if (salary > 10000 fifi salary < 25000)
{

} totalSalary = salary - 1000;


els
e
{
totalSalary = salary - (salary * 0.05);
}
cout << "\nThe Total salary of the employee after deductions is Rs. \t" << totalSalary << endl;

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;

cout << "fllease Enter a number here


\n"; cin >> num;
if (num % 2 == 0)
{
int square = num * num;
cout << "\nThe number is Even " << endl;
cout << "The square of " << num << " is : " << square << endl;
}
els
e
{ int cube = num * num * num;
cout << "\n The number is Odd" << endl;
cout << "The cube of " << num << " is : " << cube << endl;

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;

if (age < 16)


{
cout << "You can't drive." << endl;
}
if (age < 18)
{
cout << "You can't vote." << endl;
}
if (age < 25)
{
cout << "You can't rent a car." << endl;
}
if (age >= 25)
{
cout << "You can do anything that's legal." << endl;
}

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 :

Problem # 17: Write a program in C++ to find the factorial of a number.

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 :

You might also like