0% found this document useful (0 votes)
68 views12 pages

Exp No7

The document discusses different types of decision making and looping statements in C++ programming including if/else statements, if/else ladders, nested if/else, switch case statements, while loops, do/while loops, and for loops. Examples of code implementing each of these statements are provided. One example shows how to check if a user-input number is even or odd using an if statement.

Uploaded by

Pratiksha Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views12 pages

Exp No7

The document discusses different types of decision making and looping statements in C++ programming including if/else statements, if/else ladders, nested if/else, switch case statements, while loops, do/while loops, and for loops. Examples of code implementing each of these statements are provided. One example shows how to check if a user-input number is even or odd using an if statement.

Uploaded by

Pratiksha Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

 Exp no.

Decision making statements

1. If statement

#include <stdio.h>

void main()

int a=15, b=20;

if(b>a)

printf("B is greater");

}
2. If else ladder

#include <stdio.h>
int main()
{
int num1, num2;
printf("Please Enter Two different values\n");
scanf("%d %d", &num1, &num2);
if(num1 > num2)
{
printf("%d is Largest\n", num1);
}
else if (num2 > num1)
{
printf("%d is Largest\n", num2);
}
else
{
printf("Both are Equal\n");
}
return 0;
}
3.Nested if-else

#include <stdio.h>

void main()

int x=20,y=30;

if(x==20)

if(y==30)

printf("Value of x is 20,Value of y is 30");

}
4. If-else

int main()
{

int num1, num2;

printf("Please Enter Two different values\n");


scanf("%d %d", &num1, &num2);

if(num1 > num2)


{
printf("%d is Largest\n", num1);
}

else if (num2 > num1)


{
printf("%d is Largest\n", num2);
}

else
{
printf("Both are Equal\n");
}

return 0;
}
XIII.Exercise

3. C++ program to returns day based on the numeric value.

#include<iostream>
using namespace std;

class Day
{
private:
int day;

public:
void set_data()
{
cout<<"Enter no of day you want to display: ";
cin>>day;
}

void display_day()
{
switch (day)
{
case 1:
cout<<"MONDAY";
break;

case 2:
cout<<"TUESDAY";
break;

case 3:
cout<<"WEDNESDAY";
break;

case 4:
cout<<"THURSDAY";
break;
case 5:
cout<<"FRIDAY";
break;

case 6:
cout<<"SATURDAY";
break;

case 7:
cout<<"SUNDAY";
break;

default:
cout<<"INVALID INPUT";
break;
}
}
};

main()
{
Day d1;

d1.set_data();
d1.display_day();

return 0;
}
5. Switch case

#include <stdio.h>

void main()
{
int x ;
printf(“Please enter a number between 1 to 5:”);
scanf(“%d”,&x);
switch (x) {
case 1:
printf("Choice is 1");
break;

case 2:
printf("Choice is 2");
break;

case 3:
printf("Choice is 3");
break;
case 4:
printf("Choice is 4");
break;

case 5:
printf("Choice is 5");
break;

default:
printf("Invalid choice");
break;
}
return 0;
}
Looping Statements

1. While loop

#include <iostream>

using namespace std;

int main() {

int i = 1;

while (i <= 5) {

cout << i << " ";

++i;

return 0;

}
2. Do while loop

#include <iostream>

using namespace std;

int main() {

int i = 1;

do {

cout << i << " ";

++i;

while (i <= 5);

return 0;

}
3.For loop

#include <iostream>

using namespace std;

int main()

for (int i = 1; i <= 5; ++i)

cout << i << " ";

return 0;

}
XIII.Exercise

1. Program of even odd numbers

#include <iostream>

using namespace std;

int main()

int n;

cout << "Enter an integer: ";

cin >> n;

if ( n % 2 == 0)

cout << n << " is even.";

else

cout << n << " is odd.";

return 0;

You might also like