Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views11 pages

Programming I Assignment 1 2

Uploaded by

70138105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views11 pages

Programming I Assignment 1 2

Uploaded by

70138105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

The University of Lahore

Department of Mechanical Engineering


Programming-I Assignment# 1
Instructor Name: Miss Duaa-e-Nadeem 27th Jan, 2023
Course Title: Programming-I Course Code: CS02119 | 11
Deadline: 31st Jan, 2023 Total Marks: 20
Student Name: Hussnain Ahmad Sap ID. 70135185
NOTE: Write the code here and also attach the screenshot of output of the following programs.
1. Write a program that inputs three numbers and displays maximum number using if-else-
if statement.

#include <iostream>
Answer:
using namespace std;

int main() {

double n1, n2, n3;


double result;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)


{
result = n1;
cout << "Maximum number is " << n1;
}

else if(n2 >= n1 && n2 >= n3)


{

result = n2;
cout << "Maximun number is " << n2;
}

else
{
result = n3;
cout << "Maximum number is " << n3;
}

return 0;
}

2. Write a program that inputs a number and finds whether it is even or odd using if-else
statement.

Answer:
#include <iostream>
using namespace std;

int main( ) {

int num;

cout<<"Enter a numebr";
cin>>num;

if (num%2 == 0)
{
cout<< num << " Is an even number";
}
else
{
cout<< num << " Is an odd number ";
}
return 0;

3. Write a program that inputs a 3 digit number then

a) Displays first digit of that number

Answer:
#include <iostream>

using namespace std;

int main( ) {

int value1, result;

cout<< "Enter a three digit value";

cin>> value1;

result = (value1 / 100);

cout<< "First digit of the entered value is = " << result;

return 0;

}
b) Displays middle digit of that number

Answer:
#include <iostream>
using namespace std;
int main( ) {

int value1, result;


cout<< "Enter a three digit value";
cin>> value1;
result = (value1 % 100) / 10;
cout<< "Second digit of the entered value is = " << result;

return 0;
}

c) Displays last digit of that number

Answer:
#include <iostream>
using namespace std;
int main( ) {

int value1, result;


cout<< "Enter a three digit value";
cin>> value1;
result = (value1 % 10);
cout<< "Third digit of the entered value is = " << result;

return 0;

4. Write a program that inputs marks from a student and then displays the grade of that
student. Use if-else-if statement.

Marks Grade
85-100 A+
80-84 A-
75-79 B+
70-74 B-
65-69 C+
60-64 C-
55-59 D+
50-54 D-
Below 50 F

Answer:
#include <iostream>
using namespace std;

int main() {

int marks;
cout<< " Enter your marks ";
cin>> marks;

if (marks>= 85 && marks<=100)


{
cout<< "A+" <<endl;
}
else if (marks >=80 && marks <=84)
{
cout<< "A-";
}
else if (marks>=75 && marks<=79)
{
cout<< "B+";
}
else if (marks>=70 && marks<=74)
{
cout<<"B-";
}
else if (marks>=65 && marks<=69)
{
cout<< "C+";
}
else if (marks>=60 && marks <=64)
{
cout<<"C-";
}
else if (marks>=55 && marks<=59)
{
cout<<"D+";
}
else if (marks>=50 && marks<=54)
{
cout<<"D-";
}
else
{
cout<<"Fail";
}
return 0;
}

5. Write a program that inputs two numbers from a user. The program also inputs the
operator in the form of string and then perform the desired operation on those two
inputs.

Operator Grade
ADD +
SUB -
MUL *
DIV /
REM %
Answer:
#include <iostream>
using namespace std;

int main()
{
int a,b;
float res;
string op;

cout<<"Enter value of a";


cin>> a;
cout<<"Enter value of b";
cin>> b;
cout<<"Enter Add to add values \n Enter Sub to subtract values \n
Enter Mul to multiply values \n Enter Div to divide values \n Enter Rem to
get remainder \n";
cout<<"Enter which operation you want to perform";
cin>> op;

if (op == "Add")
{
res=a+b;
cout<< "result" <<res;
}
else if (op == "Sub")
{
res=a-b;
cout<< "result" <<res;
}
else if (op == "Mul")
{
res=a*b;
cout<< "result" <<res;
}
else if (op == "Div")
{
res=a/b;
cout<< "result" <<res;
}
else if (op == "Rem")
{
res=a%b;
cout<< "result" << res;
}
else
{
cout<<" Your entered invalid operation";

return 0;
}
6. Write a program that inputs salary and grade. It adds 50% bonus if the grade is greater
than 17. It adds 30% bonus if the grade is 16 and adds 25% bonus if the grade is 15 or
less then displays the total salary.

Answer:
#include <iostream>
using namespace std;

int main( ) {

double salary,bonus,grade;

cout<<" Enter your grade ";


cin>> grade ;
cout<<" Enter your salary ";
cin>> salary ;

if(grade>=17)
{
bonus = salary*50/100;
salary = salary+bonus;
cout<<" Your total salary is "<< salary;
}
else if(grade==16)
{
bonus = salary*30/100;
salary = salary+bonus;
cout<<" Your total salary is "<< salary;
}
else
{
bonus = salary*25/100;
salary = salary+bonus;
cout<<" Your total salary is "<< salary;
}

return 0;

 Good Luck 

You might also like