You are on page 1of 27

INTRODUCTION TO

PROGRAMMING

RAHUL KHANNA
IT - 2
760/IT/14

INTRODUCTION TO C

C programming is an ANSI/ISO standard and powerful programming language for developing real
time applications. C programming language was invented by Dennis Ritchie at the Bell Laboratories in
1972. It was invented for implementing UNIX operating system. C programming is most widely used
programming language even today. All other programming languages were derived directly or
indirectly from C programming concepts. C programming is the basis for all programming languages.
This C programming tutorial explains all basic concepts in C like history of C language, data types,
keywords, constants, variables, operators, expressions, control statements, array, pointer, string,
library functions, structures and unions etc.
This C programming tutorial is designed for the new learners, students and also for the corporate level
developers who want to learn and refresh their C programming skills.

C programming history

The C programming language is a structure oriented programming language, developed at


Bell Laboratories in 1972 by Dennis Ritchie

C programming language features were derived from an earlier language called B (Basic
Combined Programming Language BCPL)

C language was invented for implementing UNIX operating system

In 1978, Dennis Ritchie and Brian Kernighan published the first edition The C Programming
Language and commonly known as K&R C

In 1983, the American National Standards Institute (ANSI) established a committee to provide
a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or
ANSI C, was completed late 1988.

Features of C programming language:

Reliability

Portability

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

Flexibility

Interactivity

Modularity

Efficiency and Effectiveness

Uses of C programming language:


The C programming language is used for developing system applications that forms a major portion of
operating systems such as Windows, UNIX and Linux. Below are some examples of C being used.

Database systems

Graphics packages

Word processors

Spreadsheets

Operating system development

Compilers and Assemblers

Network drivers

Interpreters

Which level is C language belonging to?


S.no

High Level

Middle Level

High level languages provide


almost everything that the
programmer might need to do as
already built into the language

Middle level languages dont provide all the


built-in functions found in high level
languages, but provides all building blocks
that we need to produce the result we want

Examples:
Java, Python

C, C++

Low Level
Low level languages
provides nothing other than
access to the machines
basic instruction set
Assembler

The C language is a structured language


S.no
1

Structure oriented
In this type of language, large

Object oriented

Non structure

In this type of language, programs are There is no specific structure

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

programs are divided into small


programs called functions

divided into objects

for programming this language

Prime focus is on functions and


procedures that operate on the data

Prime focus is in the data that is being


operated and not on the functions or
procedures

Data moves freely around the systems Data is hidden and cannot be
from one function to another
accessed by external functions

N/A

Program structure follows Top Down


Approach

Program structure follows Bottom UP


Approach

N/A

Examples:

C++, JAVA and C# (C sharp)

N/A

BASIC, COBOL, FORTRAN

C, Pascal, ALGOL and Modula-2

Key points to remember in C language:


1. The C language is structured, middle level programming language developed by Dennis
Ritchie
2. Operating system programs such as Windows, Unix, Linux are written in C language
3. C89/C90 and C99 are two standardized editions of C language
4. C has been written in assembly language

C programming basics
Below are few commands and syntax used in C programming to write a simple C program. Lets see
all the sections of a simple C program line by line.
S.no
Command
1 #include <stdio.h>
2
3
4
5
6
7
8

Explanation
This is a preprocessor command that includes standard input output
header file(stdio.h) from the C library before compiling a C program
int main()
This is the main function from where execution of any C program
begins.
{
This indicates the beginning of the main function.
/*_some_comments_*/ whatever is given inside the command /* */ in any C program, wont
be considered for compilation and execution.
printf(Hello_World! ); printf command prints the output onto the screen.
getch();
This command waits for any character input from keyboard.
return 0;
This command terminates C program (main function) and returns 0.
}
This indicates the end of the main function.

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

____________________________________________________________

C printf and scanf

printf() and scanf() functions are inbuilt library functions in C which are available in C library
by default. These functions are declared and related macros are defined in stdio.h which is a
header file.

