DATA INPUT
The input operators are used to read user value (input) from the keyboard.
The c programming language provides the following built-in-input functions.
1. scanf()
2. getcher()
3. getch()
4. gets()
5. fscanf()
scanf() function :
The scanf() function is used to read multiple data values of different data types from the keybord.
Syntax:
scanf("format strings",&variableName);
Example:
#include<stdio.h>
#include<conio.h>
void main()
{ int i;
printf("\nEnter any integer value:");
scanf("%d",&i);
printf("\nYou have entered %d number",i);
getch();
}
getcher() functn
The getcher() function is used to read a characher from the keyboard and return it to the program.
getch() function:
The getch function is similar to getcher function. The getch() function is used to read a character from the keyboard and return it to the program. This function is used to read a single character.
source code :
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("\nEnter any character:");
ch = getch();
printf("\nYou have entered: %c",ch);
}
Program:- Write a program to take input form user to generate calculator which can perform Operations like (+,-,* and /)
Code-
#include<stdio.h>
void main()
{
int ch=0,no1,no2,result=0;
while(ch<=4)
{
printf("Menu:\n1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the two number for Addition:\n");
scanf("%d%d",&no1,&no2);
result=no1+no2;
printf("The sum of %d and %d is %d\n\n",no1,no2,result);
break;
case 2:
printf("Enter two number for substraction:");
scanf("%d%d",&no1,&no2);
result=no1-no2;
printf("\nThe sum of %d and %d is %d\n",no1,no2,result);
break;
case 3:
printf("Enter two number for Multiplication:");
scanf("%d%d",&no1,&no2);
result=no1*no2;
printf("\nThe product of %d and %d is %d\n",no1,no2,result);
break;
case 4:
printf("\nEnter two number for Division:");
scanf("%d%d",&no1,&no2);
if(no2==0)
printf("\nany number divided by 0 is not defined.\n");
else
{
result=no1/no2;
printf("\nThe division of %d and %d is %d\n",no1,no2,result);
}
break;
case 5:
printf("Well done Rajan:");
break;
default :
printf("Invalid Request");
}
}
}
Output:-
Menu:
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
Enter your choice1
Enter the two number for Addition:
15
6
The sum of 15 and 6 is 21
Menu:
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
Enter your choice2
Enter two number for substraction:56-9
The sum of 56 and -9 is 65
Menu:
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
Enter your choice3
Enter two number for Multiplication:5
9
The product of 5 and 9 is 45
Menu:
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
Enter your choice4
Enter two number for Division:10
2
The division of 10 and 2 is 5
Menu:
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
Enter your choice4
Enter two number for Division:3
0
any number divided by 0 is not defined.
Menu:
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
Enter your choice5
Well done Rajan:
------------------------------------------
for while loop click the below link
Nice work bro keep it up
ReplyDelete