C Program to Print Integer Numbers Till N

Write a C program to print integer numbers till user entered limit(num).

Natural numbers are all the numbers from 1, 2, 3, 4, 5, … They are the numbers you usually count and they will continue till infinity.

Whole numbers are all natural numbers including 0.
e.g. 0, 1, 2, 3, 4, 5, …

Integer numbers include all whole numbers and their negative counterpart
e.g. …, -4, -3, -2, -1, 0,1, 2, 3, 4, 5, …

Related Read:
C Program to Print Natural Numbers from 1 to N using While loop
C Program to Print Natural Numbers from 1 to N using for loop

scale

Expected Input/Output

User Input:
Enter an integer number
-5
Output:
Integer numbers from -1 to -5 are …
-1
-2
-3
-4
-5

Video Tutorial: C Program to Print Integer Numbers Till N


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

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

Source Code: C Program to Print Integer Numbers Till N: using For loop

#include<stdio.h>

int main()
{
    int num, i;

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

    if(num < 0)
    {
        printf("Integer numbers from -1 to %d are ...\n", num);
        for(i = -1; i >= num; i--)
            printf("%d\n", i);
    }
    else if(num > 0)
    {
        printf("Integer numbers from 1 to %d are ...\n", num);
        for(i = 1; i <= num; i++)
            printf("%d\n", i);
    }
    else
    {
        printf("You entered zero!\n");
    }

    printf("\n");

    return 0;
}

Output 1:
Enter an integer number
0
You entered zero!

Output 2:
Enter an integer number
5
Integer numbers from 1 to 5 are …
1
2
3
4
5
Output 3:
Enter an integer number
-5
Integer numbers from -1 to -5 are …
-1
-2
-3
-4
-5

Logic To Print Integer Numbers Till N: using For loop

For a integer number input there are 3 possible cases: Either user can input positive number, negative number or a zero. So we cover all these 3 possibilities in our program.

If user enters a number less than 0, then its a negative number. So we initialize i value to -1(which is the biggest negative number), and iterate the for loop until i is greater than or equal to the user entered number. For each iteration we reduce the value of i by 1. Inside for loop, we print the value of i.

If user entered number is greater than 0, then its a positive number. So we initialize i value to 1(smallest positive number), and iterate the for loop until i is less than or equal to user entered number, and for each iteration we increment the value of i by 1. Inside for loop we print the value of i.

If user enter number is nether greater than 0 and nor less than zero, then the user input number must be 0. In that case, we simply output the message that – user entered zero!

Note:
1. When user inputs a negative number, it’ll always be smaller than or equal to -1.
2. When user inputs a positive number, it’ll always be greater than or equal to 1.

Source Code: C Program to Print Integer Numbers Till N: using while loop

#include<stdio.h>

int main()
{
    int num, count;

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

    if(num < 0)
    {
        count = -1;
        printf("Integer numbers from -1 to %d are ...\n", num);
        while(count >= num)
        {
            printf("%d\t", count);
            count--;
        }
    }
    else if(num > 0)
    {
        count = 1;
        printf("Integer numbers from 1 to %d are ...\n", num);
        while(count <= num)
        {
            printf("%d\t", count);
            count++;
        }
    }
    else
    {
        printf("You entered zero!\n");
    }

    printf("\n");

    return 0;
}

Logic To Print Integer Numbers Till N: using While loop

1. For negative value input: We initialize count value to -1, as its the biggest negative number. In while loop condition we check if user input number is less than or equal to value of count(-1), if true, we print the value of count. And for each iteration we decrement the value of count by 1.

2. For positive value input: We initialize count value to 1, as its the smallest positive number. In while loop condition we check if user input number is greater than or equal to value of count(1), if true, we print the value of count. And for each iteration we increment the value of count by 1.

Note:
1. For negative value input, we initialize count value to -1, because we want to print from -1 till user input number.
2. For positive value input, we initialize count value to 1, because we want to print from 1 till user input number.

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

C Program To Count Positive, Negative, Even And Odd Numbers In An Array

Twenty-five numbers are entered from the keyboard into an array. Write a program to find out how many of them are positive, how many are negative, how many are even and how many odd.

