C language to print count 0 to 10

 In this  I will tell you how to write a program in which we can count number of zeros, number of positive integers , number of negative integers . So without any further delay let's get started . First I will show you code then we come to explanation.

Code:

#include <stdio.h>
//Counting numbers from 0 to 10
int main()
{
    int n1 = 0;
    int n2 = 0;
    int n3 = 0;
    for (int i = 0; i <=10; i++)
    {if(i==0){//condition for zeros 
        n1++;
    }
    else if(i>=0){//Condition for postive integers 
        n2++;
    }
    else if(i<=0){//Condition for negative integers 
        n3++;
    }
    }
    printf("Number of zeros : %d\n",n1);
    printf("Number of positive integers : %d\n",n2);
    printf("Number of Negative integers : %d\n",n3);

    return 0;
}

Logic :

The logic here is very simple . Here we first three integer n1 , n2 , n3 and initialize it from zero .Then we open a for loop and start it from 0 and till 10 . You can also do it without other loops. And then we use if condition for zero if  true then value of n1++(increase by one ). Then we come to next condition of else if  for positive integers if true then n2++(increase by one). Then we come to negative integers if true then n3++(increase by one ). And in end we use printf and print number of zeros ,then positive integers and then negative integers . 

Output:


You can see as we run loop till 10 that why here we have 1 zero , 10 positive integers because we start from 0 . No negative integers  you must try this on your pc . And you can also run this loop till user given number by changing condition of loop . 

                                              I hope that you find this helpful in your work .

                                                             Thanks for reading .


No comments:

Post a Comment