Professional Documents
Culture Documents
Using C Language
ELTP-CAMPUS
EXCELLENT CAMPUS PLACEMENTS 1 ELTP-GROUP
Problem Solving & Programming Fundamentals using C
2 Introduction
2. I t d ti to t C Language
L
3. Data Type
yp
5. Type Conversion
6 Operator
6. O t
7. Control Statements
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 2
Problem Solving using flowchart
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 3
Representation of Computation
Using Flowchart
A fl
flowchart
h t is
i a diagrammatic
di ti representation
t ti off computation
t ti
A flowchart is an organized
g combination of shapes,
p , lines and
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 4
Flowcharting Symbols
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 5
Basic Program Control
1 Sequential
1.
2. Selection
3. Iteration
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 6
Sequential Control
• In sequence
• One-after-the-other
Statement 1
Statement 2
Statement 3
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 7
Selection Control
Statement 1
Decision
Statement 2a Statement 2b
Statement 3
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 8
Iteration(Loop) Control
1.
Initialization initialization
of loop index
of loop index
Modify
y Loop
p 4. Update of
Index loop index
T
cond_expr
2. loop F
condition
Loop body
(S) 3. Task
Repeat
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 9
Example-1
Draw a flow chart to sum of given two integer numbers
Start
Get a
Input
Get b
Sum = a + b
Output
p Process
Put Sum
End
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 10
Example-2
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 11
Draw a flowchart to check whether a year is Leap year or not?
Solution Start
Get year
If (year % 400 = = 0) OR F
( year % 100 ! = 0 AND year % 4 = = 0)
End
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 12
Example-3
Draw a flowchart to sum of given N integer numbers
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 13
Draw a flowchart to sum of given N integer numbers
Start
Get N
I=1 S
Sum =0 loop
(initialization I=1 condition
of loop
index))
If
F
I<=N
? Put Sum
T
Modification
Sum = Sum + temp
of loop
index
Loop
I=I+1
Body
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 14
General C Program Structure
Pre-Processor Directives
functions declarations
void main()
void main()
Entry point for
{ /* start of main function*/ program execution
printf(“Hello World”);
Each instruction in a C
} /* end of main function*/ program terminates with a
semicolon (;)
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 16
Example-1
Draw a flow chart to sum of given two integer numbers
Start
Get a
Input
Get b
Sum = a + b
Output
p Process
Put Sum
End
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 17
C program to sum of given two integer numbers.
#include <stdio.h> /* Pre-processor
p command */
void main(){ /* Start execution of program */
int a,
a b,
b sum;/
sum;/* Declaration of local variable *//
scanf(“%d“, &b);
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 19
Home Assignment
Draw a flowchart to Sum of the following series of N terms:-
5 20 + 21 + 22 + 23 …………..
5.
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 20
Home Assignment
9. Draw a flowchart to check whether a number is perfect
square or not
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 21
Home Assignment
13 D
13. Draw a fl
flowchart
h t tto print
i t N rows off th
the ffollowing
ll i pattern:
tt
1
12
123
1234
12345
14. Draw a flowchart to print N rows of the following pattern:
1
22
333
4444
55555
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 22
Home Assignment
12345
1234
123
12
1
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 23
Introduction of Function
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 24
What is a Function?
• A sub
b program th
thatt does
d a specific
ifi ttask.
k
Function-1
u ct o in need
eed o
of service
se ce
from function 2
Function 1 Function 2
{ Tasks } { Tasks }
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 25
Classification of Functions
1. Library functions
Ex: main()
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 26
Elements of a Function
Function Definition :
- Implementing
p g the function or writing
g the task of the function.
- To utilize a function’s
function s service we have to invoke( call ) the
function.
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 27
Example – Function Terminologies
void
id display()
di l () ; Function Prototype
void main() {
di l ()
display(); Calling Function Definition
void display() {
printf(“Hello World”); Function Call Statement
}
Called Function
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 28
C program structure
{ Function Definition of
main() function
printf(“Hello World”);
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 29
Phases of C/C++ Programs:
Editor
Preprocessor
Compiler
Object File
Linker Libraries
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 30
C Files (Source Code) Header Files
Phases of
C/C P
C/C++Programs prog.c help.c strtest.c stdio.h func.h
Text Editor
C COMPILER
Pre-Processor
COMPILER
Windows: cl.exe
Intermediate UNIX: cc
C d
Code LINUX: gcc
Software Developer
Compiler
1011 101101
0101
Object Files 010101 11
101
101010
011101 10101 010
110 1010101 101
prog.obj help.obj strtest.obj 10011 111
Runtime
Libraries
LINKER:
Windows: link.exe Linker
UNIX: ld
LINUX: ld
6. Execute CPU
Memory
CPU takes each
instruction and
executes it, possibly
storing
sto g new
e data
.. values as the program
..
ELTP-CAMPUS ..
executes.
ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 32
Data Types
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 33
Data Types
– Range of data
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 34
Data Types(Cont’d)
D t ttypes
Data
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 35
Data Types(Cont’d)
C supports the
h following
f ll i P i i i Data
Primitive D Types:
T
– int – for storing whole numbers
– float
– Double
Note: float and double data types are used to store fractional
values and also Integer.
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 36
Data Types (Continued…)
• Modifiers alter the meaning of the base type to more precisely fit a
specific need.
• All modifiers short, long, signed and unsigned can be used along
with int data type.
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 37
Literals
• Literals refer to fixed values that the program does not alter.
Ch
Character
t lit
literals
l are enclosed
l d iin single
i l quotes
t
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 39
Variable Names in C programming language
• A variable name can either begin with an alphabet or
underscore
• No other special character except underscore is allowed
• Variable names are case sensitive
• Keywords cannot be used as variable names
amount_in$
$
Which of these
2many variable names
if are valid ones?
_iTotal
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 40
Declaration of variables
Syntax:
data-type [variable-name list] Initial
value of
data-type variable-name1 , variable-name2, … ;
variable
Ex: iNum
int iNum ;
Address
declares a variable off int data type off iNum
iN
in
The variables will contain some garbage value when they are declared.
memory
A variable can be initialized when it is declared
int iNum = 10; 10
22FA06
float fData = 2.3F ;
iNum
char cChoice = ‘Y’;
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 41
Input / Output Library Functions
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 42
Output Statement
Writes onto screen (Standard Output)
Syntax:
printf (“conversion specifier or string”, variables if any);
Ex:
printf(“Hello World”);
printf(“Hello World\n Welcome”);
printf(“%d”, a);
printf(“%f” x);
printf(“%f”,
printf(“%c”, y);
printf(“%d%d%f”,
printf( %d%d%f , j, k, a);
printf(“The sum of %d and %d is %d”, a, b, c);
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 43
Output Statement (Continued…)
Conversion Specifier
• These characters tell the input or output functions as to what
t
type off data
d t is
i being
b i read
d or written.
itt
– %c character
– %s string
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 44
Input statement
We can perform
W f the
th input
i t operation
ti in
i C using
i the
th function
f ti scanff
Syntax:
scanf (“conversion specifier(s)”, variable(s))
Format specifier’s in c:
%d- for int When user enters 10
%l - for long
g 0A
%f - for double and float datatype
&iNum 12FF78
%c - for characters returns the
%s - for strings address
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 46
Type Conversion (1 of 2)
• When variables off one type are mixed with variables off
another type, type conversion will occur.
• A variable of one type can receive the value of another
type.
• Type Conversion can be of two types:
■ Widening Conversion
■ Narrowing
N i C Conversion
i
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 47
Type Conversion (2 of 2)
Widening Conversion
int i = 10;
d bl d;
double d
d = i;
printf(“%d” d);
printf(“%d”, d)
Narrowing conversion
doubled = 10.856;
int i;
i = d;
printf(“%d”
printf( %d , i);
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 48
Arithmetic Conversions
char
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 49
What is the output of the following code snippet?
#include <stdio.h>
main() {
int i = 17;
char c = 'c'; /* ASCII value is 99 */
float sum;
sum = i + c;;
printf("Value of sum : %f\n", sum );
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 50
Type Casting
we have to explicitly specify the type cast operator
int i = 5;;
double d = 10.85;
d = i + ((int)) d;;
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 51
What is the output of the following code snippet?
#include<stdio.h>
void main(){
fl t avg;
float
total = i + j + k;
avg = total/3;
printf("%.2f",avg);
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 52
What is the output of the following code snippet?
#include<stdio.h>
void main(){
fl t avg;
float
total = i + j + k;
avg = total/3.0;
printf("%.2f",avg);
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 53
What is the output of the following code snippet?
void main() {
float a=12
a=12.25;
25;
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 54
Operators In C
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 55
Operators In C
O
Operators are predefined
d fi d symbol
b l iin llanguage to
perform specific operation on operands.
Classification of Operators:
(1) Based on the number of operands:
(1) Unary operators ++, --
(2) Binary operators +,-,*,/,%,==,<,>,>=,<=,!=,&&,||
(3) Ternary operators ? :
(2) Based on the operations performed:
(1) Arithmetic operators
(2) Relational operators
(3) Logical operators
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 56
What is the output of the following code snippet?
main() {
int a = 5, b = 3, c = 8, d = 7;
printf(“%d”, b + c / 2 - d * 4 % a);
}
How to find out order in which operators
p p
perform in expr?
p
Operator Table
Operators in order of precedence (highest to lowest).
Their associativity indicates in what order operators of
equal precedence in an expression are applied.
applied
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 57
Operator Description Associativity
() Parentheses left-to-right
[] Brackets (array subscript)
. Member selection via object
j name
-> Member selection via pointer
++ , -- Prefix increment/decrement right-to-left
+, - Unary plus/minus
!, ~ Logical
g negation/bitwise
g complement
p
(type) Cast (convert value to temporary value of type)
* Dereference
& Address (of operand)
sizeof Determine size in bytes on this implementation
* ,/, % Multiplication/division/modulus left-to-right
+ ,- Addition/subtraction left-to-right
<< , >> Bitwise shift left, Bitwise shift right left-to-right
< , <= Relational
R l ti l less
l than/less
th /l than
th or equall to
t l ft t i ht
left-to-right
> , >= Relational greater than/greater than or equal to
== , != Relational is equal to/is not equal to left-to-right
& Bitwise AND left-to-right
^ Bitwise exclusive OR left-to-right
| Bitwise inclusive OR left-to-right
&& Logical AND left-to-right
|| Logical OR left-to-right
?: Ternary conditional right-to-left
ELTP-CAMPUS
= Assignment ELTP-GROUP
right-to-left
EXCELLENT
, CAMPUS
Comma (separatePLACEMENTS
expressions)
58
left-to-right
Operator Description Associativity
() Parentheses left-to-right
++ , -- Prefix increment/decrement right to left
right-to-left
+, - Unary plus/minus
! Logical negation
(type) Cast (convert value to temporary value of type)
& Address (of operand)
sizeof Determine size in bytes on this implementation
* ,/, % Multiplication/division/modulus left-to-right
+ ,- Addition/subtraction left-to-right
< , <= R l ti
Relational
l less
l than/less
th /l than
th or equall to
t l ft t i ht
left-to-right
> , >= Relational greater than/greater than or equal to
== , != Relational is equal to/is not equal to left-to-right
&& Logical AND left-to-right
|| Logical OR left-to-right
?: Ternary conditional right-to-left
right to left
= Assignment
ELTP-CAMPUS right-to-left
ELTP-GROUP
EXCELLENT
, CAMPUS
Comma PLACEMENTS
(separate expressions) 59 left-to-right
What is the output of the following code snippet?
main()
int b = 3, c = 5,d = 4;
float g = 2.5;
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 60
What is the output of the following code snippet?
#include<stdio.h>
main(){
int a = 3, c = 5;
ch = a + e * (int) f + d * c;
printf("%c", ch);
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 61
What is the output of the following code snippet?
#include<stdio.h>
# c ude std o
main()
{
int x = 0;
int y = 0;
if ((++x || ++y))
{
x++;;
}
printf("%d %d\n",x, y);
}
a) 1 0 b) 1 1 c) 2 1 d) 2 0
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 62
What is the output of the following code snippet?
#include<stdio.h>
main()
{
int x = 0;
int y = 0;
if (!(x++ && ++y))
{
++x;
}
printf("%d
printf( %d %d\n"
%d\n ,x,
x y);
}
a) 0 0 b) 1 0 c) 2 0 d) 2 1
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 63
What is the output of the following code snippet?
#include<stdio.h>
main()
{
int x = 0;
int y = 0;
if (!x++ && ++y)
{
++x;
}
printf("%d
printf( %d %d\n"
%d\n ,x,x y);
}
a) 0 0 b) 1 0 c) 2 0 d) 2 1
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 64
Program Development Using C Language
Control statements
ELTP-CAMPUS
EXCELLENT CAMPUS PLACEMENTS 65 ELTP-GROUP
Control statements
1. Selectional
2. Iterational
1 unconditional
1. diti l control
t l statements
t t t
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 66
Selectional Statement
if (expr)
( )
if (expr) if (expr) stmt1;
stmt1; stmt1; else if (expr)
else stmt2;
stmt2; else if (expr)
……
else
stmtn;
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 67
Selectional Statement
if (expr)
stmt1;
F
expr
stmt1;
t t1
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 68
WAP to find the largest of three numbers using if statement
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 69
WAP to find the largest of three numbers using if statement
#include <stdio.h>
void main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d
f("%d %d %d"
%d", &
&a, &b
&b, &
&c);
)
if( a >= b && a >= c )
printf("%d
printf( %d is the largest number
number.", a);
if( b >= a && b >= c )
printf("%d
p ( is the largest
g number.",, b);
);
if( c >= a && c >= b )
printf("%d is the largest number.", c);}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 70
Selectional Statement
if (expr)
stmt1;
else expr F
stmt2;
stmt1;
t t1 stmt2;
t t2
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 71
Write C program to find the largest of three numbers using
nested of if...else statement
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 72
Write C program to find the
int a, b, c;
printf("Enter three numbers: "); largest of three numbers using
scanf("%d %d %d", &a, &b, &c);
nested if and if...else statement
if (a>=b)
{
if(a>=c)
printf("%d is the largest number.", a);
else
l
printf("%d is the largest number.", c);
}
else{
if(b>=c)
printf("%d
printf( %d is the largest number.",
number. , b);
else
printf("%d is the largest number.",c);
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 73
Selectional Statement
if (expr)
F
stmt1; expr
else if (expr)
stmt2; T expr F
else
stmt3; T
stmt1;
t t1 stmt2; stmt3;
t t3
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 74
Write program that reads marks of a student and
computes displays grade
computes,
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 75
Selectional Statement
if (mark >=> 90)
printf(“Grade A”);
F
else if (mark >= 80) mark >= 90
printf(“Grade B”);
else T F
mark >= 80
printf(“Grade
printf( Grade C”);
C );
T
P t ‘A’
Put Put ‘B’ P t ‘C’
Put
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 76
Write program that reads marks of a student and computes and displays grade
int mark;
else
printf("The
i tf("Th Grade
G d off student
t d t is
i = ‘E’”);
‘E’”)
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 77
Write c program to find the largest of three numbers
using if...elseif…else statement
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 78
Write c program to find the largest of three numbers using if...elseif…else
statements
int a, b, c;
else
l if (b>=a
(b> && b>
b>=c))
else
printf("%d
printf( %d is the largest number
number.", c);
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 79
Looping Statements:– for loop statement
Repeat
p an action specified
p number of times
initialization
Syntax for loop stmt:
(expr1)
Loop body
(S)
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 80
Write a C p
program
g to find the sum of first N natural numbers
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 81
Write a C program to find the sum of first N natural numbers
#include<stdio.h>
main(){
int n, sum=0, i;
scanf(“
f(“ %d”
%d”, &n);
& )
sum = sum + i;
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 82
Write a C program to sum of given N numbers.
#include<stdio.h>
void main(){
unsigned int n, i;
int temp;
long int sum =0;
printf("Enter Number Count (Positive Decimal Value from 0 to 65535):");
scanf("%u", &n);
for (i=1;i<=n; i++){
printf("Enter
i tf("E t %d number:",i);
b " i)
scanf("%d", &temp);
sum = sum + temp;
}
printf ("\n\nSum of %u Numbers is = %ld", n, sum);
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 83
Write a C program to find the multiplication of two
unsigned integer without using multiplication operator
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 84
Write a C program to find the multiplication of two
g
unsigned integer
g without using
g multiplication
p operator
p
#include<stdio.h>
main(){
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 85
Looping Statements – while
Repeat an action till the expression evaluates to false
while(expr){
hil ( ){
S;
} expr
Loop Body
(S)
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 86
W it a C program to
Write t find
fi d the
th average marks
k off three
th
subjects of each student in a class
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 87
Write a program to find the average marks of three subjects of
each student in a class
fl t Avg;
float A
char
h Choice
Ch i = 'y';
' '
scanf("%d%d%d",
f("%d%d%d" &Mark1,
&M k1 &Mark2,
&M k2 &Mark3);
&M k3)
A =S
Avg Sum/3.0;
/3 0
scanf("%c", &Choice);
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 89
Write a C program to find the quotient and remainder to divide two
unsigned integers without using division and remainder operator.
#include<stdio.h>
main(){
unsigned int a, b, rem, quot=0;
scanf("%u %u", &a, &b);
rem = a;
while(rem >= b){
rem = rem - b;;
quot++;
} // End of while loop
printf("Divide %u by %u : ", a, b);
printf("quotient = %u and remainder = %u", quot, rem);
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 90
Looping Statements : do - while
do{
S; expr
}while(expr);
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 91
C Program to add numbers until user enter 0 from keyboard
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 92
C Program to add numbers until user enter 0 from keyboard
using do while loop
#include<stdio.h>
void main() {
int number, sum = 0;
do {
printf("do
i tf("d you wantt tto stop
t addition
dditi enter
t 0 otherwise
th i enter
t number:");
b ")
scanf("%d", &number);
sum = sum + number;
} while(number != 0); /* end of do while loop*/
printf("Sum
p ( = %d",, sum);
);
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 93
C Program to add numbers until user enter 0 from keyboard
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 94
C Program to add numbers until user enter 0 from keyboard
using while loop
#include<stdio.h>
void main() {
while(number
( != 0){
){
scanf("%d"
scanf( %d , &number);
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 95
break Statement:
The break statement is used to:
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 96
What is the output of the following code snippet?
main()
{
i t j;
int j
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 97
What is the output of the following code snippet?
main()
{
i t j;
int j
if( j == 2)
break;
}
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 98
Write a C Program for the following flowchart
Start
Sum = 0
Get temp
If temp == 0 T
Put Sum
F
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 99
Write a C Program for the following flowchart
main()
{
int sum = 0;;
do {
scanf(“%d”, &temp);
} while
hile (1)
(1);
printf(“%d”, sum);
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 100
WAP in C Language to add given N integer numbers. If any time sum >=100,
stop and display sum Start
Get N
I = 1,
1 sum =0
F
If (I < =N)
Get temp
I=I+1
F T
If (sum >= 100) Put sum
End
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 101
WAP in C Language to add given N integer numbers. If any time sum >=100,
stop and display sum
#include<stdio.h>
void main(){
unsigned int n, i;
int sum=0, temp;
scanf("%u",&n);
f("% " & )
for (i=1;i<=n; i++){
scanf("%d"
scanf( %d ,&temp);
&temp);
sum = sum + temp;
if(sum
( >= 100)) break;;
}
printf("The sum is = %d", sum);
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 102
‘continue’ Statement
• ‘continue’ statement forces
f the next iteration off the loop
to take place and skips the code between continue
statement and the end of the loop
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 103
What is the output of the following code snippet?
main() {
int i;
printf(“%d”, i );
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 104
What is the output of the following code snippet?
main() {
int i;
if ( i == 4))
continue;
printf(“%d”, i );
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 105
Write a C Program for the following flowchart
Start
Get n
sum = 0
i=1
If
F
i<=N
? Put sum
T
sum = sum + temp
i=i + 1 End
Get temp
If
T
temp % 2 != 0
?
ELTP-CAMPUS F
ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 106
Write a C Program for the following flowchart
main(){
int sum = 0, i=1;
scanf(“%d”,
( &n);
)
while(i <= n) {
i = i + 1;
scanf(“%d”, &temp);
if(t
if(temp %2 !=0)
! 0) continue;
ti
}
printf(“%d”, sum);
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 107
WAP in C Language to count even integer numbers in
given N numbers
g Start
Get N
I=1
count= 0
F
I<=N
T put count
Get temp
E d
End
F T
I=I+1 If temp %2
== 0
count = count + 1
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 108
WAP in C Language to count even integer numbers in
given N numbers
g
#include<stdio.h>
void main(){
(){
unsigned int n, i;
int temp
temp, count =0;
0;
scanf("%u", &n);
scanf("%d", &temp);
count++;}
[ stmt2; ]
T
break;
stmt1;
t t1 stmt2; stmt3;
t t3
default :
[ stmt3; ]
break;
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 110
Write a C p
program
g to check given
g alphabet
p is vowel or
not vowel
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 111
Write a C program to check given alphabet is vowel or
not vowel main(){
char ch;
scanf(“%c”, &ch);
switch(ch)
( ){
case ‘a’ : printf(“Vowel”);
break;
case ‘e’
e : printf(
printf(“Vowel”);
Vowel );
break;
case ‘i’ : printf(“Vowel”);
break;
case ‘o’ : printf(“Vowel”);
break;
case ‘u’ : printf(“Vowel”);
break;
default : printf(“Not a vowel”);} // End of switch case
}//End of main function
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 112
Write a program to check given alphabet is vowel or
not vowel main(){
char ch;
scanf(“%c”, &ch);
switch(ch) {
case ‘a’ :
case ‘e’
e :
case ‘i’ :
case ‘o’ :
case ‘u’
‘ ’ : printf(“Vowel”);
i tf(“V l”)
break;
default : printf(“Not a vowel”);
}
}
ELTP-CAMPUS ELTP-GROUP
EXCELLENT CAMPUS PLACEMENTS 113