Programming
Language C Tutorial – III
(Programs on printf(), scanf(), Mathematical
operators)
Let us do the simple program to print given
statement as output. Here we simply need to use printf() method to print statement.
We must include stdio.h (for print(),
scanf()) and conio.h(for clrscr(), getch()) in every program.
// prog to print
message - heading
of prog. (// - single line comment)
#include<stdio.h> - header
/library files
#include<conio.h>
void main() - begining
of pro by main() method.
{
clrscr(); - to clear screen
printf("\nWelcome to learn C
language");
getch(); - to display output on screen
}
|
Output – Welcome to
learn C language
|
Following program is to print sum of two
values. If we starting logic from beginning then we found that here we need
three variables. Two variables are for input values and one is for output.
After beginning i.e. after main() method there
is declaration part then write a input section followed by process section and
finally print output.
Printf() function containing \n as a first
argument for new line.
//prog to perform
addition of two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2,add;
clrscr();
//input
printf("\nEnter any two numbers");
scanf("%d%d",&no1,&no2);
//process
add = no1 + no2;
//output
printf("\nAddition = %d",add);
getch();
}
|
Output – Enter any
two numbers
12
23
Addition = 35
|
After you have studied above program then for
better practice you should do programs for subtraction, multiplication and
division.
How to create program for displaying mark
sheet of student?
Now again think in logical order then you can
find that here we need variables for roll no., name, marks of subjects, total
and percentage. Then think about declaration of variables in appropriate data
types, what is input? How is the process? And what will be the output?
//prog to print
marksheet of student
#include<stdio.h>
#include<conio.h>
void main()
{
int rlno,s1,s2,s3,tot;
float per;
char sname[20]; //it
accepts value 0 – 20 characters
clrscr();
printf("\nEnter roll no of student");
scanf("%d",&rlno);
printf("\nEnter name of student");
scanf("%s",&sname);
printf("\nEnter marks of 3 subjects");
scanf("%d%d%d",&s1,&s2,&s3);
//process
tot = s1 + s2 + s3;
per = (float)tot/3;
printf("\nTotal = %d\n",tot);
printf("\nPercent = %.2f",per); //it will
display per with 2 digit after decimal point
getch();
}
|
Output – Enter roll
no of student
1
Enter name of student
Ram
Enter marks of 3 subjects
65
78
86
Total = 229
Percentage = 76.33
|
From above program you should create
following programs
- Item bill contains item code, item name, price, quantity, amount, discount(10% on amount) and net amount.
- Payslip of employee conataing details empcode, empname, department, salary, annual salry, bonus(40% on annual salary) and gross salary.
Now, let us write a simple program to
calculate area of circle. Here we should have two variables one for radius and
other for area. Here we directly use standard value of pi.
// prog to
calculate area of circle
#include<stdio.h>
#include<conio.h>
void main()
{
int rad;
float pi=3.14,area;
clrscr();
printf("\nEnter radius of
circle");
scanf("%d",& rad);
area=pi*rad*rad;
printf("\nArea of
circle=%.2f",area);
getch();
}
|
Output – Enter
radius of circle
3
Area of circle = 28.26
|
You can be extend above program by adding
formula of circumference. You should do this for good programming practice. You
can be performing area of rectangle, area of square etc. programs.
Let us discuss small program having a
different logic. Program to swap (interchanged) two integers. For this program
we need three variables. Two for input and one more for swapping values. We
have third variable temp because two variables already contained values.
Compare it with a example – If mother tell
you to exchange the tin of sugar and tea then you should have a third empty
container.
// prog to swap two
integers
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,temp;
clrscr();
printf("\nEnter two values for swaping");
scanf("%d%d",&num1, &num2);
printf("\nBefore
swaping num1=%d \t num2=%d",num1,num2)
temp = num1;
num1 = num2;
num2 = temp;
printf("\nAfter swaping num1=%d \t
num2=%d",num1,num2);
getch();
}
|
Output – Enter two
values for swaping
300
900
Before swaping num1=300 num2=900
After swaping num1=900 num2=300
|
No comments:
Post a Comment