We have to include stdio.h file as shown in below C program to make use of these printf()
and scanf() library functions.

1. C printf() function:

printf() function is used to print the character, string, float, integer, octal and hexadecimal
values onto the output screen.

We use printf() function with %d format specifier to display the value of an integer variable.

Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for
double and %x for hexadecimal variable.

To generate a newline,we use \n in C printf() statement.

2. C scanf() function:

scanf() function is used to read character, string, numeric data from keyboard

Consider below example program where user enters a character. This value is assigned to
the variable ch and then displayed.

Then, user enters a string and this value is assigned to the variable str and then displayed.

C Data Types
C data types are defined as the data storage format that a variable can store a data to perform a
specific operation. Data types are used to define a variable before using in a program. Size of
variable, const and array are determined by data types. There are four data types in the C language.
They are
S.no
1
2

Types
Basic data types
Enumeration data type

Data Types
int, char, float, double
Enum

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

3
4

Derived data type


Void data type

pointer, array, structure, union


Void

C Tokens and keywords:


C tokens, Identifiers and Keywords are the basics in a C program. All are explained in this page with
definition and simple example programs. C tokens are the basic building blocks in C language which
are constructed together to write a C program. Each program element in a C program is given a name
called identifiers.

Constants:
C Constants are also like normal variables. But, the only difference is, their values cant be modified
by the program once they are defined. Constants refer to fixed values. They are also called as
literals. Constants may be belonging to any of the data type.

Variables:
C variable is a named location in a memory where a program can manipulate the data. This location is
used to hold the value of the variable. The value of the C variable may get changed in the program.
The C variable might be belonging to any of the data types like int, float, char etc.

Operators and Expressions:


The symbols which are used to perform logical and mathematical operations in a C program are
called C operators. These C operators join individual constants and variables to form expressions.
Operators, functions, constants and variables are combined together to form expressions.

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

Practicals
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

Q1. If a 5-digit number is input through keyboard write a program to

print a new number by adding 1 to each of the digits.


ALGORITHM:
1.Start
2.Declare variables for number, sum, remainder and check
3.Input variables
4.While( number>0)
{rem = number%10;
If(rem!=9)
If(check == 0)
sum = 10*sum + rem +1;
Else
sum = 10*sum + rem +2;
check = 0;
Else
sum = 10*sum;
check = 1;
}number = number/10;
5. number = sum, sum =0;
6. while(number>0)
{
rem = no%10;
sum = 10*sum + rem;
number = number/ 10;
}
7.Print Sum;
8.Stop;

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

FLOWCHART:

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

OUTPUT:

SOURCE CODE:

#include<stdio.h>
int main()
{
long int no,sum=0;
int rem,check=0;
printf("Enter the required number:");
scanf("%ld",&no);
printf("\nGiven Number: %d",no);
while(no>0)
{
rem=no%10;
if(rem!=9)
{
if(check==0)
sum=(10*sum)+(rem+1);
else{
sum=(10*sum)+(rem+2);
check=0;
}
}
else
{
sum=(10*sum)+0;
check=1;
}
no=no/10;
}
no=sum; sum=0;
while(no>0)
{
rem=no%10;
sum=(10*sum)+rem;
no=no/10;
}
printf("\nAfter Adding one: %ld",sum);
return 0;
}

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

Q2. Basic salary is input through the keyboard. His dearness allowance

is 40% of basic salary, and house rent allowance is 20% of basic salary.
Write a program to calculate his gross salary.
ALGORITHM:
1.Start
2.Declare variables for Gross salary,hr,bs.
3.Input Basic Salry
4.Calculations:
Da = 0.4*basic salary
Hra = 0.2*basic salary
Gross salary = HRA + DA + Basic Salary;
5.Print Gross Salary
6.Stop

FLOWCHART:

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

OUTPUT:

SOURCE CODE:

