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

Assignment: Name: Nawaf Rasheed Reg#no: 50816 Program: BS (SE)

The document contains an assignment submission by a student named Nawaf Rasheed with registration number 50816 enrolled in the BS (SE) program. It includes answers to 8 questions in C++ programming. The answers cover topics like input/output, if-else conditional statements, switch case, operators, and calculating grade from marks. Code snippets are provided as part of the answers to demonstrate concepts in C++.

Uploaded by

Nawaf Rasheed
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)
44 views

Assignment: Name: Nawaf Rasheed Reg#no: 50816 Program: BS (SE)

The document contains an assignment submission by a student named Nawaf Rasheed with registration number 50816 enrolled in the BS (SE) program. It includes answers to 8 questions in C++ programming. The answers cover topics like input/output, if-else conditional statements, switch case, operators, and calculating grade from marks. Code snippets are provided as part of the answers to demonstrate concepts in C++.

Uploaded by

Nawaf Rasheed
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

Assignment

Name: Nawaf Rasheed


Reg#no: 50816
Program: BS (SE)

Question: 02:

Answer: # Store input numbers


num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# Add two numbers


sum = float(num1) + float(num2)

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Question:no:03:

Answer:

include <stdio.h>

int main(void){

int num;

printf("Enter your mark ");

scanf("%d",&num);

printf(" You entered %d", num); // printing outputs

if(num >= 80){

printf(" You got A grade"); // printing outputs

else if ( num >=60){ // Note the space between else & if

printf(" You got B grade");

else if ( num >=40){


printf(" You got C grade");

else if ( num < 40){

printf(" You Failed in this exam");

return 0;

include<stdio.h>

#include<conio.h>

void main ()

float m1,m2,m3;

float per;

float total;
clrscr();

printf(" enter the marks of m1");

scanf("%f", &m1);

printf(" enter the marks of m2");

scanf("%f", &m2);

printf("enter the marks of m3");

scanf("%f", &m3);

total= m1+m2+m3;

printf("the value of marks is %.2f",total);

per = (total/300)*100;

printf(" the percentage of each subjects is %.2f:", per);

getch();

include <stdio.h>

int main(void){
int num;

printf("Enter your mark ");

scanf("%d",&num);

printf(" You entered %d", num); // printing outputs

if(num >= 80){

printf(" You got A grade"); // printing outputs

else if ( num >=60){ // Note the space between else & if

printf(" You got B grade");

else if ( num >=40){

printf(" You got C grade");

else if ( num < 40){

printf(" You Failed in this exam");


}

return 0;

Question:04:
Answer:
Example
Input
Input cost price: 1000

Input selling price: 1500

Output
Profit: 500

Formula to calculate profit and loss


Profit = S.P - C.P (Where S.P is Selling Price and C.P is Cost Price)
Loss = C.P - S.P
/**
* C program to calculate profit or loss
*/

#include <stdio.h>

int main()
{
int cp,sp, amt;

/* Input cost price and selling price of a product */


printf("Enter cost price: ");
scanf("%d", &cp);
printf("Enter selling price: ");
scanf("%d", &sp);

if(sp > cp)


{
/* Calculate Profit */
amt = sp - cp;
printf("Profit = %d", amt);
}
else if(cp > sp)
{
/* Calculate Loss */
amt = cp - sp;
printf("Loss = %d", amt);
}
else
{
/* Neither profit nor loss */
printf("No Profit No Loss.");
}

return 0;
}
Output
Enter cost price: 1000
Enter selling price: 1500
Profit = 500

Question:05:
Answer:
/*If ages of Ahmed, Khurram are input through the keyboard. Write a programme
to determine the youngest of the two.*/
#include<stdio.h>
int main()
{
int Ahmed,Khurram ;
printf(" Enter the ages of Ahmed :");
scanf("%d",& Ahmed);

printf("\n Enter the ages of Khurram :");


scanf("%d",&Khurram);

if( < Ahmed & < Khurram)


{
printf("\n The youngest of the two is Ahmed");
}
else if(Ahmed < & < Khurram)
{
printf("\n The youngest of the Two is Khurram");
}
else if(Ahmed < & < Khurram)
{
printf("\nThe youngest of the two is Ahmed");
}
else
{
printf("\nSame Age");
}
return 0;
}

Output : 
Enter the ages of Ahmed,Khurram :20
30

The youngest of the three is Khurram

Question.no:06:
Answer:

#include <iostream>

using namespace std;

int main()
{
   int score;

   cout << "Enter your grade scored in programming class (0-100)" << endl;
   cin >> score;

   switch (score) {
       case 100:
           cout << "You got a perfect score!" << endl;
           break;
       case 0:
           cout << "You failed. Really badly." << endl;
           break;
       default:
           if(score>=0 && score<60) {
               cout << "Your Grade : F" << endl;
           } else if(score>=60 && score<70) {
               cout << "Your Grade : D" << endl;
           } else if(score>=70 && score<80) {
               cout << "Your Grade : C" << endl;
           } else if(score>=80 && score<90) {
               cout << "Your Grade : B" << endl;
           } else if(score>=90 && score<100) {
               cout << "Your Grade : A" << endl;
           }
   }
}

Question:07:
Answer:
include <iostream>

using namespace std;

int main()
{
   int drink;

   cout << "Pick a beverage.\nType 1 for Coke\nType 2 for Pepsi\nType 3 for Sprite\nType 4 for Water\nType 5 for Mountain
Dew\n";
   cin >> drink;

   switch(drink) {
       case 1:
           cout << "You chose Coke\n";
           break;
       case 2:
           cout << "You chose Pepsi\n";
           break;
       case 3:
           cout << "You chose Sprite\n";
           break;
       case 4:
           cout << "You chose Water\n";
           break;
       case 5:
           cout << "You chose Mountain Dew\n";
           break;
       default:
           cout << "Error. choice was not valid, here is your money back.";
   }
}

//If I replace switch statement with if/else:


/*

if(drink=1) {
   cout << "You chose Coke\n";
} else if(drink=2) {
   cout << "You chose Pepsi\n";
} else if(drink=3) {
   cout << "You chose Sprite\n";
} else if(drink=4) {
   cout << "You chose Water\n";
} else if(drink=5) {
   cout << "You cose Mountain Dew\n";
} else {
   cout << "Error. choice was not valid, here is your money back.";
}
  
*/

Question.no:08:
Answer:

# include <iostream>
using namespace std;

int main()
{
char op;
float num1, num2;

cout << "Enter operator either + or - or * or /: ";


cin >> op;

cout << "Enter two operands: ";


cin >> num1 >> num2;

switch(op)
{
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;

case '*':
cout << num1*num2;
break;

case '/':
cout << num1/num2;
break;

default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}

return 0;
}

Output

Enter operator either + or - or * or divide : -


Enter two operands:
3.4
8.4
3.4 - 8.4 = -5.0

You might also like