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

Assignment 2

Uploaded by

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

Assignment 2

Uploaded by

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

Assignment-1 (week7)

Course Title:
PROGRAMMING FUNDAMENTALS
Submitted to: Junaid Abdullah Mansoor
Submitted by: Misbah Aiman
ID: F20232661169
Section: V24
Course code: CC1021
Program name: BS (CS)

University of Management and


Technology, Lahore
Question 1 . Write a program that inputs a three-digit integer, separates the integer into its
digits and prints the largest digit. An example of interaction with the program might be:

Enter a three-digit integer: 243

The largest digit is: 4

Code:
#include <iostream>

using namespace std;

int main()

int n, a, b, c ;

cout << "Enter a three-digit number: " ;

cin >> n ;

a=n%10;

n=n/10;

b=n%10;

n=n/10;

c=n;

if (a>b)

if(a>c)

cout<< "Largest digit is " << a << endl;

else

cout<< "Largest digit is " << c << endl;

else

if(b>c)
cout<< "Largest digit is " << b << endl;

else

cout<< "Largest digit is " << c << endl;

return 0;

Question 2. Write a C++ program that takes user input for an integer value and the program should
perform the following tasks:

1. Prompt the user to enter an integer.


2. Check if the entered integer is positive, negative, or zero.
3. Display an appropriate message based on the classification (positive, negative, or zero).

Code:
#include <iostream>

using namespace std;

int main()

int n ;

cout << "Enter the number: " ;

cin >> n ;

if(n>0)

cout << "You entered a positive number." << endl;

else if (n<0)

cout << "You entered a negative number." << endl;

else if (n==0)

{
cout << "You entered zero." << endl;

else

cout << "Invalid input." << endl;

return 0;

Question 3. Write a C++ program that takes two integer inputs from the user and display
whether the first number is divisible by the second number. Ensure that the program handles
the case where the second number is zero.

Code:
#include <iostream>

using namespace std;

int main()

int num1, num2 ;

cout << "Enter the first number: " ;

cin >> num1 ;

cout << "Enter the second number: " ;

cin >> num2 ;

if (num2!=0)

if(num1%num2==0)

cout << num1 << " is divisible by " << num2 << endl;

else

cout << num1 << " is not divisible by " << num2 << endl;

}
else if(num2==0)

cout << "you shouldn't enter second number as 0." << endl;

else

cout << "Invalid input" <<endl;

return 0;

Question 3. Write a C++ program that takes two integer inputs from the user and display
whether the first number is divisible by the second number. Ensure that the program handles
the case where the second number is zero.

Code:
#include <iostream>

using namespace std;

int main()

int num1, num2 ;

cout << "Enter the first number: " ;

cin >> num1 ;

cout << "Enter the second number: " ;

cin >> num2 ;

if (num2!=0)

if(num1%num2==0)

cout << num1 << " is divisible by " << num2 << endl;

else
cout << num1 << " is not divisible by " << num2 << endl;

else if(num2==0)

cout << "you shouldn't enter second number as 0." << endl;

else

cout << "Invalid input" <<endl;

return 0;

Question 4. Develop a C++ program that prompts the user to enter a numerical grade.
Classify the grade into one of the following categories: A, B, C, D, or F. Make sure to consider
valid grade ranges and provide appropriate feedback.

Code:
#include <iostream>

using namespace std;

int main()

int grade ;

cout << "Enter the numerical grade: " ;

cin >> grade ;

if(grade<100 && grade>=90)

cout << "Congratulations! your grade is A." << endl;

else if(grade<90 && grade>=80)

cout << "Good! your grade is B." << endl;

else if(grade<80 && grade>=70)


cout << "Better! your grade is C." << endl;

else if(grade<70 && grade>=55)

cout << "Your grade is D. Make it more better." << endl;

else if(grade<55 && grade>=0)

cout << "Your grade is F. You should work hard." << endl;

else

cout << "Invalid input." << endl;

return 0;

Question 5. Create a C++ program that converts temperatures between Celsius and
Fahrenheit. Ask the user to input a temperature value along with the unit (Celsius or
Fahrenheit). Use "if-else" statements to perform the conversion and display the result. Support
both Celsius to Fahrenheit and Fahrenheit to Celsius conversions.

Code:
#include <iostream>

using namespace std;

int main()

double temperature, converted_temperature;

char unit;

cout << "Enter the temperatue value: " ;

cin >> temperature ;

cout << "Enter the unit as celsius C or fahrenheit F: " ;

cin >> unit ;

if ( unit == 'C' || unit == 'c' )

converted_temperature = ( temperature * 1.8 ) + 32 ;

cout << "The temperature in Fahrenheit is " << converted_temperature << "°F" << endl;
}

else if ( unit == 'F' || unit == 'f' )

converted_temperature = ( temperature - 32 ) * 0.55 ;

cout << "The temperature in Celsius is " << converted_temperature << "°C" << endl;

else

cout << "Invalid input entered" << endl;

return 0;

You might also like