#include<stdio.h>
int main()
{
float bs,da,hr,gs;
printf("Enter basic salary : ");
scanf("%f",&bs);
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

da=(40*bs)/100;
hr=(20*bs)/100;
gs=bs+da+hr;
printf("DA:%f\nHR:%f\nGross Salary:%f\n",da,hr,gs);
return 0;
}

Q3. Enter marks of 5 subjects(out of 100), and calculate percentage


ALGORITHM:
1.Start
2.Declare an array for marks and a variable for sum (float type)
3.Input marks in array
4.Calculate sum of marks
5.Output sum/5
6.Stop
FLOWCHART:

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

OUTPUT:

SOURCE CODE:
#include<stdio.h>
int main()
{
float marks[5],sum=0;
int i;
printf("Enter marks of :\n");
for(i=0;i<5;i++)
{
printf("Subject %d:",i+1);
scanf("%f",&marks[i]);
}
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

for(i=0;i<5;i++)
{
sum = sum + marks[i];
}
printf("Percentage = %f",sum/5);
return 0;
}

Q4.A 6 digit number is input through the keyboard, write a program to


calculate the sum of the digits of the number.
ALGORITHM:
1.Start
2.Declare variables for num and sum
3.While(num!=0)
sum = sum + num%10;
num = num/10;
4.Output Sum
5.Stop
FLOWCHART:

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

OUTPUT:

SOURCE CODE:
#include<stdio.h>
int main()
{
long int num;
int sum = 0;
printf("Enter the num:");
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

scanf("%ld",&num);
while(num!=0)
{
sum = sum + num%10;
num = num/10;
}
printf("Sum of digits: %d",sum);
return 0;
}

Q5. A company insures its drivers in the following cases:


codeblocks
- If the driver is unmarried, male and above 30 years of age.
- If the driver is unmarried, female and above 25 years of age.
In all the other cases, the driver is not insured.
If the marital status, sex and age of the driver are the inputs,
write a program to determine whether the driver is insured or not.

ALGORITHM:
1.Start
2.Declare variables for sex,marital status, age
3.if marital status is married
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

Driver is insured
4.If marital status is not married,sex = male and age is more than 30
Driver insured
5. If marital status is not married,sex = female and age is more than 25
Driver is insured
6.Else Driver not insured
7.Stop
FLOWCHART:

OUTPUT:
s

SOURCE CODE:
#include<stdio.h>
int main()
{
char sex,ms;
int age;
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

printf ("Enter age, sex, marital status:");


scanf ("%d %c %c", &age, &sex, &ms);
if (ms=='M'||ms=='m')
printf ("The driver is insured");
else
{
if (sex=='M'||sex=='m')
{
if (age>30)
printf ("Driver is insured");
else
printf ("Driver is not insured");
}
else
{
if (age>25)
printf ("Driver is insured");
else
printf ("Driver is not insured");
}
}
return 0;
}

Q6. Write a program that takes coefficients of a quadratic equation as


input and outputs the roots of the quadratic equation.

