PPS Unit 1 - R18
PPS Unit 1 - R18
PPS Unit 1 - R18
UNIT 1
1. Input Devices:
These are the devices through which input is given to the computer.
Ex: Keyboard, Mouse, Scanner, Joystick, Light Pen, Touch Screen, etc.
2. Output Device:
These are the devices through which the output is given by the computer.
Ex: Monitor, Printer, Speakers etc.
4. Primary storage:
The primary storage, also known as main memory, is a place where the programs
and data are stored temporarily during processing. The data in primary storage will
be erased when we turn off a personal computer(In case of RAM).
RAM(Random Access Memory):
It is also called the main memory. It is a place where the programs and data are
stored temporarily during processing. The data in this memory will be removed, when
we switch off the computer.
ROM (Read Only Memory):
ROM stores the data even the computer turned off. It is a permanent memory cannot
be modified, used to store BIOS (Basic Input Output System).
PPS(R18) UNI T 1 Page| 2
5. Auxiliary Storage:
Auxiliary storage, also known as secondary storage, is used for both input and
output. It is the place where the programs and data are stored permanently. When we
turn off the computer, our programs and data remain in the secondary storage, ready
for the next time we need them. It is the place where the programs and data stored
permanently.
1. Hardware:
All physical equipment of a computer system is called Hardware.
2. Software:
Software is a set of programs that allows Hardware to do specific job.
Software is divided into two categories.
1. System software 2. Application Software
PPS(R18) UNI T 1 Page| 3
SYSTEM SOFTWARE:
System software manages the computer resources. It provides the interface between the
hardware and the users but does nothing to directly serve the users‟ needs. System
software consists of programs that manage the hardware resources of a computer and
perform required information processing tasks.
This can be divided into 3 parts.
i. Operating system:
The operating system provides services such as a user interface, file and database
access, and interfaces to communication systems such as internet protocols. The
primary purpose of this software is to keep the system operating in an efficient
manner while allowing the users access to the system. It is a software which interacts
with the hardware directly.
APPLICATION SOFTWARE:
It is the software which is used to solve different problems. It can be divided into two
categories.
i. General purpose ii. Application specific
i. General purpose:
The General purpose software is purchased from a software developer and can be
used for more than one application.
Examples are word processors, database management systems, and computer aided
design systems. They labeled General Purpose because they can solve a variety of
users computing problems.
MACHINE LANGUAGES
In this language, instructions are written in 1's and 0's as the computers are made of
two state electronic devices that they could understand only pulsethat can be in any
one of the states: off or on. The off state is represented by 0; the on state is
represented by 1.
They are called binary code also called as machine code.
Computers are identical in design,therefore each computer has its own machine
language.
Machine languages are usually referred to as "first generation languages/low level
languages".
ASSEMBLY LANGUAGE
In this language the set of instructions are written in special words like
ADD,SUB,INC,HLT so on. These words are called "MNEMONICS" also called
"OPCODE" OR"PERATION CODE"
To convert assembly language to machine code, a tool called “Assembler”is used.
This is also called “symbolic language”, also referred to second generation
programming language, and is also “Low-level language”
PPS(R18) UNI T 1 Page| 5
}
HIGH-LEVEL LANGUAGE
Although symbolic languages greatly improved programming efficiency, they still
require programmers to concentrate on the hardware that they were using. And also it was
tedious because machine instruction had to be individually coded. The desire to improve
programmer efficiency and to change the focus from the computer to the problem being
solved led to the development of high- level language.
In this language instructions are written in normal English.
High- level languages are portable to many different computers, allowing the
programmer to concentrate on the application problem rather than the intricacies of
the computer.
High- level languages are designed to relieve the programmer from the details of the
assembly language.
High- level languages are too to be converted into machine language. The process of
converting them is known as Compilation.
Some high- level languages are FORTRAN, COBOL, C, C++ etc.
To convert high level language into machine level language we use a tool called
“compiler”.
High-level languages can be classified into 3 categories
procedure oriented languages (3rdGeneration Languages)
problem oriented language (4thGeneration Languages)
Ex: To create various items like Text Boxes, Text labels, Radio buttons, etc.
Natural language(5th Generation Languages)
Ex: LIP, PROLOG used to develop artificial intelligence and expert systems.
The preprocessor reads the source code and prepares it for the translator. While
preparing the code, it scans for special instructions known as preprocessor
commands.
These commands tell the preprocessor to look for special code libraries, make
substitutions in the code, and in other ways prepare the code for translation into
machine language. The result of the preprocessing is called the Translation Unit.
After the preprocessor has prepared the code for compilation, the translator does the
actual work of converting the program into machine language. The translator reads
the translation unit and writes the resulting object module to a file that can then be
combined with other precompiled units to form the final program.
An object module is the code in machine language called “Object File”(xxx.obj file)
Even though the output of the compiler is machine language code, it is not yet ready
to run; that is, it is not yet executable because it does not have the required C and
other functions included.
PROGRAM DEVELOPMENT:
When we are given the assignment to develop a program, we will be given a program
requirements statement and the design of any program interfaces. Also we should receive
an overview of the complete project. Our job is to determine how to take the inputs we are
given and convert them into the outputs that have been specified. This is known as program
design.
2. Develop a solution:
Once we finally understand the problem, we use three tools in developing a solution.
i. Structured Chart
ii. Algorithm/Pseudo Code
iii. Flow Chart
i. A Structured Chart also known as a “Hierarchy Chart”, is used to design the
whole program, which shows the relationship of various units. (the functional flow
the program).
3. Writing a Program:
After developing a solution the program has to be written in any language.
i. Sequence:
In this category all the instructions are performed one after the other.
Ex: Write an algorithm for telephone about the conversation between two people.
Step 1: Dial the Number.
Step 2: Phone rings at the called party
Step 3: Caller waits for the response.
Step 4: Phone lifted by the called party. (Connection Activated).
Step 5: Conversation begins.
Step 6: Release the connection. ( Connection Terminated)
ii. Selection:
In the selection, instructions will be executed based on the condition.
The selection form can be written as
if (condition) then
statement;
if (condition) then
statement1;
else
statement2;
PPS(R18) UNI T 1 Page|11
iii.Iteration:
In this category some of the instructions will be performed repeatedly for some number of
times.Iteration statements are written using the following format.
Repeat
Statements;
Until
Condition
The sys will be executed continuously if the condition is false
If the condition becomes true the next step will be executed
Ex:Write an algorithm for going to a movie with a friend condition is that , friend waits for
every two minutes
step 1:
Repeat
wait for 2 minutes
until
friend comes
step 2:
Goto movie .
{
do while
wait 2 min (friend don‟t come)
while wait for 2 min
Friend don‟t come
}
PPS(R18) UNI T 1 P a g e | 12
Algorithm1:
Write a program for adding 2 numbers
sol:
Algorithm Pseudo code:
step 1:start step 1: start
2:read 2 numbers 2: read 2 numbers a,b
3:add the 2 numbers 3: sum<-a+b (or) sum=a+b;
4:print sum 4: Display sum
5:stop 5: stop
Algorithm 2:
Write an algorithm for finding the average of 3 numbers
sol:
step 1:start
2:read 3 numbers a,b,c
3:average<-(a+b+c)/3.
0 4:print average
5:stop
Algorithm 3:
Write an algorithm to find the area of triangle
Sol:
Step 1: start
2:Read 2 numbers b,h
3:Area<-(b*h)/2.0
4: print area
5:stop
Algorithm 4:
Write an algorithm to find the area of circle
Sol:
Step 1: start
2: read radius "r";
3: Area <-(22/7.0)*r*r (or) 3.14 (r*r*)
4: print area;
5: stop
PPS(R18) UNI T 1 P a g e | 13
Algorithm 5:
Write an algorithm to find the given number is even or odd
Sol:
STEP1: start
2: Readnumber 'n'
3: if (n==0)then print "Number is neither even nor odd;
4: if (n%2==0) then print "Number is even ";
If (n%2==0) then print "Number is odd ";
5: stop;
STEP1: start
2: Readnumber 'n'
3: if (n==0)then
print "Number is neither even nor odd;
4: else
if (n%2==0) then
print "Number is even ";
else
print "Number is odd ";
5: stop;
Algorithm 6:
Write an algorithm to find whether a number is +ve or not
Sol:
STEP 1: Start
2: Read a number N
3: if (n=0) print “N is neither even nor odd”
else
if (n>0) then
Print “No. is Positive”
. else
Print “Number is
Negative” 5:
Stop
Algorithm 7:
Write a number to find the biggest of the two numbers
Sol:
STEP 1: start
2: read two numbers ;
3: if (a=b) then Print “both are
same” else
if (a>b) then
Print “a is bigger than
b”
else
PPS(R18) UNI T 1 Page|14
Algorithm 8:
Write an algorithm to swap given two numbers 10,20.
Sol:
STEP 1: start
2: read a <-10;
b<-20;
3: c<-a;
a<-b;
b<-c;
4: print a,b;
5: stop.
Algorithm 9:
Write an algorithm to swap two numbers without using third name(variable).
Sol:
STEP 1: start
2: read a<-10;
b<-20
3: a<-a+b;
b<-a-b;
a<-a-b;
4: print a,b;
5: stop;
PPS(R18) UNI T 1 Page|15
FLOW CHART:
A Flow chart is a diagrammatic representation of the given problem.
The different symbols used for a flow chart given below.
Reader
PPS(R18) UNI T 1 P a g e | 18
i). Repeat-Until
Repeat
Statements – 1
Until
Condition;
Statements – 2
while (condition)
statement-1
end while
statement-2
Statements – 2;
***
The difference between while and do while is do while statements will be executed at least
once without checking the condition.
PPS(R18) UNI T 1 P a g e | 19
In for loop, a particular variable will be initialized with a value, and then the condition will
be checked, if it is true, the body of the
statements will be executed.
After that, the initialized value will be updated,
and again control checks the condition, if it is
true, the process continues, else the control
comes out of the loop.
PRACTICE:
1) Draw a flowchart for finding the sum of „n‟ numbers starting from 1?
2) Write an algorithm to read five integers and find out if the values are in ascending order.
3) Draw a flow chart for displaying the grade of the marks of average of 4 subjects of a
student. Average Grade
>=75 A
>=60 &&<75 B
>=50 &&<60 C
<50 D
** Ensure that the user entered all of his marks in between 0 & 100 only.
4) Draw a flow chart to read ten integer values and print the sum of squares of the values.
5) Calculate the Expenditure per month of a sales person. Your algorithm should print either „dues‟
or „no dues. If the expenditure is below 5000/- then the balance(5000-exp) is his due. If the
expenditure is above Rs. 5000/- then no Due.
Hint: subtract exp from 5000 if bal is <5000 the print the due else print no dues
PPS(R18) UNI T 1 P a g e | 20
Ans:
Factorial of a Number N
PPS(R18) UNI T 1 Page|21
INTRODUCTION TO C PROGRAMMING
History of C Language
1958 ALGOL Too Abstract
ALGOL60 Too General
ALGOLW Also Too Abstract, & General
COBOL Commercial Purpose
PASCAL
FORTRAN Engineering & scientific Applications
1963 CPL Hard to learn& Difficult to implement.
1967 BCPL Martin Richards
Too specific Deals with specific Applications
1970 B AT & T Bell Labs
Also Too specific, only for specific requirements.
1972 C Dennis Ritchie
AT &T Bell
Right from UNIX, Windows and many Ant viruses etc developed using C.
Embedded devices, chip designing Industrial Automations products, signal
transmission.
Major Advantages
A combination of high-level Machine level program capabilities.
Introduction to C
C is a program language developed by "Dennis Ritchie" in 1972 at
AT&T Bell laboratories.
C is a high-level language.
It is also called Middle level language because it supports both
features of low-level andhigh-level languages.
C is a structured program language in which a program is written as
a set modules (or) blocks (or) functions.
PPS(R18) UNI T 1 Page|23
STUCTURE OF A C PROGRAMME
printf():
“printf()” is a predefined output function which is used to print the given text or the values on
the console(screen).
Syntax: printf (“Message to Print”);
printf (“Message or Format specifiers”, variable names);
scanf():
scanf() is a predefined input function which is used to take input from the keyboard.
C CHARACTER SET
The characters that can be used to form words numbers and expressions depend upon the
computer on which the program runs.
A set of characters is available that can be used for c.
The characters in c are grouped into following categories.
Alphabets A, B, C, D, E, F,...Y,Z
a, b, c, d, e, f,.....y, z
Digits 0, 1, 2, 3, 4,....9
special characters ~ ! @ # % ^ & * ( ) _ - + = $ 1 \ { } [ ] : ; '' <> , . ? /
White spaces Blanks space
Horizontal tab, carriage return, New line, form feed (\ f), Page Break
C tokens:
In a c program, the smallest individual units are called c tokens. C has six types of
tokens. Every c program must be written using these tokens and some syntax of c.
PPS(R18) UNI T 1 Page|24
key
words
identifiers
constants
strings
special symbols
operators
(every c words must be either a keywords or an identifier)
Keywords:
key words are pre defined names which have fixed meaning in c language. Key words must
be written only in lower case letters. There are 32 key words available in c language.c99
adds some more key words.
Identifiers:
Identifiers are the names given to the variable, functions,arrays. These are user defined
names and consist of sequence of letters and digits with a letter as a first character.
Both upper case and Lower case letters permitted.
The underscore character is also permitted.
Constants:
Constants in C are fixed values that do not change during the execution of a c program.
INTEGER CONSTANTS
An integer constant refers to a sequence of digits. There are three types of integer
constants.
Decimal integers
Octal integers
Hexadecimal integers
Decimal integers:
It consists of digits 0 to 9 preceded by an optional –ve or +ve sign.
Ex: 123, -423, 789, +65433.
Embedded spaces, commas, non-digit characters are permitted between
digits. Ex: 15, 750, 20,000/-, $ 1000 are not valid.
Octal integers:
It consists of any combination of digits from 0 through with a leading 0
(zero) Ex: octal integers are 037, 0, 0435
Hexadecimal integers:
PPS(R18) UNI T 1 P a g e | 26
It consists of digits from 0 to 9 and alphabet from A to Z, where, A=10, B=11, C=12, D=13,
E=14, F=15. They are preceded by OX or 0X
Ex: 0X2, 0X9D, 0XBC, 0X.
REAL CONSTANTS
Real numbers consists of digits from 0 to 9 with a decimal
point. Ex: 1.24, +.24, 7.0 valid.
A real number may also be expressed in scientific (or) exponential
notation. This is of the form “mantissa e exponent“
Mantissa can be integer (or) a real number.
Exponent should be an integer with an optional sign + ve or – ve.
Ex: 0.65e4, 12e-2, 1.5e+5, 3.76 E3, -1.2E-1
Space is not allowed in the syntax.
STRING CONSTANTS
A string is a sequence of chars placed inside the double quotes.
Ex: “ program “ , “ a ” , “ 123 ”, “ 877 ”, “ Hello! “.
„X‟ != “X
a char constant ( „X‟ ) is not equivalent to the single cha string constant ( “X” ).
A single char string constant doesnot have an equivalent integer value, while a char
constant has an integer value.
PPS(R18) UNI T 1 P a g e | 27
Check %d %d %d “,99,099,x99
PPS(R18) UNI T 1 P a g e | 28
BACKSLASH CONSTANTS
The backslash constants are predefined which are to be used in o/p functions.
\n new line
\t horizontal line
\v vertical line
\a audible alert ( bell )
\r carriage return
\b back space
\f form feed
\‟ single quote
\” double quote
\? Question mark
\\ back slash
\O null
VARIABLE
A variable is a data name which is used to store a value.
SYNTAX:
datatype variable1, variable2…;
DATA TYPES
C language is rich in data types. Storage representations and machine instructions to
handle constants differ from machine to machine.
Data types represent the type of data stored in the variables.
ANSIC supports three classes of data types.
1. Primary (fundamental) data types.
2. Derived data type.
3. User-defined data type.
int
signed unsigned
int unsigned int
short int unsigned short int
long int unsigned long int
PPS(R18) UNI T 1 P a g e | 29
void
void is a key word in C used to not to return any thing by a function.
INTEGER TYPES
These are the numbers without a decimal point.
typedef:(typedefinition)
This keyword is used to give another narrate to the existing data type.
SYN: typedef datatype identifier;
Datatype represents the existing datatype.
Identifier refers to the 'NEW' name given to the existing
datatype. Example1:
typedef int marks;
here marks symbolizes int. This can later used to declare variable as.
marks m1,m2,m3;
Example2
typedef float units;
units u1,u2,u3;
enum:
This keyword is used to create our own datatypes.
Syntax to define a datatype.
enum identifier {value1,value2,value3,---------------};
The 1st part declares the datatype and specifies its possible values.
These values are also called "Enumerators".
The second part declares variables of this.
Now we can give values to these variables.
person1= married;
person2= divorced;
Note :- we can't use the values that aren't in the original
declaration. EXAMPLE:-
person1= Bachelor;--> error.
Internally the compiler treats the enumerators as integers from by zero by default.
We can define and declare the enumerated datatype in a single lineusing the following
Syntax
enum identifier
{value1,value2,---------}variable1,variable2,---------;
enum week
{sun,mon,tue}day1,day2;
=================================================================
/*PROGRAM TO ILLUSTRATE ABOUT THE TYPEDEF */
#include<stdio.h>
#include<conio.h>
main()
{
typedef int Marks;
Marks m1,m2,m3,total;
float percent;
printf("\n\n\tEnter the Marks for the following subjects:");
printf("\n\tCPDS:");
scanf("%d",&m1);
printf("\n\tM1:");
scanf("%d",&m2);
printf("\n\tMM:");
scanf("%d",&m3);
total=m1+m2+m3;
percent= (total/300.0)*100;
PPS(R18) UNI T 1 P a g e | 32
OPERATORS
An operator is a symbol that performs operations on operands(data variables).
In the expession c=a+b,
+ is called the "operator"
and a,b are called "operands". C is the result.
1. ARITHMETIC OPERATORS:
These operators are used to perform arithmetic calculations.
+ --> addition
- --> subtraction
* --> multiplication
/ --> division
% -->modulus (modular division)
The unary minus operator, in effect multiplies its single operand by -1, so that a no.
preceded by minus sign changes its sign.
The division operator gives quotient as the result.
The modulus operator gives reminder as the result.
Ex1: int a =14, b =4;
a-b=10 a+b= 18 a*b=56 a/b=3 (decimal part truncated)
a%b=2 (remind or)
2. RELATIONAL OPERATORS:
These operators are used to compare two operands they are
< Less than
> Greater than
<= less than or equal to
>= greater than or equal to
== Equal to
!= not equal to
Eg: a<b, a>b, a<=b, a>=b, a==b, a!=b
These expressions are also called "relational expressions".
The value of a relational expressions is either one or zero
If the specified relation is true, then it is, if it is false, the value is 0.
3. LOGICAL OPERATORS:
These operators are used to combinetwo or more relational expressions or conditions
together.
They are
&& logical AND
|| logical OR
! logical NOT
Ex:
1. (a>b)&& (a>c) ! (a>b)
2. if (age >55 && sal < 1000)
3. if (number <0 11 number >1000)
RELATIVE PRECEDENCE:
highest ! > >= < <= == != && lowest
4. CONDITIONAL OPERATORS:
A ternary operator pair ?: is used to check the condition.
SYNTAX:
condition? statement 1 : statement 2;
The condition will be evaluated first if it is true (non zero), then stt-1 is evaluated and
becomes the value of stt-1 if condition is false, stt-2 is evaluated and its value becomes the
value of stt-2.
Only one of the statements (1 or 2) will be evaluated
Ex: Big= (a>b) ? a:b ;
a>b ? print f ("a is Big"): print f ("b is Big")
Ex: y=1.5x+3 if x<=2
2x+5 if x>2
y= (x>2)? (2x+5):(1.5x+3);
The operators may also be nested for evaluating more complex assignments.
=================================================================
PPS(R18) UNI T 1 P a g e | 35
7.BITWISE OPERATORS
These operators are for manipulation of data at bit level.
The operators are
OPERATOR PURPOSE
& Bitwise AND
l Bitwise OR
^ Bitwise exclusive OR
(both are same ---> 0 Both are different ---> 1)
~ complement
<< left shift
>> right shift
=================================================================
/*PROGRAM TO CHECK THE BITWISE OPERATORS*/
main()
{
int x,y;
printf("\n\n\t\tPROGRAM TO CHECK THE OPERATIONS ON BITWISE
OPERATORS");
printf("\n\n\tENTER TWO NUMBERS X & Y: "); scanf("%d
%d",&x,&y);
8.SPECIAL OPERATORS
C supports some special operators as sizeof (), comma, pointer operators (& and *) and
member selection operators (. and -->).
Comma operator can be used to link the related expressions together into a single line
Ex1: int a , b=10 , c ;
Ex2: t=a , a=b , b=t ;
Ex3: (c=ca=10 , b=is , a+b) ;
sizeof operator is used to display the size of datatype
syntax: sizeof (datatype);
Ex: sizeof(float)
print f ("%d",sizeof (float) ) ;
=================================================================
/*PROGAM TO ILLUSTRATE ABOUT THE SIZE OF DATA TYPES */
int main()
{
int i;
float j;
char ch;
printf("\n\nSize of i= %dBytes\tSize of int= %dBytes",sizeof(i),sizeof(int));
printf("\n\nSize of j= %dBytes\t Size of float= %dBytes",sizeof(j),sizeof(float));
printf("\n\nSize of ch= %dBytes\t Size of char= %dBytes",sizeof(ch),sizeof(char));
printf("\n\nSize of float=%dBytes",sizeof(float));
printf("\n\nSize of double=%dBytes",sizeof(double));
printf("\n\nSize of long double=%dBytes",sizeof(long double));
return 0;
}
O/P:
Size of i= 2Bytes Size of int= 2Bytes
Size of j= 4Bytes Size of float= 4Bytes
Size of ch= 1Bytes Size of char=
1Bytes Size of float=4Bytes
Size of double=8Bytes
Size of long double=10Bytes
=================================================================
FORMAT SPECIFIERS:
int %d octal %o (o is alphabet not zero)
float %f . hexadecimal %ox
char %c
string %s
short int %d or % hd
long int % ld
double %f long double % ld
PPS(R18) UNI T 1 P a g e | 40
SUMMARY OF C OPERATORS:
=================================================================
OPERATOR DESCRIPTION ASSOCIATIVITY RANK
=================================================================
( ) functional call L -> R 1
[] array element reference
=================================================================
+ unary plus R -> L 2
- unary minus
++ increment
-- decrement
! logical NOT
~ 1‟s compliment
* pointer reference
& address of
size of size of an object
(type) typecast(conversation)
=================================================================
* multiplication L -> R 3
/ division
% modular division
=================================================================
+ addition L -> R 4
- subtraction
=================================================================
<< left shift L -> R 5
>> right shift
=================================================================
< Lessthan L -> R 6
<= Lessthan or equal to
> Greater that
>= Greater than or equal to
=================================================================
PPS(R18) UNI T 1 Page|41
=================================================================
== equality L -> R 7
!= inequality
=================================================================
& bitwise AND L -> R 8
=================================================================
^ bit wise XOR L -> R 9
=================================================================
! bitwise OR L -> R 10
=================================================================
&& Logical AND L -> R 11
=================================================================
!! logical OR L -> R 12
=================================================================
? : conditional expression R -> L 13
=================================================================
=
*= /= %=
+= -= &= assignment operators R -> L 14
^=
=================================================================
, comma L -> R 15
=================================================================
Note: Follow the Class notes for more problems in the Precedence and Associativity.
=================================================================
/*PROGRAM TO ILLUSTRATE ABOUT THE OPERATOR'S PRECEDENCE &
ASSOCIATIVITY */
#include<stdio.h>
int main()
{
int i,j,a,b,c;
i=(2*3/4+4/4+8-2+5/8);
printf("\n\ni=%d",i);
a=3; b=5; c=8;
a+=b*=c-5; printf("\n\
na=%d",a); a=3; b=4;
c=5;
j=--a*(3+b)/2-c++*b;
printf("\n\nj=%d",j);
}
i=8
a=18
j=-13
=====================================================================
ILLUSTRATION ABOUT
PPS(R18) UNI T 1 Page|42
1. Null Statement:
An empty statement ending with a semicolon “;” is called a Null
statement. Ex: ;
This statement is used when we require a statement but no action is performed.
2. Expression statement:
An expression ending with ; is called “Expression
statement”. Ex: c=a+b;
area=(1.0/2)*b*h; a=99;
3. Compound statement:
It is a unit/block of code that combines a group of
statements. Ex: {
---
--- // body statements;
---
}
4. Return Statement:
A return statement terminates a
function; Syntax: return value;
5. Selection Statements:
It contains a decision making to see whether a condition is satisfied or not and to execute the
respective statements.
The different selection statements are
i. if statement
ii. switch statement
iii. conditional statement
iv. goto statement
The above are all called the “Decision making statements” or “Control Statements”.
i. if statement:
The different forms of if statements are
1. simple if
2. if …else
3. nested if
4. else if
PPS(R18) UNI T 1 P a g e | 46
If the condition is true, the statements block-1 will be executed and the control goes
to next statements.
If the condition is false, then the control directly goes to next to the if block with
out executing the inside block of statements-.
Ex: Write a program to find the biggest of the two numbers using the simple if.
=====================================================================
/* Program to find the biggest of two numbers using simple if*/
#include<stdio.h>
int main()
{
int a,b;
printf(“\nEnter two Integers:”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“\n%d is bigger than %d”,a,b);
if(a<b)
printf(“\n%d is bigger than %d”,b,a);
if(a==b)
printf(“\n%d is equal to %d”,a,b);
}
Syntax:
if(condition)
{
True Statements Block;
}
else
{
False Statements Block
}
Statements Block – 3;
PPS(R18) UNI T 1 P a g e | 47
If the condition is true, then true stt block will be executed and the control goes to
the stat-3 block;
If the condition is false, then false statements block will be executed and then
the control goes to the Stat-3 block.
In either case only one of the true/false block statements will be executed.
if only one statement is there in true/false block then { } are optional for blocks.
Ex:
/*PROGRAM TO FIND THE BIGGEST OF TWO NO.S USING IF…ELSE*/
#include<stdio.h>
int main()
{
int a,b;
printf(“\nEnter two Integers:”);
scanf(“%d%d”,&a,&b);
if(a>=b)
printf(“\n%d is bigger than %d”,a,b);
else
printf(“\n%d is bigger than %d”,b,a);
}
Enter two Integers: 5 6
6 is bigger than 5
=========================================================================
c). nested if :
Syntax:
if(condition-1)
{
if(condition-2)
{
if(condition-3)
{
Statement-1;
}
}
}
In the nested if block first the condition-1 will be checked, if it
is true, the control again checks the condition-2, if it is true,
the control again checks the condition-3,if it is also true, then
stt-1 will be executed. if either of the conditions is false then
stt-1 will not be executed.
after that control comes out of the block and then executes any other statements
=========================================================================
PPS(R18) UNI T 1 P a g e | 48
Syn:
switch(variable) //value
{
case value-1:
stt-1;
case value-2:
stt-2;
:
:
default:
Default stt;
}
PPS(R18) UNI T 1 Page|52
case 3:
printf("\n\n%f * %f = %f",a,b,a*b);
break;
case 4:
if(b!=0)
printf("\n\n%f / %f = %f",a,b,a/b);
else
printf("\nDivision is not possible with zero");
break;
default :
printf("\n Enter correct option");
scanf("%d",&i);
}
return 0;
}
Enter any no.s a & b: 45 15
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
Enter your option: 4
45.000000 / 15.000000 = 3.000000
PPS(R18) UNI T 1 Page|54
goto statement
C supports go to statement branch unconditionally from one point to another point in the
program. Although it may not be essential to use the go to statement in a highly
structured language like C, there may be occasions to use goto.
goto breaks the normal sequential execution of the program
. syntax:
goto label;
…….
…….
label:
{
stts;
}
label:
{
stts;
}
…….
…….
goto label;
/*PROGRAM TO ILLUSTRATE ABOUT GOTO STATEMENT*/
int main()
{
int i;
for(i=1;i<=5;i++)
{
printf("\ti=%d",i);
if(i==5)
goto VITS;
}
printf("\nThis stt won't be executed");
VITS:
printf("\n This is a labeled Stt for goto");
printf("\n Completed");
}
O/P:
i=1 i=2 i=3 i=4 i=5
PPS(R18) UNI T 1 P a g e | 55
1. While statement
The test condition is evaluated and if condition is
true ,then the body of the loop is executed .After the
execution of the body ,the test-condition are again
evaluated ,and if it true ,the body is executed once again.
This process continues till the test condition becomes
false and the control is transformed out of the loop.
The body of the loop may have one or more
statements .the braces are needed only if the body
contains two more statements
=====================================================================
/*program to find the average of n numbers using while*/
#include<stdio.h>
main()
{
int i=1,n;
float x,sum=0; printf("\
n\n\tEnter n: ");
PPS(R18) UNI T 1 P a g e | 57
scanf("%d",&n);
while(i<=n)
{
printf("\n\n\t Enter x%d: ",i);
scanf("%f",&x);
sum+=x;
i++;
}
printf("\n\n\tThe sum is: %2.3f",sum);
printf("\n\tThe Avg is: %2.4f ",sum/n);
}
O/P: Enter n: 4
Enter x1: 1.2 Enter x2: 3.4 Enter x3: 5.6 Enter x4: 7.8
The sum is: 18.000
The Avg is: 4.5000
=====================================================================
The do-while statement:
The do stt is an exit control loop.
The do stt first executes the body of the loop, then it
will check the condition given in while stt.
If the condition in while stt is true, then once again the body of
the loop will be executed.
This process will continue till the condition becomes false,
then the loop will be terminated and control goes to the
stt that appears immediately after the while stt.
====================================================================
Ex: Program to calculate the average of n numbers.
/*program to find the average of n numbers using do-while*/
#include<stdio.h>
main()
{
int i=1,n;
float x,sum=0; printf("\
n\n\tEnter n: ");
scanf("%d",&n);
do
{
printf("\n\n\t Enter x%d: ",i);
scanf("%f",&x);
PPS(R18) UNI T 1 P a g e | 58
sum+=x;
i++;
}while(i<=n);
printf("\n\n\tThe sum is: %2.3f",sum);
printf("\n\tThe Average is: %2.4f ",sum/n);
}
o/p: Enter n: 5
Enter x1: 1.2 Enter x2: 2.3 Enter x3: 3.4 Enter x4: 4.5
Enter x5: 5.6
The sum is: 17.000
The Average is: 3.4000
=====================================================================
for statement:
The for loop is another entry control loop
format:
The for loop follows three things
1. Initialization
2. Condition checking
3. Increasing/Decrementing
1. Initialization
Sets the loop counter to an initial value.
2. Condition checking:
The value of the control variable is tested using the test condition. If the condition is
true the body of the loop will be executed, else the loop will be terminated and execution
continues with the stt that immediately follows the loop.
3. Increment/Decrement:
After the body of the loop executed, the value of the control variable either
incremented or decremented and again the condition will be tested.
=====================================================================
/*PROGRAM TO PRINT THE FACTORIAL OF A NO.*/
#include<stdio.h>
int main()
{ int i,n,fact=1;
printf("\nEnter No: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact*=i; printf("\n
%d! = %d",n,fact);
}
Enter No: 10
10! = 24320
=================================================================
PPS(R18) UNI T 1 P a g e | 59
PERFECT NUMBER:
a perfect number is a positive integer that is the sum of its proper positive divisors, that is,
the sum of the positive divisors excluding the number itself. Equivalently, a perfect number is a
number that is half the sum of all of its positive divisors (including itself), or s(n) = 2n.
The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and
1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors:
(1 + 2 + 3 + 6 ) / 2 = 6.
The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers
496 and 8128 (sequence A000396 in OEIS).
These first four perfect numbers were the only ones known to early Greek mathematics.
Here are the first few perfect numbers:
6,
28,
496,
8128,
33550336,
8589869056,
====================================================================================
PALINDROMIC NUMBER:
A palindrome number or numeral palindrome is a 'symmetrical' number like 16461 that remains
the same when its digits are reversed. The term palindromic is derived from palindrome, which
refers to a word like rotor that remains unchanged under reversal of its letters. The first
palindrome numbers (in decimal) are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171,
181, 191, … (sequence A002113 in OEIS).
Palindromic numbers receive most attention in the realm of recreational mathematics. A typical
problem asks for numbers that possess a certain property and are palindromic. For instance,
The palindromic primes are 2, 3, 5, 7, 11, 101, 131, 151, (A002385) etc.
The palindromic square numbers are 0, 1, 4, 9, 121, 484, 676, 10201, 12321,(A002779) etc.
====================================================================================
ARMSTRONG NUMBER:
An Armstrong number is a number that is equal to the sum of each of the individual digits that
make up the number raised to the power of the number of digits the number has.
Armstrong numberAlso known as narcissistic numbers, Armstrong numbers are the sum of their
own digits to the power of the number of digits. As that is a slightly brief wording,
Let me give an example:
153 is an Armstrong number because 153 = 1^3 + 5^3 + 3^3.
370 is an Armstrong number because 370 = 3^3 + 7^3 + 0^3.
1634 is an Armstrong number because 1634 = 1^4 + 6^4 + 3^4 + 4^4.
PPS(R18) UNI T 1 P a g e | 60
SECTION-2
PART A
Write minimal C expressions for the following:
4 3 2
A1) 2x + 3x - 4x + 7x -10
3 2
A2) x - 4x + 7x -12
4 3 2
A3) 5a +3a -4a +6a+12
A4) i). abcbccaab−− ii). 1235abbc+ iii). 2234abccab--
A5) Digit at the 10's place of the given positive integer x
(for example, digit at the 10's place in 3458 is 5)
A6) Maximum of the values of three variables a, b and c
A7) Minimum of the values of three variables a, b and c.
A8) If a > b then the value of expression is a-b, otherwise b-a
A9) Add x to y, and then decrement x
A10) Increment x, and then add to z
A11) True if the given positive integer x is odd, false otherwise
A12) True if the given positive integer x is even and is also a multiple of 7, false
Otherwise.
A13) True if the given character variable c represents a numeral (that is '0'...'9'),
false otherwise.
A14) True if the given positive integer x is a multiple of both 17 and 11, false
otherwise.
A15) True if 25 > a ≥ 10, false otherwise
A16) True if 5 <x < 10, otherwise false
A17) True if x is exactly divisible by 5 but not divisible by 3, otherwise
false. A18) True if 5 <= a <= 10, false otherwise
A19) True if the value of character variable c is in uppercase, otherwise false
A20) Absolute value of (a-b)
A21) Absolute value of variable x.
A22) True if x/y >3 without zero divide, false otherwise
A23) If x<y then -1 else if x = = y then 0 else 1(use ternary operator)
A24) Subtract x from y and then increment x.
A25) Remainder when unsigned integer variable x is divided by 8, using bitwise
operators.
A26) Divide the integer variable x by 16 using bit-wise operators
NOTE:
FOR THE ABOVE QUESTIONS IN SECTION A, NO NEED TO WRITE THE
ENTIRE PROGRAM EVEN IN THE EXAM JUST WRITE THE LOGIC WITH C
SYNTAX.
PPS(R18) UNI T 1 P a g e | 62
PART B
B1) What is the difference between the following c-words?
i) amount and “amount” ii) 200 and 200.0
B2) What is the difference between the following C-words?
i) 253 and 0253 ii) „r‟ and „\r‟
B3) What is the difference between the following C-words?
i) count and int ii) 526 and “526”
B4) What is the difference between the following C-words?
i) 5 and „5‟ ii) if and ++
PART C
C1) An integer is divisible by 9 if the sum of its digit is also divisible by 9. Write
a C program that prompts the user to input an integer. The program should
then output the number and a message stating whether the number is divisible
by 9.
(June2012)
C2) What is type conversion? Explain briefly about implicit and explicit type of
conversions. (June2012)
C3) Write C- language program that reads a number from input and determine
whether it is a prime or not.
C4) Write a complete C Program to print all the prime numbers between 1 and n.
Where „n‟ is the value supplied by the user.
C5) Write C-program for determining whether the given integer at input is perfect
number or not.
C6) Write a C-program that reads the given n observations at input and computes
average of n observations and find the number of observations above average
value
C7) Write a complete C program that reads a value in the range 1 to 12 and print
the name of that month and the next month: Print error for any other input
value. (For example, print “May followed by June” if the input is 5. Note that
December is followed by January).
C8) Write C-program that reverses the decimal digits of integer value at input.
For example, for input 5379, the program need to output 9735
C9) Write a complete C Program to read ten integers and find:
(i) The number of even integers and their sum, and
(ii) The number of odd integers and their sum.
C10) Explain the structure of a C program?
C11) List the basic data types, their sizes and range of values supported by „C‟
language?
PPS(R18) UNI T 1 P a g e | 63
Note: The students have to submit the above assignment within 10 days.
* * *
PPS(R18) UNI T 1 P a g e | 64
LAB2
PROGRAM TO CALCULATE THE GROSS SALARY OF AN EMPLOYEE AS
FOLLOWS
BASIC SALARY DA HRA CONVEYENCE
>=5000 110% 20% 500
>=3000 &<5000 100% 15% 400
<3000 85% 10% 300
PROGRAM TO FIND THE NATURE AND ROOTS OF A QUADRATIC EQUATION
2
ax +bx+c=0.
LAB 3
PROGRAM TO FIND THE SUM OF THE EVEN AND ODD DIGITS OF THE GIVEN
NUMBER
PROGRAM TO PRINT THE FIRST N FIBONACCI NUMBERS
PROGRAM TO FIND THE GCD/HCF and LCM OF TWO NUMBERS
LAB 4
PROGRAM TO CHECK WHETHER THE NO. IS PRIME OR NOT
PROGRAM TO CHECK WHETHER THE NO.IS ARMSTRONG OR NOT
( EXAMPLE ARMSTRONG NO.S
0 1 2 3 ... 9 153 370 371 407 1634 8208 9474 54748 )
PROGRAM TO CHECK WHETHER THE NO.IS PERFECT OR NOT
(EXAMPLE PERFECT NO.S 6 28 496 8128).
LAB 5
PROGRAM TO PRINT NUMBERS IN THE FORMAT
1
2 3
45 6
7 8 9 10
PROGRAM TO FIND THE EXP X
PROGRAM TO FIND THE SUM OF THE SINE SERIES
PPS(R18) UNI T 1 P a g e | 65
NOTE:
Flow chart is mandatory for each and every program in the record; however for
LAB 1 flow charts are not required.
Write the dates, Roll No and page numbers on each and every record sheet.
Draw the flow chart and write the output of the program on the left hand side
and write the program on the right hand side of the record sheet.
Draw the flow charts neatly with pencil only.
Write index also with the dates in which you have done the programs.(later)
You can enquire for the old records for the reference only from your lab
programmer, otherwise your faculty.
==================================================================
PPS(R18) UNI T 1 P a g e | 66
I BTECH-CPDS-MID1-UNIT1:: 2009-10
1. Which of the following is not an input device [ ]
a) Plotter b) scanner c) keyboard d) mouse
2. How many times the body of the following loop executed?
x=5; y=50; while (y! =0) {y/=x ;} [ ]
a) 4 b) 1 c) 3 d) 2
3. The parallelogram is used to represent type of statements in flow charts [ ]
a) input/output b) Functions c) decision d) processing
4. Which of the following is not a translator program [ ]
a) Linker b) assembler c) interpreter d) compiler
5. Which of following loop executes the body of the loop at least once [ ]
a) while b) for c)do..while d) compiler
6. The hardware along with the read-only software that resides on this hardware
is combinly called as
7. In flowcharts, decisions are represented by using symbol
8. acts as an interface between the computer hardware and user of the computer.
9. In menu driven programs loop statement is used
KEY
1.B 2.C 3.A 4.B 5.C 6.ROM 7. ROMBOS 8. Operating System
9. SWITCH
I BTECH-CPDS-MID1-UNIT1::2010-11
1. What will be sum of the binary numbers 1111 and 1101 [ ]
a) 101000 b) 100010 c) 11110 d) 11100
2. Which one of the following is known as the language of the computer [ ]
a) Programming Language b) High Level Language
c) Machine Language d) Assembly Language
3. What type of errors are checked during compilation [ ]
a) Logical errors b) divide by zero errors c) run-time errors d) syntax errors
4. Which one of the following numeric value is used to represent base of the binary number [ ]
a) 8 b) 10 c) 2 d) 16
5. What will be the binary value of B [ ]
a) 1001 b) 1011 c) 1100 d) 1101
6. Which of the following is not a translator program [ ]
a) Linker b) Assembler c) Compiler d) Interpreter
7. The process of repeating a group of statements in an algorithm is known as
8. is very similar to the while loop except that the test occurs at the end of the loop
body.
9. ALU stands for ________________
10. translates the high level language source code into low-level language.
11. The output of the assembler in the form of sequence of 0’s and 1’s is called _______________________
KEY
2.C 3.D 4.C 5.B 6.A 7.LOOP 8.DO-WHILE 9.Arithmatic
Logic Unit 10. Translator 11.Binary Code or Machine Code.
PPS(R18) UNI T 1 P a g e | 67
I BTECH-CPDS-MID1-UNIT1::2011-12
1. Which one of the following is not a translator program [ ]
(A)Assembler (B) Interpreter (C) Linker (D) Compiler
2. What will be sum of the binary numbers 1111 and 11001 [ ]
(A) 111100 (B) 100010 (C) 11110 (D) 101000
3. Which one of the following is known as the language of the computer [ ]
(A) Programming language (B) Machine language
(C) High level language (D) Assembly level language
4. is used to compile your c program
5. The while loop repeats a statement until the test at the top proves _________________
6. The transfers control to a statement within its body
7. The process of repeating a group of statements in an algorithm is known as
8. Extend the term CPU
9. Monitor, keyboard, mouse and printers are
KEY
1.C 2.D 3.B 4. Compiler 5. Fails6. Conditional Statement Of A Loop
7. LOOP 8. Central Processing Unit 9. Input and Output Devices
I BTECH-CPDS-MID1-UNIT1 ::2009-10
1. Which of the following is the correct order of operators for the evaluation for the expression
z = x + y * z/4 % 2-1 [ ]
a)* / % + - = b) - % ? * + = c) / * % - + = d) * / % - + =
2. How many times the body of the following loop executed? x=5; y=50; while(y! = 0) {y/=x;}[ ]
a) 4 b) 1 c) 3 d) 2
3. Which of the following statement is syntactically correct [ ]
a) printf(“%d”, &a); b) scanf(“%d”, a); c) scanf(“%d”, #a); d) scanf(“%d”, &a);
4. Which of the following is the correct syntax of for loop [ ]
a) for(i=o, i<10, i++) b) for(i=0; i++; i<=10)
c) for(i=0; i<10; i++) d) for(i=0, i++, i<=10)
5. Which of the following loop executes the body of the loop at least once [ ]
a) while b) for c) do..while d) all of the above
6. Which of the following is used inside a loop to terminate the current iteration and start with the
next generation [ ]
a) break b) continue c) goto d) return
Fill in the Blanks
7. is the largest value that an unsigned short int type varible can store
8. logical operator is true only when both operands are true
9. The order of evaluation can be changed by using in an expression
10. A for loop with no test condition is called as loop
11. Execution of a C program begins at
12. loop in C is both counter controlled and pretest loop
13. In menu driven programs statement is used
KEY
1). A 2). C 3). D 4). C 5). C 6). B
7). 255 8). && 9). ( ) 10). Infinite 11). Main 12). For
13). Switch
=====================================================================
PPS(R18) UNI T 1 P a g e | 68
I BTECH-CPDS-MID1-UNIT1::2010-11
Choose the correct alternative:
1. What will be sum of the binary numbers 1111 and 1101 [ ]
a) 101000 b) 100010 c) 11110 d) 11100
2. Consider the following program segment. i=6720; j=4; [ ]
While ((i%j)==0)
{ i=i/j; j=j+1;
} then, On termination j will have the value
a) 4 b) 8 c) 9 d) 6720
3. Which of the following is/are syntactically correct [ ]
a)for( ); b)for(;); c)for(,); d)for(;;);
4. #include<stdio.h> [ ]
main( )
{
int i=1,j=2;
Switch(i)
{
Case 1:
printf(“GOOD”); break;
case j:
printf(“BAD”); break;
}
}
a) GOOD b) BAD c) GOOD BAD d)Compiler Error
5. What type of errors are checked during compilation [ ]
a) Logical errors b) divide by zero errors c) run-time errors d) syntax errors
6. Which one of the following numeric value is used to represent base of the binary number[ ]
a) 8 b) 10 c) 2 d) 16
7. What will be the binary value of B [ ]
a) 1001 b) 1011 c) 1100 d) 1101
8. The program fragment [ ]
int a=5,b=2;
printf(“%d”,a+++++b);
a) prints 7 b) prints 8 c) prints 9 d) none of the above
KEY
1)D 2) C 3)D 4) D 5) D 6) C 7) B 8) D
9) preprocessor 10) keywords 11) precedence 12) do-while 13) 10
14) associativity 15) binary code or machine code
====================================================================
I BTECH-CPDS-MID1-UNIT1 ::2011-12
Choose the correct alternative
1. Which of the following is syntactically correct [ ]
(A) for(;); (B)for(); (C) for(,); (D) for(;;);
2. Find out the output for the following [ ]
#include<stdio.h>
main() { int c=--2; printf(“c=%d”,c); }
(A) -2 (B) 0 (C) 2 (D) None
3. Identify the result [ ]
vdoid main()
{ int i=5;
Printf(“%d”,i+++++i);}
(A) 5 (B) 6 (C) 10 (D) Compiler error
4. What will be the ASCll Octal value of A [ ]
(A) 100 (B) 101 (C)110 (D) 111
5. a<<1 is equal to [ ]
(A) multiplying by 2 (B) dividing by 2 (C) added 2 (D) None
6. Consider the following and find the output [ ]
Main()
{ int a=0;int b=30;char x=1;
If (a,b,x)
Printf(“Hello”);
}
(A) Compiler error (B) abxHello (C) Hello (D)None
7. What will be sum of the binary numbers 1111 and 11001 [ ]
(A) 111100 (B) 100010 (C) 11110 (D) 101000
8. Find the output [ ]
int main()
{char a[]=”12345\0”;
int i=strlen(a);
printf(“here in 3 %d\n”,++i);}
(A) here in 3 (B) here in 3 6 (C) 6 (D) 3
KEY
1). D 2). D 3). D4) .B5).A6). C7). D 8).B 9). 1byte 10). fails 11). Loop
12). goto 13). loop
* * * End of Unit1 * * *