Find output of C programs (if else statement) | set 2

Find output of c programs based on if else statement in C programming language: this section contains if else statement based programs with output and explanation.
Submitted by Manju Tomar, on July 30, 2019

Find the output of the following programs,

Program 1)

#include <stdio.h>
int main()
{
    int k = 14, l = 12;
    if (l >= k) 
    {
        l = k;
        k = l;
        printf("%d, %d\n", k, l);
    }
    return 0;
}

Output

//No output

Explanation:

The value of the variable l is 12 and the variable k is 14, so the condition l>=k will be false and there will be no output because the print() statement is written within the condition.

Program 2)

#include <stdio.h>
int main()
{
    if ('Y' < 'y') {
        printf("ASCII value of Y is smaller than that of y\n");
    }
    else {
        printf("ASCII value of y is smaller than that of Y\n");
    }
    return 0;
}

Output

ASCII value of Y is smaller than that of y

Explanation:

The ASCII value of lowercase alphabets are from 97 to 122 and the ASCII value of uppercase alphabets are from 65 to 90. Thus, the ASCII value of Y will be less than the ASCII value of y. Thus, the condition 'Y' > 'y' will be true and the output will be "ASCII value of Y is smaller than that of y".

Program 3)

#include <stdio.h>
int main()
{
    int j = 16, k = 25;
    if (j % 2 == k % 5) {
        printf("Solve\n");
    }
    return 0;
}

Output

Solve

Explanation:

The value of j and k are divisible by 2 and 5 respectively, then the condition j % 2 == k % 5 will evaluate as 0==0, thus the condition will be true and output will be "Solve".

Program 4)

#include <stdio.h>
int main()
{
    int a = 50, b = 100;
    if (a == b / 2) {
        printf("Hello");
    }
    else {
        printf("Hi");
    }
    return 0;
}

Output

Hello

Explanation:

The condition a==b/2 will be true. Thus, the output of the program will be "Hello".

Program 5)

What will be the output of the below program, if input values are 1 and 2?

#include <stdio.h>
int main()
{
    int r, s, c;
    c = scanf("%d%d", &r, &s);
    if (r + s - c) {
        printf("This is a game");
    }
    else {
        printf("you have to play it");
    }
    return 0;
}

Output

1
2
This is a game

Explanation:

scanf() returns the total number of inputs, so the value of c will be 2 and the input value of r is 1 and s is 2. The condition r + s - c will be evaluated as 1 + 2 - 2 = 1. Thus, condition will be true and output will be "This is a game".





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.