Module 1 Chap3
Module 1 Chap3
Chapter 3
Input and Output(I/O)
Input/Output (I/O) Functions
I/O functions in C are categorized as:
General format(Syntax):
scanf("control string",arg1,arg2,…,argn);
scanf("%[^\n]",str);
❑ General Syntax:
printf(“Control string", arg1,arg2,arg3,…,argn);
❑ E.g.:
int a = 10,b=5,sum;
sum = a+b;
printf("The sum of %d and %d is %d",a,b,sum);
❑ Output:
The sum of 10 and 5 is 15
printf("hello world"); ==> hello world
printf("hello\nworld"); ==>hello
world
❑ Default precision is 6.
printf("%f",2.3) ==>2.300000
printf("%.3f",2.3) ==> 2.300
Output of single character
Output of strings
Unformatted I/O
1. Reading a character
This means taking character input from the user.
General Syntax:
variable_name = getchar();
E.g.
char c;
c = getchar();
Program: Reading a character
#include<stdio.h>
int main()
{
char c;
printf("Enter a character:");
// getchar function waits for user to type a Character
c = getchar();
printf("Character %c is entered",c);
return 0;
}
Output:
Enter a character:A
Character A is entered
Unformatted I/O
2. Writing a character
Outputting a character on to standard output
device
Function: putchar(variable_name);
#include<stdio.h>
int main()
{
char c;
printf("Enter a character:");
c = getchar();
printf("Character entered is:");
putchar(c);
return 0;
}
Output:
Enter a character:A
Character entered is:A
Area of triangle
#include<stdio.h>
int main()
{
float b ,h, area;
printf(“Enter the height of the triangle”);
scanf(“%f”,&h);
printf(“Enter the breadth of the triangle”);
scanf(“%f”,&b);
area = (b*h) / 2 ;
printf(“Area of Triangle is: %f",area);
return 0;
}
Square of a Number
#include<stdio.h>
int main()
{
int num,square;
printf("Please Enter any integer Value : ");
scanf("%d", &num);
square = num * num;
printf("square of %d is = %d", num, square);
return 0;
}
Simple Interest
# include <stdio.h>
int main()
{
int principal, rate, time, interest;
printf("Enter the principal: ");
scanf("%d", &principal);
printf("Enter the rate: ");
scanf("%d", &rate);
printf("Enter the time: ");
scanf("%d", &time);
interest = principal * rate * time / 100;
Printf(“The Simple interest is %d”, interest);
return 0;
}
Compound Interest
#include <stdio.h>
#include <math.h>
int main()
{
float principle, rate, time, CI;
printf("Enter principle : ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);
CI = principle* (pow((1 + rate / 100), time));
printf("Compound Interest = %f", CI);
return 0;
}
Cube of number
#include <stdio.h>
int main()
{
int n,volumecube;
printf(“Enter a number”);
scanf(“%d”,&n);
volumecube= n*n*n;
printf(“Volume of Cube of %d is %d",n,volumecube);
return 0;
}
Square root of a number
#include <stdio.h>
#include <math.h>
int main()
{
int num, root;
printf("Enter any number to find square root: ");
scanf("%d", &num);
root = sqrt(num);
printf("Square root of %d = %d", num, root);
return 0;
}
Program: Check the type of
character entered
Include header file: #include<ctype.h>
Use functions: isalpha() and isdigit()
#include<stdio.h>
#include<ctype.h>
int main()
{
char c;
printf("Enter a character:");
c = getchar();
if(isalpha(c)!=0)
printf("Entered character is a letter.\n");
else if(isdigit(c)!=0)
printf("Entered character is a digit.\n");
else
printf("Entered character is not alpha-numeric\n");
return 0;
}
Output:
Enter a character: A
Entered character is a letter.
Enter a character:9
Entered character is a digit.
Enter a character:#
Entered character is not alpha-numeric