4 Statements
4 Statements
• getchar() is an input function that reads a single character from the standard input
device, normally a keyboard.
• For the input functions it does not require <Enter> to be pressed after the entry of
the character.
• putchar() is an output function that writes a single character on the standard
output device, the display screen.
• There are two other functions, gets() and puts(), that are used to read and
write strings from and to the keyboard and the display screen respectively
#include<stdio.h>
#include<conio.h>
main()
{
char c1;
c1='$';
printf("using printf %c\t",c1);
putchar(c1);
return(0);
}
FORMATTED INPUT AND OUTPUT FUNCTIONS
• When input and output is required in a specified format the
standard library functions scanf() and printf() are used.
The scanf() function allows the user to input data in a specified format.
It can accept data of different data types .
The printf() function allows the user to output data of different data
types on the console in a specified format.
• The format string in printf(), enclosed in quotation marks, has
three types of objects:
Ordinary characters: these are copied to output,
Conversion specifier field:
denoted by % containing the codes listed in Table 1. and by optional modifiers such as
width, precision, flag, and size
The conversion specifier field is used to format printed values, often to arrange things
nicely in columns.
}
The switch statement
The general format of a switch
statement is
switch(expr)
{
case constant1: stmtList1;
break;
case constant2: stmtList2;
break;
case constant3: stmtList3;
break;
………………………….
………………………….
default: stmtListn;
}
#include<stdio.h>
#include<conio.h>
main()
{
int n,cur ;
scanf("%d %d",&n,&cur);
switch(cur)
{
case 10:printf("%d\t",n*10);break;
case 20:printf("%d\t",n*20);break;
case 50:printf("%d\t",n*50);break;
case 100:printf("%d\t",n*100);break;
}
return(0);
}
FOR loop
The general form of the for statement
Statement cane be
• any single statement like input/output/assignment/if-
else/any loop/return etc
• compound statement.
return;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=20;i<=30;i=i+2) {printf("%d\t",i); }
return;
}
For-loop Flow chart
• First initialization is done and then condition is
tested.
• If the condition is true, loop statement is
executed, updation is done and again condition is
tested and so on.
• Testing the condition, executing the statement,
updating are repeated as long as the condition is
true.
• When condition becomes false, control goes the
next statement after the loop.
• Initialization is done only once
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
for(i=1 ; i<5 ;i++) printf("%d ",i);
return(0);
}
Output : 1 2 3 4
Multiple initializations separated by
comma
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1,j=5 ; i<5 ; i++) printf("%d %d\n",i,j);
return(0);
}
Output : 1 5
25
35
45
Multiple updations
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1,j=5 ; i<5; i++,j-- ) printf("%d %d\n",i,j);
return(0);
}
Output : 1 5
24
33
42
Multiple conditions separated by commas
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=50,j=10,k=100;i>=25,j<50;i=i-5,j=j+5)
{printf("%d %d %d\n",i,j,k); }
return;
}
You come out of loop when last condition is satisfied
Multiple conditions can be combined using logical operator to
form a compound condition
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1,j=5 ; i<5&&j<0 ; i++,j--) printf("%d %d\n",i,j);
return(0);
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=50,j=10,k=100;i>=25||j<50;i=i-5,j=j+5) {printf("%d %d
%d\n",i,j,k); }
return;
}
// display count of number of Even, odd numbers in set of given N numbers
#include<stdio.h>
#include<conio.h>
main()
{
int n,num,even_count=0,odd_count=0,i;
printf("enter value of n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter value\n");
scanf("%d",&num);
if(num%2==0) even_count++;
else odd_count++;
}
printf("number of even numbers=%d #odd=%d\n",even_count,odd_count);
return;
}
return(0);
}