Q:

Write a C program to Print How Many Inputs are taken from Keyboard using Scanf in C program

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
Write a C program to Calculate Employee and Emplo... >>
<< Write a C program to Print ASCII value of entered...