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