Академический Документы
Профессиональный Документы
Культура Документы
Charudatt Kadolkar
IIT Guwahati
C Programming p.1/48
Grading Scheme
Written Examinations
Quizzes(2)
20%
Midsem
30%
Endsem
50%
Laboratory Work
70 %
30 %
C Programming p.2/48
Textbooks
1. The C Programming Language by Kernighan and Ritchie, PHI.
2. Computer Programming in C by V Rajaraman, PHI.
C Programming p.3/48
Introduction
Programming
Set of Instructions, Manual, Recipies, ...
Language
C, Fortran, Pascal, Java, ...
C Programming p.4/48
Programs
Peas Pulao
Ingredients
3 onions
4 cloves
2tick cinnamon
2 bay leaves
4 tsps oil
1 cup basmati rice
1/4 kg. peas
2 cups hot water
1 maggi cube
2 tsps ginger,garlic and chilli paste
Method
1. Slice onions and keep it aside.
2. Heat oil in a teflon vessel. Put
cloves, cinnamon and bay leaves
and stir for 30 secs.
3. Put the onions and fry till brown.
Add the maggi cube and fry for some time. Put the washed and drained rice and fry for 5 mins. Add the
ginger-garlic-chilli paste and saute
for some time.
4. Add the hot water and peas. Cover and cook on low fire till done.
5. Serve hot.
C Programming p.5/48
Programs
C Programming p.6/48
Programs
C Programming p.7/48
Hello World!
A program that prints a line
#include
<stdio.h>
main()
{
printf("Hello,
World!\n");
C Programming p.8/48
Executing a Program
$ cc helloworld.c
$ a.out
Hello, World!
$
Human-readable helloworld.c is translated to a.out which is machine-readable.
C Programming p.9/48
Simple Arithmetic
/* Arithmetic
#include
Expressions
*/
<stdio.h>
main()
{
printf("5
printf("%d
is an integer\n");
is an integer\n",5);
printf("Approxima
printf("Approxima
printf("100
te value
te value
F corresponds
of pi is 22.0/7.0\n");
of pi is %f\n",22.0/7.0);
to %f",5.0*(100.0-32
.0) /9 .0) ;
C Programming p.10/48
Sequential Programs
The programs are executed statement by statement from the top of the file.
Such programs are called sequential programs.
There are other possibilities, for example, parallel programs
C Programming p.11/48
Variables
Variable is a short-form for variable memory. Clearly, each variable memorizes/holds/stores a datum (either
a number or a character) that can be changed(varied) in the program.
main()
{
int
no_of_apples;
no_of_apples
= 10;
printf("I
have %d apples\n",
no_of_apples
printf("Now,
= 15;
I have
no_of_apples);
%d apples\n",
no_of_apples);
C Programming p.12/48
Variables
Variable Name.
Data Type of a variable. int,float,char
declaration of a variable
int no_of_students;
float density_of_merc
char name;
ury ;
no_of_students
density_of_mercu
name = c;
= 18;
ry = 13.6;
C Programming p.13/48
=
=
=
=
p;
r;
t;
i;
1000.0;
10.0;
3;
p * r * t / 100;
printf("Principle
printf("Rate
printf("Term
printf("Interest
=
=
=
=
%f
%f
%d
%f
Rs\n", p);
\% pa \n", r);
years \n", t);
Rs \n", i);
}
C Programming p.14/48
Result
The output:
Principle
Rate
Term
Interest
=
=
=
=
1000.000000
Rs
10.000000
% pa
3 years
300.000000
Rs
C Programming p.15/48
Quadratic Equations
Consider equation
Ax2 + Bx + c = 0.
The solution is given by
B B 2 4AC
.
x=
2
We want to write a program such that if A, B and C are given, we get solutions.
C Programming p.16/48
Quadratic Equations
C Programming p.17/48
Quadratic Equations
C Programming p.18/48
Quadratic Equations
C Programming p.19/48
Quadratic Equations
C Programming p.20/48
Quadratic Equations
#include
#include
main()
{
<stdio.h>
<math.h>
C Programming p.21/48
Quadratic Equations
#include
<stdio.h>
#include
<math.h>
main()
{
float A, B, C, x;
C Programming p.22/48
Quadratic Equations
#include
<stdio.h>
#include
<math.h>
main()
{
float A, B, C, x;
A = 1.0;
B = -4.0;
C = 3.0;
C Programming p.23/48
Quadratic Equations
#include
<stdio.h>
#include
<math.h>
main()
{
float A, B, C, x;
A = 1.0;
B = -4.0;
C = 3.0;
x = (-B + sqrt(B*B
- 4*A*C))/2.0;
printf("First
solution
= %f\n",
x);
C Programming p.24/48
Quadratic Equations
#include
<stdio.h>
#include
<math.h>
main()
{
float A, B, C, x;
A = 1.0;
B = -4.0;
C = 3.0;
x = (-B + sqrt(B*B
- 4*A*C))/2.0;
printf("First
solution
= %f\n", x);
x = (-B - sqrt(B*B
- 4*A*C))/2.0;
printf("Second
solution
= %f\n", x);
}
C Programming p.25/48
Quadratic Equations
#include
<stdio.h>
#include
<math.h>
main()
{
float A, B, C, x;
scanf("%f",
scanf("%f",
scanf("%f",
&A);
&B);
&C);
x = (-B + sqrt(B*B
- 4*A*C))/2.0;
printf("First
solution
= %f\n", x);
x = (-B - sqrt(B*B
- 4*A*C))/2.0;
printf("Second
solution
= %f\n", x);
}
C Programming p.26/48
Quadratic Equations
For any A, B, C
is given by (Let 2
= B 2 4AC
8
<
(B )/2
x=
: (B i 2 )/2
if
2 >= 0
if
2 < 0
C Programming p.27/48
Quadratic Equations
For any A, B, C
= B 2 4AC
8
< (B )/2
x=
: (B i 2 )/2
if
if
2 0
2 < 0
i 2 )/2.
C Programming p.28/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
/* scanf
delta2
etc....
= B*B
if ( delta2
*/
- 4*A*C;
>= 0 )
{
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
}
C Programming p.29/48
if Statement
The syntax of the if statement is
if ( condition
statement1
else
statement2
)
;
;
C Programming p.30/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
delta2
-4
|
/* scanf
etc....
*/
|
|
delta2
= B*B
if ( delta2
- 4*A*C;
>= 0 )
{
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.31/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
-4
delta2
|
/* scanf
etc....
*/
|
|
delta2
= B*B
- 4*A*C;
|
if ( delta2
>= 0 )
{
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.32/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
-4
4 >= 0
delta2
|
/* scanf
etc....
*/
|
|
delta2
= B*B
- 4*A*C;
|
|
if ( delta2
>= 0 )
3
is true
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.33/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
-4
delta2
|
/* scanf
etc....
*/
|
|
delta2
= B*B
- 4*A*C;
|
|
if ( delta2
>= 0 )
|
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.34/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
-4
delta2
|
/* scanf
etc....
*/
|
|
delta2
= B*B
- 4*A*C;
|
|
if ( delta2
>= 0 )
|
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.35/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
-4
delta2
|
/* scanf
etc....
*/
|
|
delta2
= B*B
- 4*A*C;
|
|
if ( delta2
>= 0 )
|
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
|
|
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.36/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
-4
delta2
|
/* scanf
etc....
*/
|
|
delta2
= B*B
- 4*A*C;
|
|
if ( delta2
>= 0 )
|
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
|
|
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
solution
complex
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
/* Execution
jumps
here
*/
}
C Programming p.37/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
delta2
-4
|
/* scanf
etc....
*/
|
|
delta2
= B*B
if ( delta2
- 4*A*C;
>= 0 )
{
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.38/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
-4
delta2
|
/* scanf
etc....
*/
|
|
delta2
= B*B
- 4*A*C;
-4
|
if ( delta2
>= 0 )
{
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.39/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
-4
-4 >= 0
delta2
|
/* scanf
etc....
*/
|
|
delta2
= B*B
- 4*A*C;
|
|
if ( delta2
>= 0 )
-4
is false
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.40/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
-4
delta2
|
/* scanf
etc....
*/
|
|
delta2
= B*B
- 4*A*C;
|
|
if ( delta2
>= 0 )
|
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.41/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
-4
delta2
|
/* scanf
etc....
*/
|
|
delta2
= B*B
- 4*A*C;
|
|
if ( delta2
>= 0 )
|
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2)/2;
x = -B/2;
printf("First
printf("Second
|
complex
complex
solution
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
C Programming p.42/48
Quadratic Equations
main()
{
float
A, B, C, x, delta2;
/* scanf
delta2
etc....
= B*B
if ( delta2
delta2
*/
- 4*A*C;
>= 0 )
{
x = (-B + sqrt(delta2))/2.0;
printf("First
solution
= %f\n",
x);
x = (-B - sqrt(delta2))/2.0;
printf("Second
solution
= %f\n",
x);
}
else
{
delta2
= sqrt(-delta2);
x = -B/2;
printf("First
printf("Second
complex
solution
complex
solution
= (%f,%f)\n",
= (%f,%f)\n",
x, delta2);
x, -delta2);
}
/* Execution
jumps
here
*/
}
C Programming p.43/48
Conditions
Condition used in if is usually a comparison of two numbers or characters using relational operators such as
<, <=,
>, >=,
==,
!=
no_of_apples
> 100
density_of_mercu
ry < 10.0
1 > 0
no_of_boys
+ no_of_girls
< no_of_benches
x == 0.0
rate
!= 8
C Programming p.44/48
Grading Program
Here is another example: A student in "C Programming"takes 100 marks examination and gets m marks.
And he will get a letter grade according to the following rule:
If m
C Programming p.45/48
Grading Program
main()
{
int
char
marks;
grade;
scanf("%d",
if ( marks
&marks);
>= 80 ) grade
= A;
if ( (marks
>= 60 ) grade
= B;
if ( (marks
>= 40 ) grade
= C;
if ( marks
printf("Grade
< 40 ) grade
= %c\n",
= F;
grade);
C Programming p.46/48
Grading Program
main()
{
int
marks;
char
grade;
scanf("%d",
if ( marks
&marks);
>= 80 ) grade
= A;
else
{
if ( marks
>= 60 ) grade
= B;
else
{
if ( marks
>= 40 ) grade
else
= F;
grade
= C;
}
}
printf("Grade
= %c\n",
grade);
}
C Programming p.47/48
E
xercise: Given three integers a, b, and c, write a program to print these in ascending order.
For Example, if a
C Programming p.48/48