Basic Programming (Assigning statements):
Before starting the C programming let us see some BASIC
programs.It helps to understand, how to write computer instructions
in any computer oriented language.
In BASIC memory allocation is automatically allocated as we
use the variables, so it is called type less language.
BASIC is not a case sensitive language.
1. Specification:
Take any two numbers and print the sum of them.
Program:
#include<stdio.h>
main()
{
int fir,sec,sum;
clrscr();
fir=25;
sec=45;
sum=fir+sec;
printf("%d",sum);
getch();
}
Program explained:
2. Specification:
Take the length and breadth of a rectangle and print the
area and perimeter of rectangle.
Program:
#include<stdio.h>
main()
{
int l,b,area,peri;
l=45;
b=12;
area=l*b;
peri=2*(l+b);
printf ("%d",area);
printf ("%d"peri);
getch();
}
3. Specification
Take the price, quantity of a product and print the bill
Program:
#include<stdio.h>
main()
{
int price,qty,bill;
clrscr();
price=25.75;
qty=20;
bill=price*qty;
printf ("%d",bill);
getch();
}
4. Specification
Take the marks of a student in 3 subjects and print the
total and average.
Program:
#include<stdio.h>
main()
{
int maths,physics,chemistry,tot,avg;
clrscr();
maths=75;
physics=82;
chemistry=93;
tot=maths+physics+chemistry;
avg=tot/3;
printf ("%d",tot);
printf ("%d",avg);
getch();
}
Steps in writing a C program:
Writing a C program is bit difficult to write comparatively
with BASIC program.
The following steps must be followed in writing a C program.
- Declaration
of variables.
- Writing
the assigning statements.
- Printing
the output using printf( ) statements.
- Placing
the total program with in the structure of C program.
- Executing
the program.
- Rectifying
the errors if any.
Step1:
In BASIC variables are automatically declared as we use,
called auto-declaration.
In C language variables have to be declared using related
keywords to allocate the memory in the main memory (RAM) of computer.
All the variables used in a program must be declared in the
declaration section of program.
Example:
Note: We can declare multiple variables of same type in
single statement using (,) operator.
Example:
Step2:
Writing the assigning statements.
Step3:
Printing the output using the output statement (printf())
We will see the mechanism of printf () through different
examples.
Example:
#include<stdio.h>
main()
{
printf(“Hello World”);
}
Output:
Hello World
Example explained:
The string or text written within “ “ will be printed as it
is. It is called format string.
Example:
#include<stdio.h>
main()
{
printf (“Hello”);
printf(“World”);
printf(“Dennis Ritchie”);
}
Output:
HelloWorldDennisRitchie
Example explained:
Printf() prints the output in the same line.
Example:
#include<stdio.h>
main()
{
printf(“Hello\nWorld”);
}
Output:
Hello
World
Example explained:
\n is the next line
character sends the output control to the next line.
Example:
#include<stdio.h>
main()
{
int num;
num=45;
printf(“num”);
}
Output:
num
Example:
#include<stdio.h>
main()
{
int num;
num=45;
printf(“%d”,num);
}
Output:
45
Example explained:
Here printf prints “num” instead of the value of num.
In-order to print the value of num instead of “num”, we must write the num
after format string by separating with comma. It must be represented in format
string using representation characters.
%d is the representation character for integer
%f is the representation character for float.
Example:
Output:
Value of x is 45
Value of y is 25
Sum of two numbers 70
Example:
Output:
Sum of
45.250000 and 25.750000 is 71.000000
Syntax to printf( ):
printf(“<format string>”, list of variables);
0 comments:
Post a Comment