Important Note
Most of the time 0 is considered as even number, as it has odd numbers on either side of it i.e., 1 and -1. And 0 is perfectly divisible by 2.

Also note that 0 is neither positive and nor negative.

number scale

Related Read:
C Program To Count Number of Even, Odd and Zeros In An Array
C Program To Count Number of Positive, Negative and Zeros In An Array

Example: Expected Output

Enter 10 integer numbers
-4
-3
-2
-1
0
1
2
3
4
5

Positive: 5
Negative: 4
Even: 4
Odd: 5
Zero: 1

Visual Representation

count positive negative even and odd elements in array

Video Tutorial: C Program To Count Positive, Negative, Even And Odd Numbers In An Array


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

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

Source Code: C Program To Count Positive, Negative, Even, Odd And Zero Numbers In An Array

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, pos = 0, neg = 0, even = 0, odd = 0, zero = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
        if(a[i] == 0)
        {
            zero++;
        }
        else if(a[i] > 0)
            pos++;
        else
            neg++;

        if(a[i] == 0)
        {

        }
        else if(a[i] % 2 == 0)
            even++;
        else
            odd++;
    }

    printf("\nPositive: %d\n", pos);
    printf("Negative: %d\n", neg);
    printf("Even: %d\n", even);
    printf("Odd: %d\n", odd);
    printf("Zero: %d\n", zero);

    return 0;
}

Output:
Enter 10 integer numbers
-4
-3
-2
-1
0
1
2
3
4
5

Positive: 5
Negative: 4
Even: 4
Odd: 5
Zero: 1

Here we are counting positive numbers, negative numbers, even numbers, odd numbers and zeros.

Source Code: C Program To Count Positive, Negative, Even And Odd Numbers In An Array

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, pos = 0, neg = 0, even = 0, odd = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
        if(a[i] == 0)
        {

        }
        else if(a[i] > 0)
            pos++;
        else
            neg++;

        if(a[i] % 2 == 0)
            even++;
        else
            odd++;
    }

    printf("\nPositive: %d\n", pos);
    printf("Negative: %d\n", neg);
    printf("Even: %d\n", even);
    printf("Odd: %d\n", odd);

    return 0;
}

Output:
Enter 10 integer numbers
-4
-3
-2
-1
0
1
2
3
4
5

Positive: 5
Negative: 4
Even: 5
Odd: 5

In above source code, since 0 is perfectly divisible by 2, it is considered as even number.

Logic To Count Positive, Negative, Even And Odd Numbers In An Array

We ask the user to enter N integer numbers and store it inside array variable a[N]. Next we loop through the array using for loop. For each iteration we check: if the selected number is greater than 0. If true, then its positive number, so we increment the value of pos by 1. If the number is less than 0, then its a negative number, so we increment the value of neg by 1.

Next we check if the selected number is perfectly divisible by 2. If true, then its even number, so we increment the value of variable even by 1. If the selected number is not perfectly divisible by 2, then its odd number.

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

C Program To Count Number of Even, Odd and Zeros In An Array

Lets write a C program to count number of even, odd and zeros in an array.

What are Even and Odd Numbers?

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

Related Read:
Even or Odd Number: C Program

Example: Expected Output

Enter 10 integer numbers
10
15
0
-1
5
20
21
55
69
40

Even Numbers: 3
Odd Numbers: 6
Zeros: 1

Visual Representation

count even odd zero in an array

Video Tutorial: C Program To Count Number of Even, Odd and Zeros In An Array


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

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

Source Code: C Program To Count Number of Even, Odd and Zeros In An Array

Method 1

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, odd = 0, even = 0, zero = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < N; i++)
    {
        if(a[i] == 0)
            zero++;
        else if(a[i] % 2 == 0)
            even++;
        else if(a[i] % 2 != 0)
            odd++;
    }

    printf("\nEven Numbers: %d\nOdd Numbers: %d\nZeros: %d\n", even, odd, zero);

    return 0;
}

Output:
Enter 10 integer numbers
9
8
7
6
5
4
3
2
1
0

Even Numbers: 4
Odd Numbers: 5
Zeros: 1

Logic To Find Number of Even, Odd and Zeros In An Array

