Basic Examples using different datatypes
- To calculate area of a square when side is given
#include
#include
void main()
{
clrscr();
int side,area;
side=4;
area=side*side;
printf("Area of the square is %d",area);
} - To calculate area of a circle when radius is given
#include
#include
void main()
{
clrscr();
float radius,area;
radius=5.6;
area=3.14*radius*radius;
printf("\nArea of the circle is %f",area);
} - To calculate income tax of a person as 2% of his/her salary.
#include
#include
void main()
{
clrscr();
int salary;
float tax,netsalary;
salary=4000;
tax=salary*2/100;
netsalary=salary-tax;
printf("\nTotal Salary %d",salary);
printf("\nIncome Tax %f",tax);
printf("\nNet Salary %f",netsalary);
} - To print a char value
#include
#include
void main()
{
clrscr();
char c;
c='G';
printf("The alphabet is %c",c);
}
Points to remember
- The header file
is added for the clrscr() which clears the previous outputs from the screen. It can be omitted if we don't want to use clrscr(). - You can change the order of header files.
- "\n" is used to print the outputs on different lines. Removing \n will print all the messages in one line.
- %d is used whenever we want to print an integer value,%f is used to print a float(decimal) value while %c denotes a char variable.
- If we want to restrict the number of decimal positions to two we can add ".2" after the "%" sign in %f. For example, the statement
printf("\nNet Salary %.2f",netsalary);
will print only 2 decimal digits. Similarly we can %.3f or %.4f
0 comments:
Post a Comment