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

Assignment 1 pfund

Uploaded by

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

Assignment 1 pfund

Uploaded by

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

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
Following are a few problems. Understand each problem and write programs in the C++
language for each problem. Note the following instructions clearly:

• Make sure you submit your own work. Any form of collaboration will not be tolerated
and case will be forwarded directly to the Disciplinary Committee or in the best case,
negative policy! This includes copying code snippets from the internet! You may discuss
a problem with your mates, let’s say English/Urdu but not in C++!!! Make sure you use
meaningful identifier names.
• Your code should be easily readable i.e. use indentation and spacing, as discussed in
class.
• Each program should start with comments including o Your Full Name o Your Roll #
and Section
o Problem #
• Submission instructions will be communicated to you later.

Problem # 1: Take temperature in Fahrenheit as input and convert it into Centigrade and Kelvin.
Display the conversion results at the end.
• Centigrade i.e. C = 5*(F-32)/9
• Kelvin i.e. K = C + 273.

//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
int Fahrenheit, Centigrade, Kelvin;
cout << "Enter the temperature in Fahrenheit" << endl;
cin >> Fahrenheit;
Centigrade = 5 * (Fahrenheit - 32) / 9;
Kelvin = Centigrade + 273;
cout << " Your Temp in Centigrade is ==> " << Centigrade <<"" << endl;
cout << " Your Temp in Kelvin is ==> " << Kelvin << "K" << endl;
return 0;
}

Problem # 2: Prompt the user for the diameter of a circle in centimeters and find out its area in
square meters.
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
double Diameter ,Radius, Area;
cout << "Enter your diameter in cm" << endl;
cin >> Diameter;
Diameter = Diameter / 100;
Radius = Diameter / 2;
Area = 3.142 * Radius * Radius;
cout << "Area ==>" << Area <<" square meter " << endl;
return 0;
}

Problem # 3: Input a real number from the user and round it.
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
float num , Roundednum ;
cout << "Enter you real number to round off" << endl;
cin >> num;
Roundednum =(round(num));
cout << "This number rounds to "<<fixed << setprecision(2) << Roundednum <<
endl;

return 0;
}
Problem # 4: Input two characters char1 and char2 and interchange their values i.e. swap.

//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
char char1 , char2 ,swap ;
cout << "Enter the character 1" << endl;
cin >> char1;
cout << "Enter the character 2" << endl;
cin >> char2 ;
swap = char1;
char1 = char2;
char2 = swap;
cout << "character 1 ==> '" << char1 << "'" << endl;
cout << "character 2 ==> '" << char2 << "'" << endl;
return 0;
}

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. %)

//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
int number, sum = 0;

cout << "Enter a five-digit number: " << endl;


cin >> number;
if (number < 10000 || number > 99999)
{
cout << "Please enter a valid five-digit number." << endl;
}
sum += number % 10;
number /= 10;

sum += number % 10;


number /= 10;

sum += number % 10;


number /= 10;

sum += number % 10;


number /= 10;

sum += number % 10;

cout << "The sum of the digits is ==> " << sum << endl;

return 0;
}

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.
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
char v;
cout << "Enter any character/integer " << endl;
cin >> v;
if ((v >='a' && v <= 'z') || (v >= 'A' && v <= 'Z'))
{
cout << v << "You entered a alphabet" << endl;
}
else if (v >=0)
{
cout << "You entered a digit" << endl;
}
else
{
cout << "You entered a special number" << endl;
}
return 0;
}
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.
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
int month ;
cout << "Enter any month number (1-12) " << endl;
cin >> month;
if (month == 1)
{
cout << "January has 31 days." << endl;
}
else if (month == 2)
{
cout << "February has 28 or 29 days (leap year)." << endl;
}
else if (month == 3)
{
cout << "March has 31 days." << endl;
}
else if (month == 4)
{
cout << "April has 30 days." << endl;
}
else if (month == 5)
{
cout << "May has 31 days." << endl;
}
else if (month == 6)
{
cout << "June has 30 days." << endl;
}
else if (month == 7)
{
cout << "July has 31 days." << endl;
}
else if (month == 8)
{
cout << "August has 31 days." << endl;
}
else if (month == 9)
{
cout << "September has 30 days." << endl;
}
else if (month == 10)
{
cout << "October has 31 days." << endl;
}
else if (month == 11)
{
cout << "November has 30 days." << endl;
}
else if (month == 12)
{
cout << "December has 31 days." << endl;
}
else
{
cout << " Please enter a number between 1 and 12." << endl;
}

