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

Leave a Reply

Your email address will not be published. Required fields are marked *