Unit-Ii Psc-Ug
Unit-Ii Psc-Ug
UNIT II
Introduction to C: Introduction – Structure of C Program –
Writing the first C Program –File used in C Program –
Compiling and Executing C Programs – Using Comments –
Keywords – Identifiers – Basic Data Types in C – Variables –
Constants – I/O Statements in C- Operators in C-
Programming Examples.
Decision Control and Looping Statements: Introduction to
Decision Control Statements– Conditional Branching
Statements – Iterative Statements – Nested Loops – Break
and Continue Statement – Goto Statement
Introduction: C language is a programming language used to write
C Programs.
Characteristics of C language:
C is a programming language was developed in 1972 by
Dennis Ritchie at bell laboratories of AT&T (American
Telephone & Telegraph), located in U.S.A.
It was developed to overcome the problems of previous
languages such as B, BCPL etc.
This language
was developed
during the period when UNIX operating system is developed.
Many features that are required to write system programs are
added to C language.
C is called middle-level language because it combines the
advantages of a machine level language and high-level
languages.
C language can be used to write System programs as well as
Application Programs.
Advantages of C Language:
1. Stable language: C is 35 years old language and it is not
revised since last 20 years.
2. Modular language: Large programs can be divided into smaller
functions to reduce complexity.
3. Portable language: Programs written for one computer can be
executed on other computer.
Krishnaveni Degree College :: Narasaraopet Page No. : 1
Problem Solving in C
UNIT II
4. Concise language: There are only 32 keywords in C.
5. Extend itself: C language has the ability to extend itself.
6. Rich in operators: C language supports a rich set of built-
in operators.
Disadvantages of C Language
1. Case sensitive: It is a case sensitive language which makes
difficult while programming.
2. Less library functions: There is no enough library functions so
programmer has to develop functions.
3. Doesn’t support OOP’s: C does not have concept of OOP’s,
that’s why C++ is developed.
4. No Strict type checking: There is no strict type checking.
Applications:
1. C programming language can be used to design Operating
System.
2. C programming language can be used to design the compilers.
3. To Develop computer and mobile games.
4. MySQL, again being an open-source project, used in Database
Management Systems was written in C/C++.
5. Adobe Photoshop, one of the most popularly used photo editors
since olden times, was created with the help of C.
Figure: Files in c
Source Code File: This file includes the source code of the program.
The extension for these kind of files are '.c'. It defines the main and
many more functions written in C. main() is the starting point of the
program. It may also contain other source code files.
Basic data types: Every variable in C has a data type. Data types
specify the amount of memory to be allocated and type of value
that can be stored in a variable. C language is rich in its data types
and they are classified into two data types. They are
1. Primitive data types or Basic data types or Primary data types
2. Non-primitive data types or Derived data types
The classification of data types in C are shown in figure below.
Programming Examples:
1. Write a C Program to calculate simple interest.
/* C Program to calculate simple interest */
#include<stdio.h>
void main() Step 1: Start
{ Step 2: Read P, T, R
float p,t,r,si;
Step 3: SI = (P * T * R)
clrscr();
printf("Enter principal, rate and /100
time : "); Step 4: Print SI
scanf("%f%f%f",&p,&t,&r); Step 5: End
si=p*t*r/100;
printf("Simple Interest =%.2f\n",si);
getch();
}
/* C Program to find area of the triangle when three sides are given
*/
#include<stdio.h> Step 1: Start
#include<math.h> Step 2: Read a, b, c
void main() Step 3: s = (a + b + c) / 2
{ Step 4: area=sqrt(s*(s-a)*(s-
float area, a, b, c, s; b)*(s-c))
clrscr(); Step 5: Print area
printf("Enter three sides of the Step 6: End
triangle: ");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of the triangle = %.2f \n", area);
getch();
}
{
statement-block;
}
next-statement;
{
if(condition2)
{
statement1-block;
}
else
{
statement2-block;
}
}
else
{
statement3-block;
}
next-statement;
Example: Write a
C Program to check
whether given year is
leap year or not
Krishnaveni Degree College :: Narasaraopet Page No. : 21
Problem Solving in C
UNIT II
/* C Program to find that entered year is leap year or not */
#include<stdio.h> Algorithm: LeapYear
#include<conio.h> Step 1: Start
void main() Step 2: Read year
{ Step 3: if(year%4 == 0)
int year; if( year%100 ==
clrscr(); 0)
printf("Enter any year: "); if (year%400 ==
scanf("%d",&year); 0)
if(year%4 == 0) print("leap
if( year%100 == 0) year”)
if (year%400 == 0) else
printf("%d is a leap year\n", year); print("not leap
else year”)
printf("%d is not a leap year\n", year); else
else print("leap year”)
printf("%d is a leap year\n", year ); else
else
printf("%d is not a leap year\n", year);
getch();
}
else-if ladder : When one if statement is added in the else part of
another if statement then it is called as else – if ladder statement.
Syntax: Flowchart:
if(condition1)
statement1-block;
else
if(condition2)
statement2-block;
else
if(condition3)
statement3-block;
else
statement4-block;
next-statement;
Example: Write a C Program
to find largest among 3 numbers.
/* C Program to find largest among 3 numbers */
#include<stdio.h> Algorithm: Maximum
#include<conio.h> Step 1: Start
void main() Step 2: Read a, b, c
{ Step 3: if((a>b)&&(a>c))
float a,b,c; print(a,"is maximum”)
Krishnaveni Degree College :: Narasaraopetelse Page No. : 22
if(b>c)
print(b,"is maximum”)
else
Problem Solving in C
UNIT II
clrscr();
printf("Enter any three numbers: ");
scanf("%f%f%f",&a,&b,&c);
if((a>b)&&(a>c))
printf("%.2f is largest", a);
else
if(b>c)
printf("%.2f is largest", b);
else
printf("%.2f is largest", c);
getch();
}
Multiway selection: switch: The switch statement is a multiway
branch statement. It provides an easy way to select one of the
option among several options. It often provides a better alternative
than a large series of if-else-if statements. Here is the general form
of a switch statement:
Syntax: Flowchart:
switch (expression)
{
case value1: statement1-
block;
break;
case value2: statement2-
block;
break;
...
case valueN: statementN-
block;
break;
default: defaultstatement-
block;
}
The expression must be of type int, or char. Each case value
must be a unique constant, not a variable. Duplicate case values are
not allowed.
The switch statement works like this: The value of the
expression is compared with each of the case values in the case
statements. If a match is found, the code sequence following that
case statement is executed. If none of the constants matches the
value of the expression, then the default statement is executed.
However, the default statement is optional. If no case matches and
no default is present, then no further action is taken. The break
Krishnaveni Degree College :: Narasaraopet Page No. : 23
Problem Solving in C
UNIT II
statement is used inside the switch to terminate a statement
sequence. When a break statement is encountered, execution
branches to the first line of code that follows the entire switch
statement. This has the effect of “jumping out” of the switch.
Example: Write a C program to display name of the day using
switch statement. Algorithm: DayName
/* C Program to display name of the day */ Step 1: Start
Step 2: Read dayno
#include<stdio.h> Step 3: switch(dayno)
#include<conio.h> case 1: Print(“Monday”)
case 2:
void main() Print(“Tuesday”)
{ case 3:
int dayno; Print(“Wednesday”)
case 4:
clrscr(); Print(“Thursday”)
printf("Enter the day number: "); case 5: Print(“Friday”)
case 6:
scanf("%d",&dayno); Print(“Saturday”)
switch(dayno)
{
case 1: printf("%d day in the week is Monday\n",dayno);
break;
case 2: printf("%d day in the week is Tuesday\n",dayno);
break;
case 3: printf("%d day in the week is Wednesday\n",dayno);
break;
case 4: printf("%d day in the week is Thursday\n",dayno);
break;
case 5: printf("%d day in the week is Friday\n",dayno);
break;
case 6: printf("%d day in the week is Saturday\n",dayno);
break;
case 7: printf("%d day in the week is Sunday\n",dayno);
break;
default: printf("%d is a wrong day number\n",dayno);
}
getch();
}
// body of loop
}
Next-statement