Home » C solved programs » C basic programs

C - Print How Many Inputs are taken from Keyboard using Scanf in C program

In this C program, we will learn how to get total number of inputs taken from scanf() in c program.

Since, we know that scanf() returns total number of input using the return value of scanf(), we can count the total number of inputs.

In this program, we are reading multiple numbers and storing them into an integer array, at every input scanf() is returning the number of inputs and we are adding them into count.

Print Number of Inputs taken from Keyboard using Scanf in C Program

/*C - Print How Many Inputs are Taken from 
Keyboard using Scanf in C Progra.*/
 
#include <stdio.h>
 
int main(){
    int count=0;
    int num; 
    int arr[100],i=0;
     
    while(num!=-1){
        printf("Enter an integer number (-1 to exit): ");
        count+=scanf("%d",&num);
        arr[i++]=num;
    }
     
    printf("\nTotal Inputs (including 0) are: %d\n",count);
    printf("Entered numbers are:\n");
    for(i=0;i<count;i++){
        printf("%d ",arr[i]);
    }
     
    printf("\n");
    return 0;       
}

Output

    Enter an integer number (-1 to exit): 10
    Enter an integer number (-1 to exit): 20
    Enter an integer number (-1 to exit): 30
    Enter an integer number (-1 to exit): 40
    Enter an integer number (-1 to exit): -1

    Total Inputs (including 0) are: 5
    Entered numbers are:
    10 20 30 40 -1 






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.