SOURCE CODE :
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main() {
float a, b, c, D ,x1, x2, rp , ip;
printf("\nEnter the coefficients of quadratic equation in the form of ax^2+bx+c
= \n");
scanf("%f %f %f", &a, &b,&c);
D=b*b-4*a*c;
if(D>=0) {
x1= (-b+sqrt(D))/(2*a);
x2= (-b-sqrt(D))/(2*a);
printf("The roots are %f %f", x1, x2);
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

}
else {
rp=b/(2*a);
ip=sqrt(-D)/(2*a);
printf("The roots are %f +i%f ,%f -i%f", rp, ip, rp,ip);
}
return 0 ;
getch();
}
OUTPUT:

Q7.Write a menu-driven program (using switch statement) that takes


two input integers and performs addition, subtraction, division and
multiplication based on the menu option selected by the user.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main() {
float a,b,result = 0.0;
int choice;
printf("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\nEnter your
choice:");
scanf("%d", &choice) ;
printf("\nEnter the numbers:");
scanf("%f %f", &a, &b);
switch (choice) {
case 1:
result=a+b;
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

break;
case 2:
result = a-b;
break;
case 3:
result = a*b;
break;
case 4:
result = a/b;
break;
}
printf("\nThe result is = %f", result);
return 0;
getch();
}
OUTPUT:

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

Q8.A certain grade of steel is graded according to the following


conditions:
i.
Hardness must be greater than 50
ii.
Carbon content must be less than 0.7
iii.
Tensile strength must be greater than 5600
The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii) are met
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main() {
int hn, ts;
float carbon;
printf("Enter the hardness, carbon content and tensile strength of the steel : ");
scanf("%d %f %d",&hn, &carbon ,&ts);
if (hn>50&&carbon<0.7&&ts>5600)
printf("The grade of steel is 10");
else if(hn>50&&carbon<0.7)
printf("The grade of steel is 9");
else if(carbon<0.7&&ts>5600)
printf("The grade of steel is 8");
else if(hn>50&&ts>5600)
printf("The grade of steel is 7");
else if (hn>50||carbon<0.7||ts>5600)
printf("The grade of steel is 6");
else
printf("The grade of steel is 5");
return 0 ;
getch();
}
OUTPUT:

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

Q9.Write a program to find the greatest of three number s input


through keyboard.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main() {
int a,b,c;
printf("Enter any three different numbers:" );
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
printf("The greatest number is %d " , a );
else if(b>c)
printf("The greatest number is %d " , b);
else
printf("The greatest number is %d " , c);
return(0);
getch();
}
OUTPUT:

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

Q10..Write a program to find sum of the following series:1+

1
2

1
3 + +

1
20

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main() {
float i;
float a;
float sum=0;
for(i=1;i<21;i++) {
a=1/i;
sum=sum+a;
}
printf("Sum of series is %f", sum );
return(0);
getch();
}
OUTPUT

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

Q11.Write a program to find the sum of the Fibonacci series: 0 + 1 + 1


+ 2 + 3 + 5 + . + n.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main() {
int first=0, second=1, next, i,n, sum=0;
printf("Enter the number terms: ");
scanf("%d",&n);
for (i=0; i<n ; i++) {
if(i<=1)
next=i;
else {
next = first + second ;
first = second ;
second= next;
}
sum=sum+next;
}
printf("Sum of first %d terms of Fibonacci series is: %d",n,sum);
return 0;
}

OUTPUT:

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

Q12.Write a program to find the sum of products is defined as adding


the product of corresponding digits from two numbers.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main() {
int a, b, p, q, pos=0;
printf("Enter the two numbers: ");
scanf("%d %d", &a, &p);
while (a>0 && p>0) {
b=a%10;
q=p%10;
a=a/10;
p=p/10;
pos=pos+ b*q ;
}
printf("The sum of products is: %d ", pos );
return 0;
getch();
}

OUTPUT:

Q13.Write a program to print the following pattern.


__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

A
A
A
A
A
A
A

B
B
B
B
B
B

C
C
C
C
C

DEFGFED
DEF
FED
DE
ED
D
D

C
C
C
C
C

B
B
B
B
B
B

A
A
A
A
A
A
A

SOURCE CODE:
#include<stdio.h>
int main()
{ int n=7,row,k,j;
for(row=0;row<n;row++)
{
for(k=65;k<=72-row-1;k++)
printf("%c",k);
for(int j=0;j<row;j++)
printf(" ");
for(int j=row;j>1;j--)
printf(" ");
if(row==0)
{
for(int k=72-row-2;k>=65;k--)
printf("%c",k);
}
else{
for(int k=72-row-1;k>=65;k--)
printf("%c",k);}
printf("\n");
}
return 0;
}
OUTPUT:

__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14

You might also like