Number is Positive or Negative or Zero: C Program

C Program to check whether the user entered integer number is positive, negative or zero using else if construct.

Note:
Any number greater than 0 is positive.
Any number less than 0 is negative.

Check Whether Number is Positive or Negative or Zero: C Program


[youtube https://www.youtube.com/watch?v=lxNZZJ7NVlE]

YouTube Link: https://www.youtube.com/watch?v=lxNZZJ7NVlE [Watch the Video In Full Screen.]


Check Whether Number is Positive or Negative or Zero: C Program

#include < stdio.h >

int main()
{
    int a;

    printf("Enter an integer number\n");
    scanf("%d", &a);

    if(a > 0)
    {
        printf("%d is positive\n", a);
    }
    else if(a < 0)
    {
        printf("%d is negative\n", a);
    }
    else
    {
        printf("%d is zero\n", a);
    }

    return 0;
}

Output 1:
Enter an integer number
15
15 is positive

Output 2:
Enter an integer number
-2
-2 is negative

Output 3:
Enter an integer number
0
0 is zero

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

else if statement in C

In this program we’ll show you the usage of else-if clause in C programming language. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.

Related Read:
Nested if else Statement In C

Simple Logic: Student Grade

First in if‘s condition we check if the user entered percentage is above 100%. If true – in that case we let the user know that he entered wrong marks and he / she needs to re-enter the marks. Next in first else if we check if the percentage is greater than or equal to 60. Here we need not check if the percentage is less than 100, because in if clause itself we’ve checked if percentage is greater than 100, and we are checking the else if condition only because the condition in if is false. So percentage will be less than 100. Same logic goes to consecutive Grades.

Student Grade Calculation using else if clause: C Program


[youtube https://www.youtube.com/watch?v=WmJEbiKX4BA]

YouTube Link: https://www.youtube.com/watch?v=WmJEbiKX4BA [Watch the Video In Full Screen.]


Note: When the condition is true, it executes its corresponding block of code and then skips checking remaining else if or else conditions.

Note: Its optional to have else in if or else if clause.

Note: We can have any number of else if statements after if.

Student Grade Calculation using else-if clause

 
#include < stdio.h >

int main()
{
    int s1, s2, s3, s4, s5, per;

    printf("Enter marks of 5 subjects\n");
    scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);

    per = (s1 + s2 + s3 + s4 + s5) / 5.0;

    if(per > 100)
        printf("You've entered wrong marks, kindly re-enter\n");
    else if(per >= 60)
        printf("Grade A\n");
    else if(per >= 50)
        printf("Grade B\n");
    else if(per >= 40)
        printf("Grade C\n");
    else
        printf("Failed!\n");

    return 0;
}

Output 1:
Enter marks of 5 subject
59
60
62
61
63
Grade A

Output 2:
Enter marks of 5 subject
59
55
56
53
58
Grade B

Output 3:
Enter marks of 5 subject
49
45
46
49
48
Grade C

Output 4:
Enter marks of 5 subject
39
45
25
36
35
Failed!

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Nested if else Statement In C

In this program we’ll show you nesting of if else statements. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.

Simple Logic

When the condition in if statement is false then only else block gets executed. In our program, we check, if percentage is greater than or equal to 60. If it is false, then only control shifts to else block. So inside else(nested if else) we need not once again check if the percentage is less than 60.

Note: We are not using else if and logical operator in this C program purposefully. We want to show nesting of if else in this program.

 
#include < stdio.h >

int main()
{
    float s1, s2, s3, s4, s5, per;

    printf("Enter marks of 5 subject\n");
    scanf("%f %f %f %f %f", &s1, &s2, &s3, &s4, &s5);

    per = (s1 + s2 + s3 + s4 + s5) / 5.0;

    if(per >= 60)
    {
        printf("Grade A\n");
    }
    else
    {
        if(per >= 50)
        {
            printf("Grade B\n");
        }
        else
        {
            if(per >= 40)
            {
                printf("Grade C\n");
            }
            else
            {
                printf("Failed!\n");
            }
        }
    }

    return 0;
}

Output 1:
Enter marks of 5 subject
59
60
62
61
63
Grade A

Output 2:
Enter marks of 5 subject
59
55
56
53
58
Grade B

Output 3:
Enter marks of 5 subject
49
45
46
49
48
Grade C

Output 4:
Enter marks of 5 subject
39
45
25
36
35
Failed!

Student Grade Calculation using Nested if else: C Program


[youtube https://www.youtube.com/watch?v=I-EHRu-Nq8o]

YouTube Link: https://www.youtube.com/watch?v=I-EHRu-Nq8o [Watch the Video In Full Screen.]


Note: In next video we’ll show you how to perform the same operation as shown in above C program, using else-if and logical operators.

Also check the program C Program To Check Leap Year for a perfect nested if else example.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Loops and Conditions In Jade: Node.js

After going through the basics of Jade, lets learn about loops and conditional statements in Jade.

loops-and-conditions-in-jade-nodejs

Note: Since most text editors designed for web remove the indentation(and some retain only new line characters), thus compressing the file and optimizing it for the web use. But this doesn’t serve any good for writing Jade. Because indentation is at the heart of Jade syntax!

So in this tutorial, we are using a Chrome extension called Jade Editor to illustrate loops and conditional statements. You can simply test your code in this Google Chrome extension and then copy over the code to your actual file. This would even help reduce the complicated errors in lengthy files.

each loop in Jade
arrays

1
2
3
4
5
- names = ["Apple", "Microsoft", "Oracle"];
 
ul
each name in names
  li #{name}

output

  • Apple
  • Microsoft
  • Oracle

sign is optional for each loop. Here it loops through the array names and prints out each element of the array.

each loop and loop index in Jade
arrays

1
2
3
4
5
- names = ["Apple", "Microsoft", "Oracle"];
 
ul
each name, i in names
  li #{i+1}: #{name}

output

  • 1: Apple
  • 2: Microsoft
  • 3: Oracle

This prints out name along with the index number. Since index in JavaScript array starts with 0, we add 1 to it, to make it more sensible for non-programmers.

for loop and loop index in Jade
arrays

1
2
3
4
5
- names = ["Apple", "Microsoft", "Oracle"];
 
ul
for name, i in names
  li #{i+1}: #{name}

output

  • 1: Apple
  • 2: Microsoft
  • 3: Oracle

Same holds good even for for loop.

each loop in Jade
objects: Key, Value pair

1
2
3
4
5
- obj = {cmp1: "Apple", cmp2: "IBM"};
 
ul
each val, key in obj
  li #{key}: #{val}

output

  • cmp1: Apple
  • cmp2: IBM

Since objects in JavaScript has {key: value} pair, we make use of it to extract and print from the object.

for loop in Jade
objects: Key, Value pair

1
2
3
4
5
- obj = {cmp1: "Apple", cmp2: "IBM"};
 
ul
for val, key in obj
  li #{key}: #{val}

output

  • cmp1: Apple
  • cmp2: IBM

Same holds good to for loop too.

Conditional Statements

if, else if, else In Jade
Conditional Statements

1
2
3
4
5
6
7
8
- var fc = 2;
 
if( fc === 0 )
 p You have no followers
else if( fc === 1 )
 p You have 1 follower
else
 p You have #{fc} followers

output
You have 2 followers

Here we use the condition to check for execution of block of code. Also note that sign before if, else if and else keywords are optional.

Loops and Conditions In Jade: Node.js


[youtube https://www.youtube.com/watch?v=wL60uzhwwTA]

YouTube Link: https://www.youtube.com/watch?v=wL60uzhwwTA [Watch the Video In Full Screen.]



In coming video tutorials, we shall learn about Mixins.

Biggest of 3 Numbers Using Ternary Operator: C++

Video tutorial to find the biggest of the three integer numbers in C++, using if-else control statements and Ternary Operator.

Before watching this video, please make sure to watch and understand this short video: Find Biggest of 3 Numbers: C++

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream .h>
#include<conio .h>
 
void main()
{
int a, b, c;
clrscr();
 
cout< <"Enter 3 no's\n";
cin>>a>>b>>c;
 
int big =  ( a>b && a>c )?a:(b>c?b:c);
/*
if( a > b && a > c )
  big = a;
else if( b > c )
  big = b;
else
  big = c;
*/cout< <"\nAmong "<<a<<" , "<<b<<" , "<<c<<" Biggest is "<<big;
getch();
}

In this program we take 3 integer values from the user and using ternary operator decide the biggest of 3 numbers.

int big =  ( a>b && a>c )?a:(b>c?b:c);

if ( a > b && a > c ) is true, then value of a will be stored in variable big; else ( b > c ? b : c ) will be evaluated. Here, if value of b is greater than c, value of b will be stored in variable big else value of c will be stored in the variable big.

Video Tutorial: Biggest of 3 Integer Numbers


[youtube https://www.youtube.com/watch?v=gfmqT4PwfY0]

YouTube Link: https://www.youtube.com/watch?v=gfmqT4PwfY0 [Watch the Video In Full Screen.]



Output
Enter 3 no’s
10
20
30
Among 10, 20, 30 Biggest is 30