Lab 4
Lab 4
Subject Name
Fundamental Of Programming
Lab Number
#4
SUBMITTED TO:
Instructor Name:Asst Prof Jahanzeb
Lab instructor:
SUBMITTED BY:
Student Name: Ubaid Ali
Reg #482299
DE- 45 Dept = Mechanical Engineering / B
Submission Date:
10/20/2023
Objectives:
.
Understanding of using
If
If-else
If-else-if
Nested if
Hardware/Software required:
Hardware: PC/Laptop
Software Tool: Visual Studio, C++
Tasks:
1. Question statement
Write a program that takes a number from user. If the user enters negative
number your program should print “Negative Number”, in case of positive
number print “Positive Number”, in case if zero is entered print “Zero”.
Solution:
#include<iostream>
using namespace std;
int main() {
int a;
cin >> a;
if (a > 0)
{ cout << "number is positive:"; //task 1
if (a < 0)
{
cout << "number is negative:";
}
if (a == 0)
{
Output:
Screenshots of output console
2. Question statement
Find Largest Number among 3 values taken from user using Nested if...else statement.
Solution:
#include<iostream>
using namespace std;
int main() {
int a, b, c;
cout << "please enter the first number";
cin >> a;
cout << "please enter the second number"; //task 2
cin >> b;
cout << "please enter the third number";
cin >> c;
if (a > b)
{
if (a > c) {
cout << "first number is greater";
}
}
if (b > a) {
if (b > c) {
cout << "second number is greater";
}
}
if (c > a) {
if (c > b) {
cout << "third number is greater";
}
}
return 0;
Output:
Screenshots of output console
3. Question statement
.Write a program in C++ that takes two numbers num1, num2. If num1 is divisible by
num2, your program should print “num1 is divisible by num 2”
Solution:
C++ code for the solution
#include<iostream>
using namespace std;
int main() {
return 0;
Output:
Screenshots of output console
4. Question statement
Write a C++ program, take an integer value from user and check whether the value
is multiple of 2. Print 1 if yes and print 0 if no. Use appropriate datatype for output.
Solution:
#include<iostream>
using namespace std;
int main() {
int a;
cout << "enter the number of your choice:";
cin >> a;
if (a % 2) {
}
else {
cout << "1";
}
return 0;
Output:
Screenshots of output console
Conclusion:
Problem-Solving Skills: Understanding these concepts is
fundamental to solving problems and writing efficient, effective code. They
form the foundation of many programming tasks.
Data Type Handling: Casting and conversion between data types are
important for working with different types of data in a single program or
function.
Debugging: Being familiar with these concepts can help you identify and
fix logical errors or unexpected behavior in your code more effectively.