First we ask the user to enter N integer numbers. After that we iterate through the array elements one by one (using a for loop) and check if the fetched number is 0, if true, we’ll increment the value of zero by one. If the fetched element is not zero, then we check if its perfectly divisible by 2, if so, then its even number orelse its odd number – and we increment the values of variable even and odd accordingly.

Note: Make sure to first check if the fetched number is 0. Only after checking this, go further and check for even or odd conditions.

Method 2

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, even = 0, odd = 0, zero = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(a[i] == 0)
            zero++;
        else if(a[i] % 2 == 0)
            even++;
        else if(a[i] % 2 != 0)
            odd++;
    }

    printf("\nEven No: %d\nOdd No: %d\nZeros: %d\n", even, odd, zero);

    return 0;
}

Output:
Enter 10 integer numbers
0
1
2
3
4
5
6
7
8
9

Even Numbers: 4
Odd Numbers: 5
Zeros: 1

In above source code, once the user inputs a number, we check if the input number is equal to zero or is perfectly divisible by 2 or it is not perfectly divisible by 2. Based on that we increment the values of variable zero, even and odd accordingly.

This is the best solution for this problem statement, as we only write for loop once and we calculate the result as and when user inputs array elements. So less overhead and more efficient.

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

Positive or Negative or Zero Using Macros: C Program

C Program to check whether the user input integer number is positive, negative or zero using Macros and ternary / Conditional operator.

Related Read:
Positive or Negative or Zero Using Ternary Operator: C Program

Logic

If user input number is greater than 0, then the number is positive. If user input number is less than 0, then the number is negative. If neither of the above 2 conditions are true, then the number is zero.

Video Tutorial: Positive or Negative or Zero Using Macros: C Program


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

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

Source Code: Positive or Negative or Zero Using Macros: C Program

#include<stdio.h>

#define SIGN(num) ( (num > 0) ? \
                   printf("POSITIVE") : \
                   (num < 0) ? printf("NEGATIVE") : \
                   printf("ZERO"))

int main()
{
    int num;

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

    SIGN(num);

    return 0;
}

Output 1:
Enter a number
5
POSITIVE

Output 2:
Enter a number
-2
NEGATIVE

Output 3:
Enter a number
0
ZERO

Here the macro template SIGN(num) will be replaced by its corresponding macro expansion ( (num > 0) printf(“POSITIVE”) : (num < 0) ? printf(“NEGATIVE”) : printf(“ZERO”)) by preprocessor. And if user input number is greater than 0, it’ll print POSITIVE else it’ll print NEGATIVE. If neither of those 2 conditions are true, then it’ll print ZERO.

In our program we’re using Macro Continuation (\) Preprocessor Operator: C Program in macro expansion to break from the line and continue the logic in new/next line.

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

C Program To Count Positive, Negative and Zero using Ternary Operator and without using Array

Lets write a C program to enter number till the user wants. At the end it should display the count of positive number, negative number and zeros entered, using Ternary Operator or Conditional Operator and without using arrays.

Related Read:
while loop in C programming
Positive or Negative or Zero Using Ternary Operator: C Program

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

scale

Expected Output for the Input

User Input:
Enter the limit
6
Enter 6 numbers
-1
0
2
-3
5
6

Output:
Positive Numbers: 3
Negative Numbers: 2
Number of zero: 1

Logic To Count Positive, Negative and Zero using Ternary Operator and without using Array

Complete logic to this program is present at C Program To Count Positive, Negative and Zero without using Array

Video Tutorial: C Program To Count Positive, Negative and Zero using Ternary Operator and without using Array


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

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

Source Code: C Program to Count Positive, Negative and Zero using Ternary Operator and without using Array

#include < stdio.h >

int main()
{
    int limit, num, positive = 0, negative = 0, zero = 0;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);

    while(limit)
    {
        scanf("%d", &num);
        (num > 0) ? positive++ : ((num < 0) ? negative++ : zero ++);
        limit--;
    }

    printf("\nPositive Numbers: %d\n", positive);
    printf("Negative Numbers: %d\n", negative);
    printf("Number of zero: %d\n", zero);

    return 0;
}

Output:
Enter the limit
5
Enter 5 numbers
2
-1
5
6
0

Positive Numbers: 3
Negative Numbers: 1
Number of zero: 1

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