Assignment 2
Assignment 2
Course Title:
PROGRAMMING FUNDAMENTALS
Submitted to: Junaid Abdullah Mansoor
Submitted by: Misbah Aiman
ID: F20232661169
Section: V24
Course code: CC1021
Program name: BS (CS)
Code:
#include <iostream>
int main()
int n, a, b, c ;
cin >> n ;
a=n%10;
n=n/10;
b=n%10;
n=n/10;
c=n;
if (a>b)
if(a>c)
else
else
if(b>c)
cout<< "Largest digit is " << b << endl;
else
return 0;
Question 2. Write a C++ program that takes user input for an integer value and the program should
perform the following tasks:
Code:
#include <iostream>
int main()
int n ;
cin >> n ;
if(n>0)
else if (n<0)
else if (n==0)
{
cout << "You entered zero." << endl;
else
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>
int main()
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
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>
int main()
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
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>
int main()
int grade ;
cout << "Your grade is F. You should work hard." << endl;
else
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>
int main()
char unit;
cout << "The temperature in Fahrenheit is " << converted_temperature << "°F" << endl;
}
cout << "The temperature in Celsius is " << converted_temperature << "°C" << endl;
else
return 0;