Published by: Nuru
Published date: 21 Jun 2021
Formatted I/O can be studied in the following two topics:
Note:
The programs succeeding this slide clarify the use of formatted input.
#include
#include
void main()
{
int n1;
char ch;
printf(“Enter a number:”);
scanf(“%d”,&n1);
printf(“\nEnter a character:”);
scanf(“ %c”,&ch); //space before %c
printf(“\n Number: %d\t Character: %c”,n1,ch);
getch();
}
#include
#include
void main()
{
int day,month,year;
clrscr();
printf("Enter day, month, year in DD ("Enter day, month, year in DD-MM-YYYY format:"); YYYY format:");
scanf(" %d-%d-%d", &day, &month, &year);
printf("\nDay: %d \t Month: %d \t Year:%d", day, month, year);
getch();
}
#include
#include
void main()
{
int n1;
clrscr();
printf("Enter a number of max 5 digits:");
scanf("%5d",&n1);
printf("Number: %d",n1);
getch();
}
#include
#include
#include
void main()
{
float p,t,r,si,ci,sa,ca;
clrscr();
printf("Enter principal in Rs:");
scanf(" %f",&p);
printf("\nEnter time in year:");
scanf("%f",&t);
printf("\nEnter rate in percent per annum:");
scanf("%f",&r);
si=(p*t*r)/100;
printf("\nSimple interest:%.2f",si);
ci=p*(pow(1+r/100,t)-1);
printf("\nCompound interest:%.2f",ci);
sa=p+si;
printf("\nSimple amount:%.2f",sa);
ca=p+ci;
printf("\nCompound amount:%.2f",ca);
printf("\nThe difference between compound and simple amount:%.2f",ca-sa);
getch();
}
Flags may be,
#include
#include
void main() {
int a=10;
float x=11.123456;
clrscr();
printf("%-6d",a);
printf("\n");
printf("%+d",a);
printf("\n");
printf("%09d",a);
printf("\n");
printf("% d",a);
printf("\n");
printf("%#o",a);
printf("\n");
printf("%#0x",a);
printf("\n");
printf("%7.2f",x); //precision format %w.pf
fetch();