Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to count integers in C?

+2 votes
asked Jul 2, 2020 by Saquelain (140 points)
I want to count integers separated by space (like 5 6 9 8 2) in C . but not like this (56982)

Please help me out. Thank you.

2 Answers

–1 vote
answered Jul 4, 2020 by Peter Minarik (86,040 points)

Do you mean you would like to print integers out on the screen?

Print Integers in a Loop

You can loop through a set of numbers and print each one of them one by one.

In the following example we start from 0 and end before reaching 10, so our last number is 9. After every number we also add a space. "%d" means, we want to print a decimal number, so the second argument of printf is the decimal number we would like to print. For details, see printf().

#include <stdio.h>

int main()
{
    for (int i = 0; i < 10; i++)
    {
        printf("%d ", i);
    }

    return 0;
}

Print Integers in an Array

There is another typical scenario. You have an array, and you would like to print the content of this array. In this case, you'd write something, like this:

#include <stdio.h>

#define count   5

int main()
{
    int numbers[count] = { 5, 6, 9, 8, 2 };
    
    for (int i = 0; i < count; i++)
    {
        printf("%d ", numbers[i]);
    }

    return 0;
}

So we created a macro called count so we know how many elements are in our array. Then in the loop, we iterate through these elements one by one and print them.

I hope this helps.

Good luck! :)

0 votes
answered Jul 14, 2020 by AVJ INFINITY (180 points)
use array to count the digits of a number. first make the reverse of the number and then wse FOR loop to print induvidual number in the units place to get the digits of the number

the main code to use here is :

int n,i,m,rem,a=0;

scanf("%d",&n);                                   //  n is the number

scanf("%d",&m);               //      m is number the digits of the number

while(n!=0)

{

for(i=0;i<m;i++)

{

rem=n%10;

printf("%d",rem);

}

for(i=0;i<m;i++)

{

a=rem%10;

printf("%d",a);

}

}

return 0;

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...