1. Index
Session 1: Basic Concept of “C”.
Session2: Variableand Data Type
Session3: Operators
Session4: Loop
Session5: Array
Session6: Function
Session7: Structure
Session8: Link List
Session9: Pointer
Session10: FileI/O
String
Command Line Argument
Type Casting
StorageClasses
Pre-Processor Directives
Simple and static Assertion
Graphics
2. Session-1
BASIC CONCEPT OF “C”
“C” is a general-purpose, high-levellanguagethat was originally development by
“Dennis M. Ritchi” to develop theUNIX operating system“AT T Bell Labs”.
In 1978, Brian Kernighanand Dennis Ritchieproduced thefirst publicity
available description of “C”. “C” has now become a widely used professional
languagefor various reasons.
1: Easy to learn
2: Structured language
3: It produces efficient program
4: It can handlelow-level activities
5: It can compile on a variety of computer platforms.
Facts about “C”
1: “C” was invented to writean operating systemcalled UNIX.
2: “C” is successor of “B” languagewhichwas introduced around theearly 1970s.
3:Thelanguagewas formalized in 1998 by theAmerican NationalStandard
Institute(ANSI).
4: TheUNIX OS was totally written in “C”.
5: Today “C” is themost widely used and popular system programming language.
6: Most of thestate-of-theart software has been implemented using in “C”.
3. Why Use “C”
“C” was initially used for systemdevelopment work, particularlytheprograms
that make-up theoperating system.”C” was adapted as a system development
languagebecauseit produces codethat runs nearly as fast as the code written in
assembly language. Some example of theuse “C” might be:
1: Operating System
2: LanguageCompilers
3: Assemblers
4: Text Editors
5: Print Spoolers
6: Network Drivers
7: Modern Program
8: Database
Compiler(ALT +F9):
Its software that areuse to convert in machinelanguage.
Save of “C” file:
File Name.c
Execute (CTRL + F9):
Its means result.
4. Basic Structure of Program
#include<conio.h>-------------------1
#include<stdio.h>--------------------2
void main()--------------------------3
{-----------------------------------4
clrscr();------------------------5
getch();------------------------6
}-----------------------------------7
#: It’s a pre-process any program.
Include:It’s a keyword that areuse to collections of header files.
Header File:Header files are useto collections of different – 2 types of
classes and extension “.h”.
Conio: Conio means ConsoleInput Output thatis useto any programin
dynamic mode.
Stdio: Its means Standard Input Output thatareuse to any programin
static mode.
void:It’s a keyword, that areuse does not return any value, in case use int,
char, float than returnvalue.
main(): It’s a system-defineunction, without its functiondoes not
compile and executea program.
clrscr():It’s means clean screen, that areuse to clean old information
form output window.
getch(): Its means get character, that areuseto hold output window.
5. printf ():
That are use to display information.
Example: printf (“Hello Dear”)
Program: WAP display “Hello World “message.
Solution:
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
printf(“HelloWorld”);
getch();
}
n :Its keyword, that areuse to break a line.
Example:printf(“HellonWorld”)
Program:
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
printf(“HellonWorld”);
getch();
}
Output: Hello
World
6. t: Its keyword, that areused to createa space in two word, 1 t = 6 space
Program:
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
printf(“HellotWorld”);
getch();
}
Output: Hello World
b: Its keyword, Its means backspace, that areused to remove any character
right toleft, 1 b = 1 character remove.
Program:
#include<conio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“HellobWorld”);
getch();
}
Output: HellWorld
7. TASK
1: #
# #
# # #
# # # #
2: # # # # #
# # # #
# # #
# #
#
3: 1 1 1 1 1
1 1 1 1
1 1 1
1 1
1
4: A B C D
A B C
A B
A
5: A
B B
C C C
D D D D
8. Question
1: What is a “C” Programming?
2: What is printf ()?
3: What is a n in “C” Programming?
9. 4: What is a t in “C” Programming?
5: What is a b in “C” Programming?
10. Session-2
DATA TYPE AND VARIABLE
DATA TYPE: Data Type are used to identify thehard disk which types values
store in a disk, thereare two type of data type.
1: PrimitiveData Type-
int ---------%d------------Integer
char----------%c-----------Character
float-----------%f-----------Decimal
2: Non-Primitive: double, long, short
Variable: A variable is nothing but a name given to a storage area that our
programs can manipulate. Each variable in C has a specific type, which
determines thesize and layout of the variable's memory; the range of values that
can be stored within that memory; and the set of operations that can be applied
to the variable.
The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and
lowercaseletters are distinct becauseC is case-sensitive. Based on the basic types
explained in the previous chapter, there will be the following basic variable
types –
Sr.No. Type & Description
1 Char
Typically a single octet(one byte). This is an integer type.
2 Int
The most natural size of integer for the machine.
11. 3
Float
A single-precision floating point value.
4 Double
A double-precision floating point value.
5 Void
Represents the absence of type.
Example: int marks;
Marks=20;
char name;
name=’A’;
Program: WAP Enter two number as a integer like a, b than value a=10 and
b=20 than display a total.
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int a, b, tot;
clrscr();
a=10;
b=20;
tot = a + b;
printf(“Total is=%d”,tot);
getch();
}
12. OUTPUT:
Total is=30
Program: WAP Enter two number as an integer like a, b than value a=10 &
b=20 than display Subtraction.
Solution:#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sub;
clrscr();
a=10;
b=5;
sub = a-b;
printf(“Substraction is=%d”,sub);
getch();
}
Output: Subtraction is = 5
Program: WAP Enter two number as an integer like a, b and value a=10 &
b=5 than display multiplication.
Solution:#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sub;
clrscr();
13. a=10;
b=5;
sub = a*b;
printf(“Multiplication is=%d”,sub);
getch();
} Output: Multiplication is = 50
Program: WAP Enter two number as an integer like a, b and value a=10 &
b=5 than display divide.
Solution:#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sub;
clrscr();
a=10;
b=5;
sub = a/b;
printf(“Divide is=%d”,sub);
getch();
} Output: Divide is = 2
14. Program: WAP Enter two number as an integer like a, b and value a=5 & b=2
than display interchange value.
Solution:#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
a = 5;
b=2;
a = a+b;
b = a-b;
a= a – b;
printf(“A value is=%dn”,a);
printf(“B value is=%d”,b);
getch();
} Output:A value is=2
B value is=5
15. Question
1: What is a Data Type?
2: What is a Variable?
3: Explain:
A)-clrscr() B)- getch()
16. Multiple Choice Question
1: %d format means.
a)-int b)- char c)- float d)- long
2: %c format means.
a)-int b)- char c)- float d)- long
3:%f format means.
a)-int b)- char c)- float d)- long
4: int a, b, c;
a=10, b=20;
c=a;
a=b;
b=c;
printf(“A value is=%dn”,a);
printf(“B value is=%d”,b);
a)- A value is=20 b)- A value is=20 B value is=10 c)- Error d)- None
B value is =10
5: inta,b,c;
a=10;
b=20;
c=a+b
printf(“Total is=%d”,c);
a)- Error b)- Total is=30 c)- Both d)- None
17. Session-3
Operators
Operator are used to operate a program by different – 2 types of operators.
1: Arithmetic Operator
2: Assignment Operator
3: Unary Operator
4: Conditional Operator
5: Relational Operator
6: Logical Operator
7: Bit wise Operator
Arithmetic Operator:
Its types of operator are used to operate a program by +, - , *, /.
Program:WAP Enter two number a & b as a integer and value a=10 and b=20
than display sum;
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int a, b, tot;
clrscr();
a=10;
b=20;
tot = a+ b;
printf(“Total is=%d”,tot);
getch();
}
Output: Total is=30
18. Assignment Operator:
Its operator always based two operator like +=, -=, /=, *= etc.
Program:WAP Enter a =10 & b=20 than usea+=b formula and display a value
of a.
Solution:#include<conio.h>
#include<stdio.h>
void main()
{
int a , b;
clrscr();
a=10;
b=20;
a+=b;
printf(“A value is=%d”,a);
getch();
}
Output:
A value is=30
Program:WAP Enter a =10 & b=5 use a-=b formula display a value.
Solution:#include<conio.h>
#include<stdio.h>
void main()
{
int a, b;
clrscr();
19. a=10;
b=5;
a-=b;
printf(“A value is=%d”,a);
getch();
} Output: A value is=5
Unary Operator: Its operator are use to operat a program by increment
and decrement. There are four types of operator used.
1: Pre Increment
2: Post Increment
3: Pre Decrement
4: Post Decrement
1: Pre Increment: In this type of increment first valueincrement thanvaue
transfer for another variable.
Example: ++a
Program: WAP Enter a =10 than use ++a formula than transfer value in b
variable than display a & b value.
Solution:#include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
a = 10;
20. ++a;
b=a;
printf(“A value is=%dn”,a);
printf(“B value is=%d”,b);
getch();
}
Output: A value is=11
B value is=11
Post Increment:In this type of increment first value transfer in other
variable than incrment a value.
Example: a++
Program:WAP Enter a =10 than use a++ formula than display b value.
Solution:#include<conio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
a =10;
b=a;
a++;
printf(“A value is=%dn”,a);
printf(“B value is=%d”,b);
getch();
}
21. Output: A value is= 11
B value is=10;
Program:WAP a=10 and b=20 than display c value and use formula.
C=(++a)+(2*3)+(b++)
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int a, b;
clrscr();
a=10;
b=20;
c=(++a)+(2*3)+(b++);
printf(“C value is=%d”,c);
getch();
}
Output: C value is=37
Pre-decrement: In this type of decrement first value decrement than value
transfer another variable.
Example: --a
22. Program:WAP Enter a =10 than use - -a formula than display b value.
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
a=10;
--a;
b=a;
printf(“ A value is=%dn”,a);
printf(“ B value is=%d”,b);
getch();
}
Output: A value is=9
B value is=9
Post-decrement: In this type of decrement first transfer valuein other than
decrement.
Example: a—
Program:WAP Enter a =10 than use a- - formula than display b value.
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
23. a=10;
b=a;
--a;
printf(“ A value is=%dn”,a);
printf(“ B value is=%d”,b);
getch();
}
Output: A value is=9
B value is=10
Conditional Operator (Decision Control Statement):
Decision Control statement lets you test conditions and perform different
operations depending up on results.
Previous Code
Is the condition true?
YES NO
TRUE FALSE
There are 4 types of decision control statement.
1: If Statement
2: If else Statement
24. 3: Nested If else Statement
4: Switch case Statement
1: If Statement: If statement is used to implement decision control
statement, when this statement is encountered, compiler automatically gets the
instruction thatwhat follows is a decision control statement, In if statement we
test a condition and if the condition is true than the statement followed by if is
evaluated, if the condition is false, than the compiler skips that part of the code.
If(condition)
Statement;
Example: #include<conio.h>
#include<stdio.h>
void main()
{
int a , b;
clrscr();
a =4;
b=8;
if(a>=b)
{
printf(“ A is greater=%d”,a);
}
getch();
}
Output:
Compile Successful but no result
25. Example: #include<conio.h>
#include<stdio.h>
void main()
{
int a , b;
clrscr();
a =14;
b=8;
if(a>=b)
{
printf(“ A is greater=%d”,a);
}
getch();
}
Output: A is greater=14
Example: #include<conio.h>
#include<stdio.h>
void main()
{
int a ;
clrscr();
a =2;
if(a%2==0)
{
26. printf(“Even”);
}
getch();
}
Output: Even
scanf(): Its function that are use to input a value in runtime by user.
Example: #include<stdio.h>
#include<conio.h>
void main()
{
int x, y;
printf("enter thevalueof x:");
scanf("%d", &x);
printf("enterthevalueof y:");
scanf("%d", &y);
if (x>y)
{
printf("x is greater than yn");
}
if (x<y)
{
printf("x is less than yn");
}
if (x==y)
{
printf("x is equalto yn");
}
printf("End of Program");
getch();
}
27. Output:
enter the valueof x:20
enter the valueof y:20
x is equalto y
End of Program
Question
1: C program to print the square of a number if it is less than 10.
28. 2: Write a C program to find maximum between two numbers.
3: Write a C program to find maximum between three numbers.
29. 1: If else Statement: The if statement can execute a single statement or
multiple statements but that too only when the conditions is true you know it
does nothing when condition is false, statement you want to execute a set of
statements even when condition is false.
if(condtion)
Statement 1;
else
Statement 2;
Program:Theyear of serviceof an employee in a particular organization is
given, if theyear of service of an employee, exceeds 3 years than assign hima
salary of Rs. 35000/- otherwiseamount of 10000 Rs/- should be given to him.
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int yos;
clrscr();
yos = 10;
if(yos>=3)
printf(“Salary = Rs. 35000”);
else
printf(“Salary = Rs. 10000”);
getch();
30. }
Output: Salary=Rs. 35000
Program: WAP to calculateelectricitybill.
Solution:
#include <stdio.h>
#include<conio.h>
void main()
{
int unit;
float amt, total_amt, sur_charge;
printf("Enter total units consumed: ");
scanf("%d", &unit);
if(unit <= 50)
{
amt = unit * 0.50;
}
else if(unit <= 150)
{
amt = 25 + ((unit-50) * 0.75);
}
else if(unit <= 250)
{
amt = 100 + ((unit-150) * 1.20);
}
else
{
amt = 220 + ((unit-250) * 1.50);
}
sur_charge = amt * 0.20;
total_amt = amt + sur_charge;
printf("Electricity Bill = Rs. %.2f", total_amt);
getch();
}
31. Multiple Choice Questions
1: #include<stdio.h>
void main()
{
int i = 5, j = 6, k = 7;
if(i > j == k)
printf("%d %d %d", i++, ++j, --k);
else
printf("%d %d %d", i, j, k);
getch();
}
A: 5 7 6 B: 5 6 7 C: 666 D: 7 5 6
2: #include<stdio.h>
void main()
{
int i = 2;
if(i == (1, 2))
printf("Hai");
else
printf("NoHai");
getch();
}
A: Compilation Error B: Runtime Error C: Hai D: No Hai
32. Question
1: WAP that will find out given number is odd or even.
2: WAP to calculatetotalsalary and years of services of an employee.
33. 3: If basic slary is greater than or equalto 12000/-rs than assign himhra=4000
and da=6000 otherwisesalary is thegross salary. WAP to calculategross salary?
34. 4: WAP to camparearea of rectanglewith perimeter, lengthand breadth of a
rectanglearegiven, If thearea of the rectangleis greater than perimeter than
increaselength by 6 else breadth by 10.
35. 3: Nested If else Statement: When wewrite entire block either inside the
body of if statement or inside the body of else statement, then irt is called as
nested if else.
If(condition)
Statement 1;
else if(condition)
Statement 1;
else
Statement 2;
Program: WAP to calculateGross Slary.
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int bs,da,hra,gs;
clrscr();
bs= 7000;
if(bs>=12000)
{
da=6000;
hra=4000;
gs = bs + da + hra;
printf(“Gross Salary=Rs.%d”,gs);
}
36. else if(bs>=6000)
{
da=3000;
hra = 2000;
gs = bs + da + hra;
printf(“GrossSalary = Rs.%d”,gs);
}
else
{
gs = bs;
printf(“Gross Salary = Rs.%d”,gs);
}
getch();
}
Output: Gross Salar=Rs. 12000
Switch Case Statement: Consider a situation, you go to hotal were a menu
is given to you, lots of items are written on that menu, you can make a choice
accordingtoyour wish, Henceyou can order for the item, logic is very simple,
you are making a choicefrom all alternatives thatare given to you, switch case
allows us to make decision from number of choices.
switch(expression)
{
Case value1:
Statement;
Case value 2:
Statement;
Case value 3:
Statement;
default:
Statement;
}
37. switch is the keyword thattells the compiler what switch case proceeds
expression is any value of type byte, short, int or char. All case values are
constant, they are not variables, and there should be no duplicate values.
Previous Code Performaction when value matches
the case constant.
YES
Is the value matches
NO
YES
Is the value matches
NO
Is the value matches YES
NO
Default block
Execute next code
When comes out of
Switch case
38. Program:#include<conio.h>
#include<stdio.h>
void main()
{
int choice;
clrscr();
choice=3;
switch(choice)
{
case 1:
printf(“Hello”);
case 2:
printf(“How AreYou”);
case 3:
printf(“GetWill Soon”);
case 4:
printf(“TakeCase”);
default:
printf(“Good Bye”);
getch();
}
}
Output: Get Will Soon Take CaseGood Bye
Program: Write a menu driven programwhich has following options:
1: Number is Odd
2: Number is even
3: Display a message
4: Exit
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int a;
a= 3;
clrscr();
printf(“Main Menu”);
printf(“1. Number is Odd”);
printf(“2. Number is Even”);
39. printf(“3.Display a Message”);
printf(“4. Exit”);
printf(“Enter Choice as 1,2,3,4”);
switch(a)
{
case 1:
if((a%2)!=0)
printf(“Number is Odd”);
else
printf(“Number is Even”);
break;
case 2:
if((a%2)==0)
printf(“Number is Even”);
else
printf(“Number is not Even);
break;
case 3:
printf(“Good Day”);
break;
case 4:
break;
default:
printf(“Wrong Choice”);
}
getch();
}
Output:
Main Menu
1. Number is Odd
2. Number is Even
3. Display a message
4. Exit
Enter Choice as a 1,2,3,4
1
Number is Odd
Note:These operators operateon boolean operands, they are also called as
boolean logical operators, they operates on two boolean values which forma
boolean value as a result.
40. Logical Operator: They are moreefficient to implement as they can check
more than one condition in one pair of parentheses.
Program:#include<conio.h>
#include<stdio.h>
void main()
{
int a;
clrscr();
a = 40;
if((a>40)&&))
printf(“A is greater than 40 but less than 45”);
else
printf(“a=%d”,a);
getch();
}
Output
a=40
Program:
#include<conio.h>
#include<stdio.h>
Void main()
{
char val=’a’;
char val=’b’;
if(val==’a’)//(val==’c’))
printf(“Havea nice day”);
else
printf(“Goodbye”);
getch();
}
Output
Havea nice day
41. Relational Operator: Relational Operator determines the relation among
two or more different operand. They are used to compareequality among
operands. There aresix relational operators, they are –
Operator Meaning
== Equal To
!> NotEqual To
> GreaterThan
< LessThan
>= GreaterThan or equal To
<= LessThan or equal To
Bitwise Operator: Bitwise operator permit us to access and manipulate
individual bits with in a piece of data, these can be applied to the integer types:
long, int, short, char & byte. These operators cannotoperate upon float and
double.
7 6 5 4 3 2 1 0
8-bit memory representation for byte
Operator Meaning
~ Bitswise UnaryNOT
& Bitwise AND
! Bitswise OR
^ Bitwise XOR
>> ShiftRight
<< ShiftLeft
&= Bitwise ANDassignment
!= Bitwise ORassignment
^= Bitwise XORassignment
>>= Shiftrightassignment
<<= ShiftLeftassignment
* Bitwise AND (&): The AND operator produces the output 1 only when all
inputs are1. Otherwiseoutput is 0(zero) in all other cases.
For Example: Lets there are two numbers say 11001100and 01010101,
when AND operator is applied to these number then output is 01000100.
*Bitwise OR (||): The OR operator produces the output 1 only if any one of
all the inputs is 1 otherwise output 0(Zero) in all other cases.
42. For Example: Lets there are two numbers say 11001100and 0101010, when
OR operator is applied to these number then output is 11011101.
*Bitwise XOR (^): The XORoperator produces the output 1 when anyoneof
the input is 1, result is 0(Zero) if all inputs are same.
For Example: Lets there are two numbers say 11001100 and 01010101,
when AND operator is applied to these number then output is 10011001.
*Bitwise NOT (~): The NOT operator inverts all of the bits of the operand; it
is also called as complement operator.
For Example: Lets there is a number say 11001100, when NOToperator is
applied to this number then output is 00110011.
Left Shift Operator (<<): The left shiftoperator shifts all of the bits in a
value to the left a specified number of times.
General Form:
Value << num;
Here value is value on which shift haveto be applied and num specifies the
number of times bits will be shifted to the left, for each left shift, mostsignificant
bit or high order bit is shifted out or lost and zero is added to the right.
1 0 0 1 1 1 0 1
MSB LSB
Now if left shift is applied to this value, then MSBis lost out and 0(Zero) is added
to the right, so after on left it will look like.
0 0 1 1 1 0 1 0
MSB LSB
Right Shift Operator (>>): The right shift operator shifts all of the right a
specified number of times.
General Form:
Value >> num;
Here value is value on which shift haveto be applied and num specifies the
number of times bits will be shifted to the right, for each right shift, least
significant bit or low order bit is shifted out or lost and 0(zero) is added to the left.
1 0 0 1 1 1 0 1
MSB LSB
Now if right shiftis added to this value, then LSB is lost out and 0(zero) is added to
the left, so after one right it will look like.
0 1 0 0 1 1 1 0
MSB LSB
43. Unsigned Right Shift (>>>): The unsigned right shift operator >>>
produces a purevalue that is its left operand is rightshifted with 0(zero)
extension by the number of bits specified by its right operand, Right shifted with
0(zero) extension means that shifting a value ‘n’ places to the right causes the ‘n’
high order bits to contain 0(zero), the >>> operator may appear as partof a shift
expression.
Question
1: What is a Unary Operator with example?
44. 2: What is a Relational Operator with example?
3: What is a Logical Operator with example?
45. 4: WAP enter a any character and check vowelyes or no?
46. 5: WAP Ages of Ram, Shyamand Ajay are given to determine who is the youngest.
47. 6: WAP if basic salary is greater than or equal to 12000/- Rs. then assign him hra =
4000 and da =6000 otherwisebasic salary is the gross salary, and display gross
salary?
48.
49. Session-4
LOOP
Loop Controlstatement allow you to row one or more lines of codes repetitively,
you can repeat the statements in a loop structures until a condition is true, until a
condition is false, a specified number of times.
Previous Code
Performaction when condition is true
YES
Is the condition true
NO Code that you want to repeat
Execute
There are three type of loop in “c”.
1: For Loop
2: While Loop
3: do while Loop
1: For Loop: ‘for’ loop is the most commonly used loop, Itis used to display
values repeatedly as per condition given, Task is repeated until the conditions
become false, for statement is stated with the keyword ‘for’, thereare three
expressions which appear with in a for statement.
Initialization Expression
Test Expression
Increment/DecrementExpression
50. for (initialization; test; increment/decrement)
{
Statement block;
}
Previous code
Initialization
YES Performaction when condition is true
Body of loop
Execute next code
When condition is false
Program: WAP display “Good Day” 5 times.
Solutions:#include<conio.h>
#include<stdio.h>
void main()
{
int a;
clrscr();
for(a=1;a<=5;a++)
51. {
printf(“Good Dayn”);
}
getch();
}
Output: Good Day
Good Day
Good Day
Good Day
Good Day
Program: WAP to demonstratefor loop with multiple statements.
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int a;
clrscr();
for(a = 1; a<=5;a++)
{
printf(“Good Day”);
printf(“valueof a=%dn”,a);
}
getch();
}
52. Output: Good Day value of a = 1
Good Day value of a = 2
Good Day value of a = 3
Good Day value of a = 4
Good Day value of a = 5
Program: WAP that will declare a loop variable inside the “for” loop.
Solutions:#include<conio.h>
#include<stdio.h>
void main()
{
for(inta=0;a<=10;a+=2)
{
printf(“Good Day”);
printf(“Valueof a=%dn”,a);
}
getch();
}
Output: Good Day value of a =0
Good Day value of a =1
Good Day value of a =2
Good Day value of a =3
Good Day value of a =4
Good Day value of a =5
Good Day value of a =6
53. Good Day value of a =7
Good Day value of a =8
Good Day value of a =9
Program: WAP that will initialize variable outside for loop.
Solutions: #include<conio.h>
#include<stdio.h>
void main()
{
int a = 0;
clrscr();
for(; a<=4;a+=1)
{
printf(“Good Dayn”);
}
getch();
}
Output: Good Day
Good Day
Good Day
Good Day
Good Day
Program: WAP that will declare increment outside “for” statement.
55. for(;i<=4)
{
printf(“Good Byen”);
i+=1;
}
getch();
}
Output: Good Bye
Good Bye
Good Bye
Good Bye
Good Bye
Program: That will use comma operator.
Solution:#include<conio.h>
#include<stdio.h>
void main()
{
int i , j;
clrscr();
for(i=1;j=3;i<j;i++,j--)
{
int c = i + j;
printf(“Sumof I and J=%d”,c);
}
56. getch();
}
Output: Sumof I and J=4
While Loop: While loop is used when we want to execute set of statements
number of times. In this loop code execution is based on truthiness of a condition,
it will go on executing until the condition becomes false.
while (condition)
{
Statement block;
}
Program:#include<conio.h>
#include<stdio.h>
void main()
{
int i;
clrscr();
i = 1;
while(i<5)
{
printf(“Ashutoshn”);
i = i + 1;
}
getch();
}
57. Output: Ashutosh
Ashutosh
Ashutosh
Ashutosh
Ashutosh
Program:WAP to print factorial of a number.
Solution:#include<conio.h>
#include<conio.h>
void main()
{
int i,num,fact;
clrscr();
num=5;
fact=1;
while(i<=num)
{
fact=fact*i;
i=i+1;
}
printf(“Factorialof 5=%d”,fact);
getch();
}
Output: Factorialof 5=120
58. Program: WAP to check whether given number is palindrome.
Solution:#include<conio.h>
#include<stdio.h>
void main()
{
int i, num,rev,digit;
clrscr();
num=546;
i=num;
rev = 0;
while(num!=0)
{
digit=num%10;
rev = rev*10+digit;
num = num/10;
}
printf(“Number =%d”,i);
printf(“Reverseof Number=%d”,rev);
if(i==rev)
{
printf(“Number is Palindrome”);
}
else
{
59. printf(“Number is not Palindrome”);
}
getch();
}
Output:Number = 546
Reverseof Number=645
Number is not Palindrome
Program: WAP to print table of a number.
Solution:#include<conio.h>
#include<stdio.h>
void main()
{
int i,num,mul;
clrscr();
num=5;
i = 1;
printf(“Table of 5”);
while(i<=10)
{
mul = i*num;
printf(“%d”,num,”*”,I,”=”,mul);
}
getch();
}
60. Output: Table of 5
5 * 1 = 5
5 * 2 = 10
5 * 3 =1 5
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Do while Loop: Do while loop is used to execute a statement or set of
statements number of times based on somecondition.
do
{
Statement block;
} while(condition);
61. Previous Code
Initialization
Body of loop Performaction
When condition
Is true
Increment
YES Execute next code
Is the condition true NO when condition is
false
Program:#include<conio.h>
#include<stdio.h>
void main()
{
int i;
clrscr();
i = 1;
62. do
{
printf(“Ashu”);
i = i + 1;
}while(i<5);
getch();
}
Output: Ashu
Ashu
Ashu
Ashu
Program:#include<conio.h>
#include<stdio.h>
void main()
{
int i;
clrscr();
i = 1;
do
{
printf(“ASHU”);
i = i+1;
}
while(i>5);
63. getch();
}
Output: ASHU
Nested Loop: When we define a loop inside a loop then it is termed as nested
loops.
Program:#include<conio.h>
#include<stdio.h>
void main()
{
for(int i=4;i>0;i--)
{
for(int j=1;j<=i; j++)
{
printf(“%d”,i);
printf(“n”);
}
}
getch();
}
Output:0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
65. Jump Statement: Computer keeps on executing instructions one by one in a
sequence. Sometimes we want to change the direction of flow in a programlike
jumping fromone section of code to another.
1: break 2: continue 3: return
1: break: break is used when you wantto jump out of controlinstantly, when
break is encountered, the compiler skips that code and execute the next
instruction justafter that code in which break was declared, so break enables us
to skip over a part of code.
Program:#include<conio.h>
#include<stdio.h>
void main()
{
int i;
clrscr();
i = 1;
while(a<5)
{
printf(“Ashutosh”);
if(i==2)
{
break;
i = i+1;
}
69. Output: Ashutosh Srivastava
2: continue Statement: continue is used to by pass portions of code in a
loop and forces the conditional expression to be evaluated, it means sometimes
we wantto continue running the loop but do not wantto execute a portion of
code for a particular iteration. This can be done by using continue, it is a jump
statement that lets you skip a part of code and return to the condition expression
defined in the loop and continue executing loop.
for(init; cont; incr)
{
Statement 1;
if( condition)
continue;
Statement 2;
}
Program: #include<conio.h>
#include<stdio.h>
void main()
{
int i;
clrscr();
for(i = 1; i<=2; i++)
{
printf(“Good Bye”);
if(i == 1)
continue;
70. printf(“Good Day”);
}
getch();
}
Output: Good Bye
Good Bye
Good Day
3: return Statement: The return statement forces a return froma method,
Itterminates the method in which it is executing and control goes back to the
caller function, it also return values to the caller function sometimes.
Program:#include<conio.h>
#include<stdio.h>
void main()
{
int i=1;
clrscr();
printf(“Good Bye”);
if(i==1)
return;
printf(“Good Day”);
getch();
}
Output: Good Bye
75. Session-5
Arrays
Array isa collectionof manyelementsbutof similardatatype,itprovidesusthe facilityof keeping
togethermore thanone variable inmemory,itprovidescontiguous allocationof memory toall its
elements,youcanalsodefine arrayas a group of data undersame name.
Types of Arrays:
Arraysare of three types.
1: One Dimensional Array
2: TwoDimensional Array
3: Multi DimensionalArray
1: One Dimensional Array:
One dimensional array means a array having
one subscripti.e. one value inside brackets, all items of list are given a single
variable name using only one subscripted variableor one dimensional array.
Syntax: DataType arrayname[];
Program:WAP displaying the fifth element of rollno array.
Solution: #include<conio.h>
#include<stdio.h> Output: Roll No. is = 5
void main()
{
int rollno[]={1,2,3,4,5,6,7,8,9};
clrscr();
printf(“Roll No. is = %dn”,rollno[4]);
getch();
}
.
76. Program:WAP Initalizating each of array indivually.
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int r[5];
clrscr();
r[0]=10;
r[1]=20;
r[2]=30;
r[3]=60;
r[4]=70;
printf(“ R value is = %dn”,r[3]);
getch();
}
Output: R value is = 60
Program: #include<conio.h>
#include<stdio.h>
void main()
{
int r[5],t;
clrscr();
r[0]=60;
78. printf(“Totalis=%dn”,t);
}
getch();
}
Output: Total is = 130
Program: WAP finding out the sum of odd and even numbers in array.
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
int arr[]={2,7,8,9,11};
int sumev = 0;
int sumodd=0;
int i;
for(j=0;j<5;j++)
{
if((arr[i]%2==0)
sumev=sumev+arr[i];
else
sumodd = sumodd + arr[i];
}
printf(“Sumof Even numbers in array=%d”, sumev);
printf(“Sumof Odd numbers in array=%d”,sumodd);
getch();
}
Output: Sumof Even numbers in array = 10
Sum of Odd numbers in array = 27
Program:WAP displaying the length of an array.
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
79. double marks[]={4.5,7.8,9.9,8.9};
int n;
clrscr();
n = marks.length;
printf(“Length is=%d”,n);
getch();
}
Output: Length of =4;
Program:WAP Display the elements of an array.
Solution: #include<conio.h>
#include<stdio.h>
void main()
{
char alpha[]={‘A’,’B’,’C’,’D’,’E’};
int n = alpha.length;
printf(“Element of array”);
for(inti =0; i<n; i++)
printf(alpha[i]);
getch();
}
Output: Element of array
A
B
C
D
E