Header Ads

write a C++ program using FOR loop and an If-Else statement that performs the following tasks:


Sometimes in programming, there may be a need to run a block of code repeatedly. In general, the programming statements are executed in order. Different control structures offered by programming languages allow for more complex execution paths. A loop statement allows us to execute a statement or collection of statements multiple times. Conditional branching statement (if-else), on the other hand, controls the flow of execution of statements based on some condition. If the given condition is true, the code inside the IF block is executed, otherwise, the Else block code is executed.

Now keeping in mind, the above-discussed concepts, write a C++ program using FOR loop and an If-Else statement  that performs the following tasks:

1.      Print your VU Id.

2.      Add the first and last numeric digit of the VU Id, and store the result in the variable “sum”.

3.      Display the result of the sum on the screen.

4.      If the sum is greater than 7 then print the “Welcome to VU”. The number of iterations of FOR loop should be equal to the sum.

5.      If the sum is less than 7 then print the “Welcome to CS201P”. The number of iterations of FOR loop should be equal to the sum.

For example, suppose the student id is BC123456781. Then by adding first and last numeric digit of VU Id, we get the value of 2. In this case, the program should print “Welcome to CS201P” for 2 times using FOR loop and vice versa.


Program: For sum = 7

#include <iostream>

using namespace std;


int main()

{

char *vu_id;

int First_digit, Last_digit, sum;

vu_id = "BC200012005";

First_Digit = 2;

Last_Digit = 5;

Sum = First_Digit + Last_Digit;

cout<<"My VU ID is:\t"<<vu_id<<endl;

cout<<"The sum of first and last digit of VU ID is:\t"<<Sum<<endl;

if (Sum>7)                   // sum > 7

{

for (int i = 0; i<Sum; i++)

{

cout<<"Iteration "<<i+1<<": Welcome to VU"<<endl;

}

}

else if (Sum<7) // sum < 7

{

for (int i = 0; i<Sum; i++)

{

cout<<"Iteration "<<i+1<<": Welcome to CS201P"<<endl;

}

}

else // sum = 7

{

cout<<"The Sum is Equal to 7";

}

return 0;

}

Output


Program: sum > 7

#include <iostream>

using namespace std;


int main()

{

char *vu_id;

int first_digit, last_digit, sum;

vu_id = "BC300012005";

First_Digit = 3;

Last_Digit = 5;

Sum = First_Digit + Last_Digit;

cout<<"My VU ID is:\t"<<vu_id<<endl;

cout<<"The sum of first and last digit of VU ID is:\t"<<Sum<<endl;

if (Sum>7)                   // sum > 7

{

for (int i = 0; i<Sum; i++)

{

cout<<"Iteration "<<i+1<<": Welcome to VU"<<endl;

}

}

else if (Sum<7)  // sum < 7

{

for (int i = 0; i<Sum; i++)

{

cout<<"Iteration "<<i+1<<": Welcome to CS201P"<<endl;

}

}

else // sum = 7

{

cout<<"The Sum is Equal to 7";

}

return 0;

}


Output: 


Program: sum < 7

#include <iostream>

using namespace std;


int main()

{

char *vu_id;

int first_digit, last_digit, sum;

vu_id = "BC200012004";

First_Digit = 2;

Last_Digit = 4;

Sum = First_Digit + Last_Digit;

cout<<"My VU ID is:\t"<<vu_id<<endl;

cout<<"The sum of first and last digit of VU ID is:\t"<<Sum<<endl;

if (Sum>7)                   // sum > 7

{

for (int i = 0; i<Sum; i++)

{

cout<<"Iteration "<<i+1<<": Welcome to VU"<<endl;

}

}

else if (Sum<7)  // sum < 7

{

for (int i = 0; i<Sum; i++)

{

cout<<"Iteration "<<i+1<<": Welcome to CS201P"<<endl;

}

}

else // sum = 7

{

cout<<"The Sum is Equal to 7";

}

return 0;

}


Output




No comments

Powered by Blogger.