[go: up one dir, main page]

0% found this document useful (0 votes)
30 views8 pages

Lab 4

The document is a lab report submitted by Ubaid Ali to their instructor Asst. Prof Jahanzeb. The objectives of the lab were to understand selection structures like if, if-else, if-else-if, and nested if statements. The lab involved writing C++ code to solve problems involving positive/negative numbers, finding the largest of three numbers, checking divisibility, and determining if a number is even or odd. The conclusion discusses how the lab helped improve problem-solving, data manipulation, control flow, data type handling, efficiency, and debugging skills.
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)
30 views8 pages

Lab 4

The document is a lab report submitted by Ubaid Ali to their instructor Asst. Prof Jahanzeb. The objectives of the lab were to understand selection structures like if, if-else, if-else-if, and nested if statements. The lab involved writing C++ code to solve problems involving positive/negative numbers, finding the largest of three numbers, checking divisibility, and determining if a number is even or odd. The conclusion discusses how the lab helped improve problem-solving, data manipulation, control flow, data type handling, efficiency, and debugging skills.
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/ 8

DEPARTMENT OF MECHANICAL ENGINEERING

COLLEGE OF E&ME, NUST, RAWALPINDI

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

Related Topic/Chapter in theory class:


 Selection Structures (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)
{

cout << "zero:";


}

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() {

int num1, num2;


cout << " please enter the first number:";
cin >> num1;
cout << "please enter the second number: ";
cin >> num2;
num1% num2;
if (num2 == 0)
{
cout << "num1 is not divisible by num2: "; //task 3
}
else
{
cout << "num 1 is divisible by 2";
}

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) {

cout << "0 ";

}
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 Manipulation: You'll be able to manipulate data in various ways


using arithmetic, comparison, and logical operators, which are essential for
any data-processing application.

 Control Flow: Logical and relational operators, along with conditional


statements, help control the flow of your program. This is crucial for making
decisions in your code and executing different paths based on conditions.

 Data Type Handling: Casting and conversion between data types are
important for working with different types of data in a single program or
function.

 Efficiency: Understanding these concepts helps you write more efficient


code. For example, using the right assignment operators can reduce
unnecessary computation, and increment and decrement operators can
make your code more concise.

 Debugging: Being familiar with these concepts can help you identify and
fix logical errors or unexpected behavior in your code more effectively.

You might also like