0% found this document useful (0 votes)
172 views11 pages

Computer Programming Basics in C

This document contains notes from Lecture 2 of the course MA 511: Computer Programming. It discusses finding the largest number in a set of numbers using a program written in the C programming language. The lecture also provides examples of C programs and covers data types, variable declaration, operators, and operand conversion in C. Exercises are presented for students to practice writing C programs to solve problems involving mathematical operations, prime numbers, and Fibonacci sequences.

Uploaded by

Naveen Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views11 pages

Computer Programming Basics in C

This document contains notes from Lecture 2 of the course MA 511: Computer Programming. It discusses finding the largest number in a set of numbers using a program written in the C programming language. The lecture also provides examples of C programs and covers data types, variable declaration, operators, and operand conversion in C. Exercises are presented for students to practice writing C programs to solve problems involving mathematical operations, prime numbers, and Fibonacci sequences.

Uploaded by

Naveen Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MA 511: Computer Programming

Lecture 2:

http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html

Partha Sarathi Mandal


[email protected]
Dept. of Mathematics, IIT Guwahati
Semester 1, 2010-11
Largest of the set of numbers
START

READ NUMBER

LARGEST  NUMBER

ANY MORE No
WRITE LARGEST
NUMBERS
?
Yes STOP
READ NUMBER

No IS Yes
NUMBER > LARGEST  NUMBER
LARGEST
MA511: Computer Programming
Partha S Mandal, IITG
Exercise
1. Celsius to Fahrenheit conversation.
2. The largest of a set of numbers.
3. Given integer is prime or composite.
4. LCM of two given integers.
5. GCD of two given integers.
6. Solve: Ax^2 + Bx + C = 0
7. The Fibonacci numbers:
Let a0 = 1 and a1 = 1, and an be given by the following
recursive definition: an = an-1 + an-2

MA511: Computer Programming


Partha S Mandal, IITG
Exercise
8. Write a program which takes in a positive
integer and prints one factorization of it into
primes.

MA511: Computer Programming


Partha S Mandal, IITG
Example C Program
Preprocessor directive, stdio.h is included in the compiled
machine code at # . It contains the standard I/O routines.
Must be in the first column. Must not end with a semicolon.
/* Calculate the area of a circle */ main() is a function, is required in all C
#include<stdio.h> pgs, it indicate start of a C pgs., main() it
main(){ is not followed by a comma or
semicolon.
float radius, area;
printf("Radius = ? "); Braces { and } enclosed the
scanf("%f", &radius); computations carried out by
main()
area = 3.14159 * radius * radius;
printf("Area = %f", area);
} /* end of main */ Every statement is
Declaration : it informs the terminated by a semicolon
compiler that radius and
area are variables names Comments (remarks) are placed
and that individual boxes anywhere within the program within
must be reserved for them delimiters /* end of main */ or // end…
in the memory of Computer
MA511: the Programming
computer. Partha S Mandal, IITG
Example C Program
Symbolic constant is a name that substitutes for a
sequence of characters; numeric, character or string
/* Calculate the area of a circle */ constant. It is replaced by its corresponding character
#include<stdio.h> constant during compile
#define PI 3.14159
main(){
float radius, area;
int i, n; printf & scanf are not part of the C language;
printf("n = ? "); there is no input or output defined in C itself.
scanf("%d", &n); Its just a useful function from the standard
for(i=1; i<= n; i=i+1){ library of functions that are normally
printf("Radius = ? "); accessible to C pgs. The behavior of printf &
scanf("%f", &radius); scanf are defined in the ANSI standard.
area = PI * radius * radius;
printf("Area = %f\n", area);
} /* end of for */
for is looping statement , the 1st expression specifies
} /* end of main */ nd
an initial value for an index, 2 determines whether or
not the loop is continued, 3rd allows the index to be
MA511: Computer Programming
modified at the end of each pass.
Partha S Mandal, IITG
Data types
1. Integer constant
int a, b; (for 32bit machine )
long int a, b;
Signed (- 231 to + 231 -1), Unsigned : (0 to 232 -1)
short int a, b;
Signed: Considered 16 bit integer with range (- 215 = -32768 to + 215 -1 =
+32767), Unsigned (0 to (216 -1)=65535)
2. Floating point constant
float a = 2.0, b = 0.9999, sum = 0.; [restricted within 3.4 x 10-38 to 3.4 x 1038 ]
double fact = 0.11236E-6; [0. 11236 x 10-6] [within 1.7 x 10-308 to 1.7 x 10308 ]
3. Character constant
char a = ‘x’, b = ‘3’, c = ‘#’, text[18] = “kolkata”;
4. String constant
string a = “Delhi, 100011”, b = “$20.95”;

MA511: Computer Programming


Partha S Mandal, IITG
Data types
1. Integer constant
int a, b; (for 32-bit machine)
long int a, b;
Signed (- 231 to + 231 -1),
Unsigned : (0 to 232 -1)
short int a, b;
Signed: Considered 16 bit integer
with range (- 215 = -32768 to + 215 -1 =
+32767), Unsigned (0 to (216 -
1)=65535)

MA511: Computer Programming


Partha S Mandal, IITG
Variable declaration
int i,j,k;
long p,q;
short s;
unsigned u;
float r;
double dr;
long double lr;
char c, text[10];
MA511: Computer Programming
Partha S Mandal, IITG
Operators
Operator purpose
+ addition
- subtraction
* multiplication
/ division
% remainder after integer division
(module operator)

MA511: Computer Programming


Partha S Mandal, IITG
Operands conversion
• float @ double = double @ : operator
• float @ long double = long double
• float @ char/long int/short int/int = float
• long int @char/short int/int = long int
int i = 7;
float f = 5.5;
char c = ‘w’ [w= 119 (ASCII) American Standard Code for
Information Interchange]
i+f = 12.5 float
i+c = 126 int
i+c-’0’ = 78 int [zero, 0= 48 (ASCII)]
(i+c)-(2*f/5) = 123.8 float

MA511: Computer Programming


Partha S Mandal, IITG

You might also like