return 0;
}

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
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
int a, b , swap;
cout << "Enter the Number 1" << endl;
cin >> a;
cout << "Enter the Number 2" << endl;
cin >> b;
cout << "Before swapping a = " << a << " b =" << b << endl;
if (a != b)
{
swap = a;
a = b;
b = swap;
cout << "After swapping a = " << a << " b = " << b << endl;
}
else
{
cout << " a = b no swap needed" << endl;
}
return 0;
}

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.
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
float unit, bill , surcharge;
cout << "Enter your electricity unit charges" << endl;
cin >> unit;
if (unit <= 50)
{
bill = unit * 0.05;
}
else if ((unit > 50) && (unit <= 150))
{
bill = 50 * 0.05 + (unit-50) * 0.75;
}
else if ((unit > 150) && (unit <= 250))
{
bill = 50 * 0.05 + 100 * 0.75 + (unit-150) * 1.20;
}
else
{
bill = 50 * 0.05 + 100 * 0.75 + 150 * 1.20 + (unit-250) * 1.50;
}
surcharge = bill*0.20;
bill += surcharge;
cout << "Your electricity bill including surcharge is Rs: " <<fixed <<
setprecision(2) << bill << endl;

return 0;
}

Problem # 10: Input the Salary of an employee and make the following deductions before
printing it out:
If salary is less than or equal to 10,000 then no deduction are done.
If salary is more than 10,000 and less than 25,000 then deduct Rs. 1,000.
If salary is equal to or more than 25,000 then deduct 5 % of the salary.
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
float salary, finalsalary;
cout << "Enter your salary" << endl;
cin >> salary;
if (salary <= 10,000)
{
finalsalary = salary;
cout<<"No deductions are done"<<endl;
}
else if (salary > 10,000 && salary < 25,000)
{
finalsalary = salary - 1000;
cout << "Your salary is ==>" << salary << endl;
}
else
{
finalsalary = (salary - (0.05 * finalsalary));
cout << "Your salary is ==>" << salary << endl;
}
cout << "Your final salary is ==>" << finalsalary << endl;
}
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.

//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
int num , square , cube;
cout << "Enter your number" << endl;
cin >> num;
if (num % 2 == 0)
{
cout<<"The number is "<< num <<" which is even" << endl;
square = num * num;
cout << "The square of the number is ==> " << square << endl;
}
else
{
cout<< "The number is " << num << " which is odd" << endl;
cube = num * num * num;
cout<< "The cube of the number is ==> " << cube << endl;
}
return 0;
}
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.".
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
int age;
cout << "Enter your age" << endl;
cin >> age;
if (age < 16)
{
cout<<" You can't drive " << endl;
}
else if (age < 18)
{
cout << " You can't vote " << endl;
}
else if (age < 25)
{
cout << " You can't rent a car " << endl;
}
else
{
cout << " You can do anything that's legal " << endl;
}

return 0;
}
Problem # 13: Write a program in C++ to display n terms of natural numbers and their sum.
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int n, sum =0 ;
cout << "Enter the number of terms (n)" << endl;
cin >> n;
cout << "The natural numbers are upto ==> "<< n << endl;
for (int i = 1; i <= n; i++)
{
cout << "'" << i << "'";
sum += i;
}
cout << "\nThe sum of natural numbers is ==> " << sum << endl;
return 0;
}
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).
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
int n, sum = 0, product;
cout << "Enter the number of series (n)" << endl;
cin >> n;
cout << "The series are upto ==> " << n << endl;
for (int i = 1; i <= n; i++)
{
product = i * i;
cout << "("<<i<<"*"<<i<<") ==>" << product << endl;
sum += product;
}
cout << "\nThe sum of series is ==> " << sum << endl;
return 0;
}

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).

//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
int n, Sum = 0;

cout << "Input the value for nth term: ";


cin >> n;
for (int i = 1; i <= n; i++) {
int currentSum = 0;
for (int j = 1; j <= i; j++)
{
cout << j;
if (j < i)
{
cout << "+";
}
currentSum += j;
}
cout << " = " << currentSum << endl;
Sum += currentSum;
}

cout << "The sum of the above series is: " << Sum << endl;

return 0;
}

