Home  • Programming • C

Multiple input technique in C

When we use normal input output technique the working of the program is terminated after completing the operation of the first time imputed data. But we can run the program for multiple input. This is called multiple input technique. Here I will show some easy multiple input tips and tricks for c programming and i think they will very useful for you. 1. We can take multiple input by using while loop. Example:
 
#include<stdio.h>
int main(){
int a,b;
while(scanf(“%d %d”,&a,&b)==2){
if (a>b)
  printf(“%d is greater than %d.
”,a,b);
 else if(a<b)
  printf(“%d is less than %d.
”,a,b);
 else
  printf(“%d and %d are equal.
”,a,b);
 }
return 0;
}
In this example we can take multiple input by the statement ” while(scanf(“%d %d”,&a,&b)==2) ”. Here 2 is the number of input variable. The number given arter ‘=’ will be the same as the number of input variable. After completing operation for the input a and b program will not terminated. It will waiting for next value of a and b. 2.Another way of multiple input is using for loop. Example:
#include<stdio.h>
int main(){
int a,b;
for(;;){
 scanf(“%d %d”,&a,&b);
 if (a>b)
  printf(“%d is greater than %d.
”,a,b);
 else if(a<b)
  printf(“%d is less than %d.
”,a,b);
 else
  printf(“%d and %d are equal.
”,a,b);
}
return 0;
}
In this example for(;;) is using for looping a program. As there is no condition in to the for loop so for loop will not terminated. All the operation will repeats again and again.

Comments 0


About Author
Zinia Islam
Copyright © 2024. Powered by Intellect Software Ltd