Problem # 16: Write a C++ program to print all numbers between a and b (a and b inclusive)
using for loops.

//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main() {
int a, b;
cout << "Enter the starting number (a): ";
cin >> a;
cout << "Enter the ending number (b): ";
cin >> b;
cout << "Numbers between " << a << " and " << b << " are:" << endl;
if (a <= b)
{
for (int i = a; i <= b; ++i)
{
cout << i ;
}
}
else
{
for (int i = a; i >= b; --i) {
cout << i ;
}
}

cout << endl;


return 0;
}

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

//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main() {
int num, factorial = 1;

cout << "Enter a positive integer: ";


cin >> num;
if (num < 0)
{
cout << "Factorial of a negative number doesn't exist." << endl;
}
else {
for (int i = 1; i <= num; ++i)
{
factorial *= i;
}
cout << "Factorial of " << num << " is: " << factorial << endl;
}

return 0;
}
Problem # 18: Write a C++ program to calculate the area of Circle, Square, and Rectangle using
switch statement.
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main() {
int choice;
double area;

cout << "Enter the shape to calculate the area:" << endl;
cout << "1. Circle" << endl;
cout << "2. Square" << endl;
cout << "3. Rectangle" << endl;
cout << "Enter your choice (1-3): ";
cin >> choice;

switch (choice) {
case 1: {
double radius;
cout << "Enter the radius of the circle: ";
cin >> radius;
if (radius < 0)
{
cout << "Radius cannot be negative!" << endl;
}
else
{
area = 3.142 * radius * radius;
cout << "The area of the circle is: " << area << endl;
}
break;
}
case 2:
{
double side;
cout << "Enter the side length of the square: ";
cin >> side;
if (side < 0)
{
cout << "Side length cannot be negative!" << endl;
}
else
{
area = side * side;
cout << "The area of the square is: " << area << endl;
}
break;
}
case 3:
{
double length, width;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
if (length < 0 || width < 0)
{
cout << "Length and width cannot be negative!" << endl;
}
else
{
area = length * width;
cout << "The area of the rectangle is: " << area << endl;
}
break;
}
cout << "Invalid choice! Please enter a number between 1 and 3." << endl;
break;
}

return 0;
}

Problem # 19: Write a C++ program to input month number and print number of days in that
month using switch case structure.
//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
int month;
cout << "Enter any month number (1-12) " << endl;
cin >> month;
switch (month)
{
case 1:
{
cout << "January has 31 days." << endl;
break;
}
case 2:
{
cout << "February has 28 or 29 days (leap year)." << endl;
break;
}
case 3:
{
cout << "March has 31 days." << endl;
break;
}
case 4:
{
cout << "April has 30 days." << endl;
break;
}
case 5:
{
cout << "May has 31 days." << endl;
break;
}
case 6:
{
cout << "June has 30 days." << endl;
break;
}
case 7:
{
cout << "July has 31 days." << endl;
break;
}
case 8:
{
cout << "August has 31 days." << endl;
break;
}
case 9:
{
cout << "September has 30 days." << endl;
break;
}
case 10:
{
cout << "October has 31 days." << endl;
break;
}
case 11:
{
cout << "November has 30 days." << endl;
break;
}
case 12:
{
cout << "December has 31 days." << endl;
break;
}
default:
cout << " Please enter a number between 1 and 12." << endl;
}
return 0;
}

Problem # 20: Write a C++ program to input day number between 1 and 7 and print day of week
using switch case structure.

//Arwa Mamji
//2024F-BCS-060
#include <iostream>
using namespace std;

int main()
{
int days;
cout << "Enter any weekday number (1-7) " << endl;
cin >> days;
switch (days)
{
case 1:
{
cout << "Monday is day 1 of the week" << endl;
break;
}
case 2:
{
cout << "Tuesday is day 2 of the week" << endl;
break;
}
case 3:
{
cout << "Wednesday is day 3 of the week" << endl;
break;
}
case 4:
{
cout << "Thursday is day 4 of the week" << endl;
break;
}
case 5:
{
cout << "Friday is day 5 of the week" << endl;
break;
}
case 6:
{
cout << "Saturday is day 6 of the week" << endl;
break;
}
case 7:
{
cout << "Sunday is day 7 of the week" << endl;
break;
}
default:
cout << " Please enter a number between 1 and 7." << endl;
}
return 0;
}

You might also like