C Language Material
C Language Material
History of C :-
=====================
BCPL (Basic combined programming language) was developed by martin Richard.
BCPL lead to B, it was developed by Ken Thompson. BCPL and B are type less
programming languages. B lead to C, it was developed by Dennis Ritchie in 1972, at
AT&T Bell laboratory on UNIX machine.
Structure of C program
================================================
#include<stdio.h> // include statements
#include<conio.h> // include statements
void main() // main function
{
// processing statements;
}
Any c program has 3 types of statements.
1. Include statements
2. Main function
3. Processing statements
1. Include statements :
❖ These are the first statements.
❖ These are always begins with # symbol.
❖ These are also called as pre processor directives.
❖ It will execute at compile time.
❖ These gives direction to compiler.
❖ It copy the content of header file into our program.
2. Main function :-
❖ It is first executable statement.
❖ It is entry point.
❖ It is mandatory.
❖ It can not be duplicated.
❖ It is user defined function.
❖ It can be write in 6 forms.
a. main()
b. int main()
c. void main()
d. main(void)
e. int main(void)
f. void main(void)
3. Processing statements :-
These are the statements those tells “what to do?”, “when to do?” and
“how to do?” in order to solve the problem
Statement Terminator ( ; ) :-
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
1 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
-------------------------------------------------
❖ An instruction given to computer is called as a statement.
❖ Every statement must be terminated with semi colon.
❖ It is called as statement terminator.
Open Delimiter ( { ):-
---------------------------------------
It is used to indicate the beginning of function or method or block or user defined
reference.
Comment :-
---------------------------------
❖ Comment is a message.
❖ It improves the clarity of program.
❖ It gives extra meaning to program.
❖ Comments do not compile.
❖ Comments do not convert to machine language.
❖ Comments do not execute.
❖ It is just for user.
❖ It is 2 types.
1. Single line comment.
----------------------------------------------
A comment which fit in one line, then it is called single line comment.
It is also called as line comment. it always begins with “//”.
Example :
//This is single line comment.
2. Multi line comment.
-------------------------------------------
A comment which does not fit in one line. It present in multiple lines.
Then it is called multi line comment. it is also called as block comment.
It always enclosed with in /* ….. */
Example :
/*This is example to
Multi line comment.
It wont execute*/
Data types :-
❖ Data types are the keywords.
❖ Those are used to describe the data type.
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
2 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
❖ Data types are used to specify the size variables.
❖ C language provides 4 types of data types.
Sno Data Size Examples
types
1 char 1 byte ‘a’, ‘b’, ‘c’, ‘+’,’-‘, ‘5’,’6’
2 int 2 bytes in 32 bit OS 1,2,3,4,….
4 bytes in 64 bit OS
3 float 4 bytes 1.100000,1.2,1.3,1.111
4 double 8 bytes 1.1000000000,1.2,1.3,1.111
Note :
Float maintains 6 precisions.
Double maintains 10 precisions.
Example :
---------------------------
float r=100/3=33.333333
double r=100/3=33.3333333333
Modifiers :-
Modifier is a keyword which is used to modify the meaning of data type.
Example :
Long, short, signed , unsigned.
Example statement :
Long int
Short int
Signed int
Unsigned int
Variable :
❖ It is a container.
❖ It can store the data.
❖ It can store only one value at a time.
❖ It must be identified by a specific name.
❖ These always created inside the memory.
❖ It can vary its value, so it is called variable.
Declaration or creation of a variable :
It can be created using data type and variable name.
Syntax :
DataType VariableName;
Example :
int rno;
char sname[100];
float avgmarks;
Constants :-
❖ It is also a variable but it retains its value until program termination.
❖ We cannot modify the value of constant.
❖ If we try to change then it will produce error.
❖ For naming conversion constants is always represented in upper case.
It can be created in 2 ways.
a. Create using const keyword.
Identifier :-
------------------------------------------
The names allocated to variables or constants or functions are called as
identifiers.
Example 1:
Int rno;
Here rno is an identifier of a variable.
Example 2:
#define PI 3.14
Here PI is an identifier of a constant.
Example:
Void main()
{
}
Here main is identifier of a function.
Keyword :-
---------------------------------------------
❖ It is a reserved word.
❖ It is a pre defined identifier.
❖ We cannot alter the meaning of a keyword.
❖ C supports 32 keywords.
❖ Example :
Keywords in C Programming
Format specifiers :-
❖ It is a special character.
❖ It is used to specify the format of data.
❖ It always begins with % symbol.
❖ It can be used with input and output.
Data type Format specifier
Char %c
Int %i, %d
Float %f
Double %lf
String %s
Long int %ld
Unsigned int %u
Octal %o
Hexa decimal %x, %X
binary %b
Escape sequences :-
❖ These are the special characters.
❖ It always begins with \ symbol.
❖ It must use only with output.
❖ It is used to escape the sequence of output.
Example :
\t ➔ it is used to move the cursor to a tab distance forward.
\n ➔ it is used to move the cursor the beginning of next line.
\b ➔ it is used to move the cursor to a char backward.
\r ➔ it is used to move the cursor to the beginning of same line.
\\ ➔ it is used to print \ in output.
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
6 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
\’ ➔ it is used to print ‘ in output.
\“ ➔ it is used to print “ in output
Getchar() :-
❖ It is used to read a char.
❖ It wait for enter key.
❖ It read only first char of input.
❖ It is a input function.
Example :
#include<stdio.h>
void main()
{
char ch;
printf("Enter a char : ");
ch=getchar();
putchar( ch );
}
Output :
Enter a char :
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
7 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
abcdefgh ➔ input
a ➔ output
getche()
Output :
Enter a char :
a ➔ input
a ➔ output
getch() :-
Output :
Enter a char :
T ➔ output
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
8 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Putchar () :-
❖ It is used to print a char.
❖ It is an output function.
gets() :-
❖ It is used to read a string.
❖ It stands for get string.
❖ It allow space in a string.
❖ It is an input function.
Example :
#include<stdio.h>
void main()
{
char sname[100];
printf("Enter student name : ");
gets(sname);
puts(sname);
}
Output :
Enter student name : nagool shareef
nagool shareef
puts () :-
❖ It is used to print a string.
❖ It is an output function.
Scanf() :-
❖ It can read any type of data.
❖ It can read multiple values.
❖ It stands for scan formatted.
❖ It is a general input function.
Syntax :
scanf(“Format specifiers”,&variableName);
Example : WAP to read rno,sname, bname. Print them on screen.
#include<stdio.h>
void main()
{
int rno;
char sname[100], bname[100];
printf("Enter rno, sname, bname : ");
scanf("%d%s%s",&rno,&sname,&bname);
printf("Rno=%d",rno);
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
9 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("\nSname=%s",sname);
printf("\nBname=%s",bname);
}
Output :
Enter rno, sname, bname :
410
Sarath
ECE
Rno=410
Sname=Sarath
Bname=ECE
Printf() :-
❖ It stands for print formatted.
❖ It is used to print any type of data.
❖ It can print multiple values.
❖ It is general output function.
Syntax :
printf(“Format specified”,variableList)
Example Program
1. WAP to read length, width of a rectangle. Print the area.
#include<stdio.h>
void main()
{
int l,w,a;
printf("Enter length, width : ");
scanf("%d%d",&l,&w);
a=l*w;
printf("Area=%d",a);
}
Output :
Enter length, width : 100
50
Area=5000
Output :
Enter base, height : 100
50
Area=2500.00
Operators :
Operators :- Operators are the tools those are used to perform operations.
Operands :- it may be a constant or variable or both.
Types of operators :-
==================
C provides 9 types of operators.
1. Arithmetic operators.
2. Logical operators
3. Relational operators
4. Bitwise operators
5. Assignment operators
6. Increment or decrement operators.
7. Compound operators
8. Conditional operators.
9. Special operator
1. Arithmetic operators :
--------------------------------
The operators those are used to evaluate the arithmetic expression are
called as arithmetic operators.
Examples :
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
11 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
+, -, *, /, %( mod → it gives remainder )
a. WAP to read a number. Find the last digit of given number.
#include<stdio.h>
void main()
{
int no,rem;
printf("Enter a number : ");
scanf("%d",&no);
rem=no%10;
printf("Last digit = %d",rem);
}
Output:
Enter a number : 127
Last digit = 7
#include<stdio.h>
void main()
{
int no,rem;
printf("Enter a number : ");
scanf("%d",&no);
rem=no%1000;
printf("Last 3 digits = %d",rem);
}
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
12 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Output:
Enter a number : 1247
Last 3 digits = 417
#include<stdio.h>
void main()
{
int no;
printf("enter a number : ");
scanf("%d",&no);
no=no/100;
printf("Number after remove the last 2 digits=%d",no);
}
Output:
enter a number : 1234
Number after remove the last 2 digits=12
#include<stdio.h>
void main()
{
int no;
printf("enter a number : ");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
13 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
scanf("%d",&no);
no=no/1000;
printf("Number after remove the last 3 digits=%d",no);
}
Output:
enter a number : 1234
Number after remove the last digit=1
2. Logical operators :
----------------------------
❖ The operators those works on Boolean values( True and False ).
❖ These gives Boolean value( True and False ) as a result.
❖ These are used to join the more than one condition.
Examples :
&& ( and ), || (or), ! (Not)
Truth table of OR
Exp1 Exp2 Res
T T T
T F T
F T T
F F F
4. Bitwise operators :-
--------------------------------
❖ These works only on numbers.
❖ It works on bits of operand.
Examples :
<< ➔ Left shift operator
>> ➔ right shift operator
& ➔ bitwise and operator
| ➔ bitwise or operator
^ ➔ bitwise Exclusive OR operator
~ ➔ bitwise negation operator
| (Bitwise OR operator ) :-
---------------------------------------
❖ It works on 2 operands
❖ It test 2 bits of 2 operands.
❖ It gives 1 when eight one bit is one.
❖ Otherwise it gives 0.
5. Assignment operators :-
------------------------------------
It is used to assign the result to a variable.
It gives the result of right hand side expression to left hand side variable.
Example:
=
Explanations :
R=10
R=5+6
6. Compound operator :-
------------------------------------
❖ It is a combination of assignment operator and arithmetic
operator.
❖ It is also called as short hand operator.
❖ Example :
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
17 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
❖ +=, -=, *=, /=, %=
Example :
A=10
A=A+5 ➔ A+=5
A=A-5 ➔ A-=5
A=A*2 ➔ A*=2
A=A/2 ➔ a/=2
A=A%2 ➔ a%=2
++ :-
------------------------
❖ It is an increment operator.
❖ It increases the value of operand by 1.
❖ It can be used both the sides of operand.
❖ If you use ++ before the operand then it is called pre
increment .
❖ If you use ++ after the operand then it is called post
increment.
Example 1 :
Int a=10;
a++;
output : a=11
Example 2 :
Int a=10
++a;
Output : a=11
Example3 :
Int a=10,b;
b=++a;
output : a=11, b=11
Example 4 :
Int a=10,b;
b=a++;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
18 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
output : b=10, a=11
-- :-
----------------------------------
❖ It is decrement operator.
❖ It is used to decrease the value by 1.
❖ It can be used both the sides of operands.
❖ If you use -- before the operand then it is called pre decrement.
❖ If you use -- after the operand then it is called post decrement.
Example 1 :
A=10
--a;
Output : a=9
Example 2 :
A=10
a--;
output : a=9
example 3:
a=10,b;
b=a--;
output : b=10, a=9
example 4:
int a=10,b;
b=--a;
output : a=9, b=9
- :
-----------------------------------------
❖ It is called both increment and decrement operator.
❖ It can increase or decrease the value of operand.
Example 1:
Int a=10;
-a=-1*a
➔-1*10
➔ -10
Example 2 :
Int a=-10;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
19 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
-a = -1*a
➔ -1 * -10
➔ +10
8. Conditional operators :-
-------------------------------------
❖ The operators those works on given condition.
❖ It is also called ternary operator.
❖ It works on more than 2 operands.
❖ Example :
❖ ?, :
Syntax :
Condition ? value if true : value if false;
Syntax :
Expression1 relationalOperator expression2 ? Expression1 :
expression2
Example :
Big = a > b ? a : b
Output :
Enter 2 numbers : 100
50
Big number=100
Output :
Enter 3 numbers : 100
50
175
Big number=175
9. Special operators :-
--------------------------------------
C language provides 2 types of special operators.
a. Sizeof()
b. Comma( , )
Sizeof() :-
------------------
❖ It gives the size of operand.
Example :
#include<stdio.h>
void main()
{
printf("Size of char =%d",sizeof(char));
printf("\nSize of int =%d",sizeof(int));
printf("\nSize of float =%d",sizeof(float));
printf("\nsize of double =%d",sizeof(double));
}
Output :
Size of char =1
Size of int =4
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
21 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Size of float =4
size of double =8
camma ( , ) :-
-------------------------
Camma is a list separator
Example 1 :
#include<stdio.h>
void main()
{
int a;
a=10,20,30;
printf( "a=%d",a);
}
Output :
a=10
Example 2 :
❖ If we use comma inside the brackets then it acts as a special
operator.
❖ It evaluates all the expressions and gives the result of right
most expression.
Example Programs
#include<stdio.h>
void main()
{
int a;
a=(10,20,30);
printf( "a=%d",a);
}
Output :
a=30
example 2 :
#include<stdio.h>
void main()
{
int a;
a=(5+2,10/2,3+1);
printf( "a=%d",a);
}
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
22 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Output :
a=4
Example programs List
================================================================
===
1. WAP to read radius of a circle. Print the area.
#include<stdio.h>
#define PI 3.14
void main()
{
int r;
float a;
printf("Enter radius : ");
scanf("%d",&r);
a=PI*r*r;
printf("Area=%f",a);
}
Output :
Enter radius : 10
Area=314.000000
#include<stdio.h>
#define PI 3.14
void main()
{
int p,t,r,si;
printf("Enter p,t,r values : ");
scanf("%d%d%d",&p,&t,&r);
si=p*t*r/100;
printf("Simple interest=%d",si);
}
Output :
Enter p,t,r values :
10000
12
2
Simple interest=2400
#include<stdio.h>
#define PI 3.14
void main()
{
int basic, hra, da, pf, net;
printf("Enter basic, hra, da, pf : ");
scanf("%d%d%d%d",&basic, &hra, &da, &pf);
net=basic + hra + da - pf;
printf("Net salary=%d",net);
}
Output :
Enter basic, hra, da, pf : 25000
5000
3000
2000
Net salary=31000
#include<stdio.h>
#define PI 3.14
void main()
{
int a,b,t;
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
t=a;
a=b;
b=t;
printf("a=%d",a);
printf("\nb=%d",b);
}
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
24 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Output :
Enter 2 numbers :
100
200
a=200
b=100
#include<stdio.h>
#define PI 3.14
void main()
{
int a,b;
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("a=%d",a);
printf("\nb=%d",b);
}
Output :
Enter 2 numbers :
10
20
a=20
b=10
#include<stdio.h>
#define PI 3.14
void main()
{
a=(a+b)-(b=a);
printf("a=%d",a);
printf("\nb=%d",b);
}
Output :
Enter 2 numbers :
10
20
a=20
b=10
explanation :
a=10
b=20
a=(a+b)-(b=a);
a=(10+20)-(b=a)
a=30-(b=10)
::::: b=10 :::
a=30-10
:::: a=20 ::::
#include<stdio.h>
void main()
{
char ch;
printf("Enter a lower case char : ");
scanf(" %c",&ch);
ch=ch-32;
printf("Upper case char=%c",ch);
}
output :
Enter a lower case char : b
Upper case char=B
#include<stdio.h>
void main()
{
char ch;
printf("Enter a upper case char : ");
scanf(" %c",&ch);
ch=ch+32;
printf("Lower case char=%c",ch);
}
Output :
Enter a upper case char : D
lower case char=d
9. WAP to find the sum of N natural numbers without using any loop.
#include <stdio.h>
int main ()
{
int n,sum;
printf("Enter N value : ");
scanf("%d",&n);
sum=(n*(n+1))/2;
printf("Sum=%d",sum);
return 0;
}
Output:
Enter N value : 10
Sum=55
10. WAP to find big number between 2 numbers without using if-else or
without using
conditional statement.
#include <stdio.h>
int main ()
{
int a,b,big;
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
big=((a+b)+abs(a-b))/2;
printf("Big number=%d",big);
return 0;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
27 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
}
Output:
Enter 2 numbers : 10 50
Big number=50
Explanation :
A=10
B=50
Big=((a+b)+abs(a-b))/2;
Big=((10+50)+abs(10-50))/2
Big=( 60 + abs( -40 ))/2
Big=(60 + 40 )/2
Big= 100/2
Big=50
11. WAP to find small number between 2 numbers without using if-else or
without using
conditional statement.
#include <stdio.h>
int main ()
{
int a,b,small;
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
small=((a+b)-abs(a-b))/2;
printf("small number=%d",small);
return 0;
}
Output:
Enter 2 numbers : 10 50
Small number=10
Explanation :
A=10
B=50
small =((a+b)-abs(a-b))/2;
small =((10+50)-abs(10-50))/2
small =( 60 - abs( -40 ))/2
small =(60 - 40 )/2
small = 20/2
small=10
Control Statements
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
28 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
If :-
--------------------
❖ It is a control statement.
❖ It is a conditional statement.
❖ It is a selective statement
❖ It is a branching statement.
❖ It works based on given condition.
❖ Syntax :
if( condition )
{
body if true
}
else
{
body if false
}
Output1 :
Enter a number : 10
Even number
Output2:
Enter a number : 17
Odd number
No/2*2==no
8/2*2==8 ➔ True
7/2*2==7 ➔ False
#include<stdio.h>
void main()
{
int no;
printf("Enter a number : ");
scanf("%d",&no);
if( no/2*2==no )
printf("Even number ");
else
printf("Odd number");
}
Output 1:
Enter a number : 15
Odd number
Output2 :
Enter a number : 20
Even number
#include<stdio.h>
void main()
{
int no;
printf("Enter a number : ");
scanf("%d",&no);
if( no<0 )
printf("-ve number");
else
printf("+ve number");
}
Output 1 :
Enter a number : 10
+ve number
Output 2 :
Enter a number : -5
-ve number
#include<stdio.h>
void main()
{
int no;
printf("enter a number : ");
scanf("%d",&no);
if( no==abs(no))
printf("+ve number ");
else
printf("-ve number");
}
Output 1 :
enter a number : 10
+ve number
Output 2 :
enter a number : -7
-ve number
Example 2 :
No=-10
Abs( no ) = 10
5. WAP to read purchase rate, sales rate. Print whether it is profit or loss.
#include<stdio.h>
void main()
{
int pr,sr;
printf("Enter pr,sr : ");
scanf("%d%d",&pr,&sr);
if( sr > pr )
printf("Profit=%d",sr-pr);
else
printf("Loss=%d",pr-sr);
}
Output 1:
Enter pr,sr :
150
80
Loss=70
Output 2:
Enter pr,sr : 200
370
Profit=170
#include<stdio.h>
void main()
{
char ch;
printf("Enter a char : ");
scanf(" %c",&ch);
if( ch>='a' && ch<='z' )
printf("Lower case ");
else
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
32 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("Not a lower case");
}
Output 1 :
Enter a char : a
lower case
output 2 :
Enter a char : A
Not a lower case.
Output3:
Enter a char : +
Not a lower case
#include<stdio.h>
void main()
{
char ch;
printf("Enter a char : ");
scanf(" %c",&ch);
if( ch>='A' && ch<='Z' )
printf("Upper case ");
else
printf("Not a Upper case");
}
Output 1 :
Enter a char : a
Not a upper case
output 2 :
Enter a char : A
Upper case.
Output3:
Enter a char : +
Not a upper case
Output 1 :
Enter a digit : 5
Digit
output 2 :
Enter a digit : A
Not a digit.
#include<stdio.h>
void main()
{
int a,b;
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
if( a==b )
printf("Both are equal");
else
printf("Both are not equal");
}
Output1 :
Enter 2 numbers : 10
20
Both are not equal
Output2 :
Enter 2 numbers : 20
20
Both are equal
10. WAP to read 2 numbers. Print whether they are equal or not without
using == operator.
#include<stdio.h>
void main()
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
34 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
int a,b;
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
if( a-b )
printf("Both are not equal");
else
printf("Both are equal");
}
Output1 :
Enter 2 numbers : 10
20
Both are not equal
Explanation :
If( a-b)
If( 10-20)
If( -10 )
If( True )
Output2 :
Enter 2 numbers : 20
20
Both are equal
Explanation :
If( a-b)
If( 20-20)
If( 0 )
If( False )
11. WAP to read marks in 3 subjects. Print whether the student is pass or fail.
#include<stdio.h>
void main()
{
int m1,m2,m3;
printf("Enter marks in 3 subjects : ");
scanf("%d%d%d",&m1,&m2,&m3);
if( m1>=35 && m2>=35 && m3>=35 )
printf("Pass");
else
printf("Fail");
}
Output1:
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
35 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Enter marks in 3 subjects : 90 80 70
Pass.
Output2:
Enter marks in 3 subjects : 40 50 20
Fail
.
Output1 :
Enter a char : a
Vowel
Output2 :
Enter a char : b
Consonant
13. WAP to read 3 sides of a triangle. Print whether we can draw the triangle
or not.
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter 3 sides of a triangle : ");
scanf("%d%d%d",&a,&b,&c);
if( a+b>c && b+c>a && a+c>b )
printf("We can draw the triangle");
else
printf("We can not draw the triangle");
}
14. WAP to read 3 angles of a triangle. Print whether it is right angle triangle
or not.
#include<stdio.h>
void main()
{
int a1,a2,a3;
printf("Enter 3 angles of a triangle : ");
scanf("%d%d%d",&a1,&a2,&a3);
if( a1==90 || a2==90 || a3==90 )
printf("Yes it is a right angle triangle");
else
printf("No it is not a right angle triangle");
}
Output1 :
Enter 3 angles of a triangle : 90
50
40
Yes it is a right angle triangle
Output2 :
Enter 3 angles of a triangle : 50 60 70
No it is not a right angle triangle
15. WAP to read 3 angles of a triangle. Print whether the angles are valid or
not.
#include<stdio.h>
void main()
{
int a1,a2,a3;
printf("Enter 3 angles of a triangle : ");
scanf("%d%d%d",&a1,&a2,&a3);
if( a1+a2+a3==180 )
printf("Valid angles");
else
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
37 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("Invalid angles");
}
Output1 :
Enter 3 angles of a triangle : 50 60 70
Valid angles
Output 2:
Enter 3 angles of a triangle : 10 20 30
Invalid angles
#include<stdio.h>
void main()
{
int year;
printf("Enter year : " );
scanf("%d",&year);
if( (year%4==0 && year%100!=0)|| year%400==0 )
printf("Leap year");
else
printf("Not a leap year");
}
output : 1:.
Enter year : 100
Not a leap year
Output 2:
Enter year : 400
Leap year
17. WAP to read age of a person. Print whether the person is eligible to vote
or not.
#include<stdio.h>
void main()
{
int age;
printf(“enter age : “);
scanf(“%d”,&age);
if( age>=21 )
printf(“eligible to vote”);
else
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
38 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf(“not eligible to vote”);
18. WAP to read 2 numbers. Print whether the second number is factor to
first number or not.
#include<stdio.h>
void main()
{
int a,b;
printf(“enter 2 numbers : “);
scanf(“%d%d”,&a,&b);
if( a%b==0 )
printf(“b is factor to a”);
else
printf(“b is not a factor to a”)
}
Output:
Enter 2 numbers : 10 5
b is factor to a
output 2:
Enter 2 numbers : 10 3
b is not a factor to a
19. WAP to demonstrate null else.
#include<stdio.h>
void main()
{
char ch;
printf("Enter a lower case char : ");
scanf(" %c",&ch);
if( ch>='a' && ch<='z' )
ch=ch-32;
else
;
printf("Upper case char=%c",ch);
}
Output 1 :
Enter a lower case char : a
A
Output2 :
Enter a lower case char : A
A
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
39 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Output3:
Enter a lower case char : 5
5
Output4:
Enter a lower case char : +
+
#include<stdio.h>
void main()
{
char ch;
printf("Enter a upper case char : ");
scanf(" %c",&ch);
if( ch>='a' && ch<='z' )
;
else
ch=ch+32;
printf("Lower case char=%c",ch);
}
Output 1 :
Enter a upper case char : T
Lower case char=t
Output2 :
Enter a upper case char : b
Lower case char=b
If else ladder / Stair case if / if else – if else –if
Syntax :
if( condition )
{
body
}
else if( condition )
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
40 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
{
body;
}
…..
else
{
body;
}
Output 1 :
Enter temp of water : -20
ICE
Output2 :
Enter temp of water : 25
Water
Output 3:
Enter temp of water : 125
Steam
#include<stdio.h>
void main()
{
int avg;
printf("Enter avg marks : ");
scanf("%d",&avg);
if( avg>=60 )
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
41 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("I Class");
else if( avg>=50 )
printf("II Class");
else
printf("III Class");
}
Output1 :
Enter avg marks : 90
I Class
Output 2:
Enter avg marks : 55
II Class
Output 3 :
Enter avg marks : 45
III Class
23. WAP to read sales done by a sales man. Print the commission based on
following table.
Sales Commission
<5000 0
<10000 200
Else 400
#include<stdio.h>
void main()
{
int sales;
printf("Enter sales : ");
scanf("%d",&sales);
if( sales<5000 )
printf("Commission = 0");
else if( sales<10000 )
printf("Commission = 200");
else
printf("Commission = 400");
}
Output1 :
Enter sales : 4999
Commission = 0
Output2 :
24. WAP to read power units consumed. Find the bill amount.
Units Rate/unit
<100 2.52
<200 3.75
Else 5.10
#include<stdio.h>
void main()
{
int units;
float bill;
printf("Enter power units consumed : ");
scanf("%d",&units);
if( units<100 )
bill=units*2.52;
else if( units<200 )
bill=99*2.52+(units-99)*3.75;
else
bill=99*2.52+100*3.75+(units-199)*5.10;
printf("Bill amount=%.2f",bill);
}
Output1 :
Enter power units consumed : 50
Bill amount=126.00
Output 2:
Enter power units consumed : 125
Bill amount=346.98
Output3 :
Enter power units consumed : 250
Bill amount=884.58
25. WAP to read age of a person print the message based on following table.
Age Message
<3 Infants
<16 Kids
void main()
{
int age;
printf("Enter age : ");
scanf("%d",&age);
if( age<3 )
printf("Infants ");
else if( age<16 )
printf("Kids");
else if( age<21 )
printf("Teenagers");
else if( age<28 )
printf("Youngster");
else if( age<40 )
printf("Middle agers");
else
printf("Old agers");
}
Output:
Enter age : 20
Teenagers
Nested if :-
----------------------================-
A if statement within another if statement is called nested if statement.
First outer if condition tested, if it is true then only inner if condition will
be tested. If both the conditions satisfied then only the body will be
executed.
26. WAP read age, gender of a person. Print whether the person is major or
minor.
Gender Age Message
<21 Minor
M
<18 Minor
F
else major
#include<stdio.h>
void main()
{
char gender;
int age;
printf("Enter age, gender of a person : ");
scanf("%d %c",&age,&gender);
if( gender=='m' || gender=='M')
{
if( age>=21 )
printf("He is major");
else
printf("He is minor");
}
else
{
if( age>=18 )
printf("She is major");
else
printf("She is minor");
}
}
Output 1 :
Enter age, gender of a person : 20
m
He is minor
Output2 :
Enter age, gender of a person : 20
f
She is major
27. WAP to read marks in 3 subjects. Print the grade.
#include<stdio.h>
void main()
{
int m1,m2,m3,avg;
printf("Enter 3 subjects marks : ");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
45 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
scanf("%d%d%d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
if( m1>=35 && m2>=35 && m3>=35 )
{
if( avg>=60 )
printf("I Class");
else if( avg>=50 )
printf("II Class");
else
printf("III Class");
}
else
printf("Fail");
}
Output1 :
Enter 3 subjects marks :
100
100
20
Fail
Output 2:
Enter 3 subjects marks :
35
35
35
III Class
Switch :-
----------------------------------
❖ It is a control statement.
❖ It is a decision making statement.
❖ It is a conditional statement.
❖ It is multi way selection statement.
❖ It is a branching statement.
❖ It is used when we need to select a single choice from group of choices.
Syntax :
switch ( expression )
{
case constant1: body; break;
case constant2: body; break;
case constant3: body; break;
…
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
46 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
case constantN : body; break;
default: body;
}
Note :
❖ Break is optional.
❖ Body is optional.
❖ Cases also optional.
❖ Default also optional.
❖ Order of case is user choice.
❖ Place of default is user choice.
❖ Case should be a constant/constant expression.
❖ Cases should not be duplicated.
❖ Switch allows to pass only char or only integer as input.
#include<stdio.h>
void main()
{
int no;
printf("enter a single digit number : ");
scanf("%d",&no);
switch( no )
{
case 0: printf("Zero"); break;
case 1: printf("One"); break;
case 2: printf("Two"); break;
case 3: printf("Three"); break;
case 4: printf("Four"); break;
case 5: printf("Five"); break;
case 6: printf("Six"); break;
case 7: printf("Seven"); break;
case 8: printf("Eight"); break;
case 9: printf("Nine"); break;
default: printf("Invalid input");
}
}
Output1 :
Enter a single digit number : 5
Five
Output2 :
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
47 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Enter a single digit number : 2
Two
Output3:
Enter a single digit number : 12
Invalid input
#include<stdio.h>
void main()
{
int no;
printf("enter a month number : ");
scanf("%d",&no);
switch( no )
{
case 1: printf("JAN"); break;
case 2: printf("FEB"); break;
case 3: printf("MAR"); break;
case 4: printf("APR"); break;
case 5: printf("MAY"); break;
case 6: printf("JUN"); break;
case 7: printf("JUL"); break;
case 8: printf("AUG"); break;
case 9: printf("SEP"); break;
case 10: printf("OCT"); break;
case 11: printf("NOV"); break;
case 12: printf("DEC"); break;
default : printf("Invalid input");
}
}
Output1 :
Enter a month number : 2
FEB
OUTPUT 2:
Enter a month number : 12
DEC
Output3:
Enter a month number : 16
Invalid input
Output 1 :
Enter a week number : 2
Mon
Output 2 :
Enter a week number : 1
Sun
Output 3 :
Enter a week number : 10
Invalid input.
#include<stdio.h>
void main()
{
char ch;
printf("Enter a char : ");
scanf(" %c",&ch);
switch( ch )
{
case 'a': printf("Vowel"); break;
case 'e': printf("Vowel"); break;
case 'i': printf("Vowel"); break;
case 'o': printf("Vowel"); break;
case 'u': printf("Vowel"); break;
case 'A': printf("Vowel"); break;
case 'E': printf("Vowel"); break;
case 'I': printf("Vowel"); break;
case 'O': printf("Vowel"); break;
case 'U': printf("Vowel"); break;
default : printf("Consonants");
}
}
Output :
Enter a character : a
Vowel
Output 2 :
Enter a character : b
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
50 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Consonant
Or :
#include<stdio.h>
void main()
{
char ch;
printf("Enter a char : ");
scanf(" %c",&ch);
switch( ch )
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': printf("Vowel"); break;
default : printf("Consonants");
}
}
#include<stdio.h>
void main()
{
int no;
printf("Enter a month number : ");
scanf("%d",&no);
switch( no )
{
case 3:
case 4:
case 5:
case 6: printf("Summer"); break;
case 7:
case 8:
case 9:
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
51 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
case 10: printf("Rainy"); break;
case 11:
case 12:
case 1:
case 2: printf("Winter"); break;
defualt: printf("Invalid input ");
}
}
Output 1 :
Enter a month number : 3
Summer
Output 2 :
Enter a month number : 7
Rainy
Outpu3 :
Enter a month number : 1
Winter
7. WAP to read a single digit number. Print whether it is odd or even using
switch case.
#include<stdio.h>
void main()
{
int no;
printf("Enter a single digit number : ");
scanf("%d",&no);
switch( no)
{
case 1:
case 3:
case 5:
case 7:
case 9: printf("Odd number"); break;
case 0:
case 2:
case 4:
case 6:
case 8:printf("Even number"); break;
default : printf("Invalid input");
}
}
While :-
-------------------
❖ It is a control statement
❖ It is a conditional statement.
❖ It is an iterative tool.
❖ It is a pre test loop.
❖ It is used to execute the code repeatedly.
Syntax :
while( condition )
{
Body;
}
1. WAP to print your name 5 times.
#include<stdio.h>
void main()
{
int i=1;
while( i<=5 )
{
printf("\nChina Saheb");
i++;
}
}
Tracing:
I
==================
1
2
3
4
5
6
Output :
China Saheb
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
53 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
China Saheb
China Saheb
China Saheb
China Saheb
#include<stdio.h>
void main()
{
int no=1;
while( no<=100 )
{
printf("%5d",no);
no+=2;
}
}
Output :
1 3 5 7 9 … 99
#include<stdio.h>
void main()
{
int no=1;
while( no<=100 )
{
if( no%7==0 || no%11==0 )
printf("%6d",no);
no++;
}
}
Output :
7 11 14 21 22 28 33 35 96 99
#include<stdio.h>
void main()
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
55 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
int no=1;
while( no<=10 )
{
printf("%5d",no*no);
no++;
}
}
#include<stdio.h>
void main()
{
int i=1,j=5;
while( i<=5 )
{
printf("%5d%5d",i,j);
i++;
j--;
}
}
9. WAP to print Fibonacci series.
0 1 1 2 3 5 8 13 21 34
#include<stdio.h>
void main()
{
int a=0,b=1,c,i=3;
printf("%5d%5d",a,b);
while( i<=10 )
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
56 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
c=a+b;
printf("%5d",c);
i++;
a=b;
b=c;
}
}
#include<stdio.h>
void main()
{
int no;
printf("Enter a number : ");
scanf("%d",&no);
printf("%5d",no);
while( no!=1 )
{
if( no%2==0 )
no=no/2;
else
no=no*3+1;
printf("%5d",no);
}
}
Output :
Enter a number : 22
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
#include<stdio.h>
void main()
{
Output :
Enter 2 numbers : 20 20
Sum=40
#include<stdio.h>
void main()
{
int a, b;
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
while( b!=0 )
{
a--;
b--;
}
printf("Sub=%d",a);
}
Output :
Enter 2 numbers : 10 3
Sub=7
#include<stdio.h>
void main()
{
int a,b,r=0;
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
while( b!=0 )
{
r=r+a;
b--;
}
printf("Result=%d",r);
}
Output :
Enter 2 numbers : 10 5
Result=50
#include<stdio.h>
void main()
{
int a,b,r=0;
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
59 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
while( a>=b )
{
r++;
a=a-b;
}
printf("Result=%d",r);
}
Output :
Enter 2 numbers : 21 5
Result=4
#include<stdio.h>
void main()
{
int a,b,r=0;
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
while( a!=0 )
{
if( a%2==1 )
r=r+b;
a=a/2;
b=b*2;
}
printf("Result=%d",r);
}
Output :
Enter 2 numbers :
15
No Rem Rev
125 5 0
12 2 5
1 1 52
0 521
#include<stdio.h>
void main()
{
int no,rem,rev=0;
printf("Enter a number : ");
scanf("%d",&no);
while( no!=0 )
{
rem=no%10;
rev=rev*10+rem;
no=no/10;
}
printf("Reverse=%d",rev);
}
Output:
Enter a number : 1234
Reverse=4321
#include<stdio.h>
void main()
{
int no,rem,rev=0,dup;
printf("Enter a number : ");
#include<stdio.h>
void main()
{
int no,rem,sum=0,dup;
printf("Enter a number : ");
scanf("%d",&no);
dup=no;
while( no!=0 )
{
rem=no%10;
sum=sum+rem*rem*rem;
no=no/10;
}
19. WAP to read a number. Find first and last digits of a number.
No Fd ld
1527 7 7
152 2
15 5
1 1
#include<stdio.h>
void main()
{
int no,fd,ld;
printf("Enter a number : ");
scanf("%d",&no);
ld=no%10;
while( no!=0 )
{
fd=no%10;
no=no/10;
}
printf("First digit=%d",fd);
printf("\nLast digit=%d",ld);
}
Output:
Enter a number : 1527
First digit : 1
Last digit : 7
Note :
❖ If there are more than one initialization then we have to
separate them by camma.
❖ If there are multiple increments or decrements then we have
to separate them by camma.
❖ If there are multiple conditions then we have to combine them
by logical operators.
Example 1 :
for( i=1 , j=5; i<=5 && j>=1 ; i++ , j--)
{
Body;
}
❖ If there is no initialization then we can omit that part.
❖ If there is not condition then we can omit that part.
❖ If there is no increment or decrements then we can omit that
part.
Example :
For( ; ; )
{
}
❖ If there is no body then we have to terminate with semi colon.
Example :
for( i=1;i<=10;i++) ;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
65 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
#include<stdio.h>
void main()
{
int no,c=0,i;
printf("Enter a number : ");
scanf("%d",&no);
for( i=1;i<=no;i++ )
{
if( no%i==0 )
c++;
}
if( c==2 )
printf("Prime number ");
else
printf("Not a prime number");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
66 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
}
Output 1:
Enter a number : 5
Prime number
Output 2 :
Enter a number : 6
Not a prime number
5!= 120
6!= 720
7!= 5040
8!= 40320
#include<stdio.h>
void main()
{
int no,i;
long int f;
printf("Enter a number : ");
scanf("%d",&no);
for( i=1,f=1;i<=no;i++ )
f=f*i;
printf("Factorial=%ld",f);
}
Output :
Enter a number : 4
Factorial=24
#include<stdio.h>
void main()
{
int no,i,sum=0;
printf("enter a number : ");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
67 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
scanf("%d",&no);
for( i=1; i<no;i++ )
{
if( no%i==0 )
sum=sum+i;
}
if( sum==no )
printf("Perfect number");
else
printf("Not a perfect number");
}
Output 1:
enter a number : 28
Perfect number
Output 2 :
enter a number : 5
Not a perfect number
#include<stdio.h>
void main()
{
long int no,rem,f,i,sum=0,dup;
printf("Enter a number : ");
scanf("%d",&no);
for( dup=no; no!=0 ; no=no/10 )
{
rem=no%10;
for( i=1,f=1;i<=rem;i++)
f=f*i;
sum=sum+f;
}
if( sum==dup )
printf("Strong number ");
#include<stdio.h>
void main()
{
int g,h;
for( g=1,h=29;g<=30;g++,h--)
if( g*4+h*2==84 )
break;
printf("Goats=%d",g);
printf("\nHens=%d",h);
}
Output :
Goats=12
Hens=18
#include<stdio.h>
void main()
{
int r,w;
for( r=1,w=19;r<=20;r++,w--)
if( r*3+w*-1==40 )
break;
printf("Right Answers=%d",r);
printf("\nWrong Answers=%d",w);
}
Output :
Right Answers=15
Wrong Answers=5
#include<stdio.h>
void main()
{
int n;
for( n=1;n<=700; n++ )
if( n*1+n*0.5+n*0.25==700 )
break;
printf("1 Rupee coins=%d",n);
printf("\n50 paise coins=%d",n);
printf("\n25 paise coins=%d",n);
}
Output :-
1 Rupee coins=400
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
70 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
50 paise coins=400
25 paise coins=400
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++)
{
for( j=1;j<=i;j++)
printf("* ");
printf("\n");
}
}
#include<stdio.h>
void main()
{
int i,j;
for( i=5;i>=1;i-- )
{
for( j=1;j<=i;j++)
printf("* ");
printf("\n");
}
}
#include<stdio.h>
void main()
{
int i,j,s=4;
for( i=1;i<=5;i++ )
{
for( j=1;j<=s;j++)
printf(" ");
for( j=1;j<=i;j++ )
printf("* ");
printf("\n");
s--;
}
}
12. WAP to print following pattern.
#include<stdio.h>
void main()
{
int i,j,s=0;
for( i=5;i>=1;i--)
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=i;j++)
printf("* ");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
72 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("\n");
s++;
}
}
#include<stdio.h>
void main()
{
int i,j,s=4;
for( i=1;i<=5;i++ )
{
for( j=1;j<=s;j++)
printf(" ");
for( j=1;j<=2*i-1;j++)
printf("* ");
printf("\n");
s--;
}
}
#include<stdio.h>
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
73 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
void main()
{
int i,j,s=0;
for( i=5;i>=1;i-- )
{
for(j=1;j<=s;j++)
printf(" ");
for( j=1;j<=2*i-1;j++)
printf("* ");
printf("\n");
s++;
}
}
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=i;j++ )
{
if( j==1 || j==i || i==5 )
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
#include<stdio.h>
void main()
{
int i,j;
for( i=5;i>=1;i-- )
{
for( j=1;j<=i;j++ )
{
if( j==1 || j==i || i==5 )
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
17. WAP to print following pattern.
#include<stdio.h>
void main()
{
int i,j,s=0;
for( i=1;i<=5;i++ )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=5;j++ )
printf("* ");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
75 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("\n");
s++;
}
}
#include<stdio.h>
void main()
{
int i,j,s=0;
for( i=1;i<=5;i++ )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=5;j++ )
{
if( j==1 || j==5 || i==1 || i==5 )
printf("* ");
else
printf(" ");
}
printf("\n");
s++;
}
}
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=i;j++ )
printf("* ");
printf("\n");
}
for( i=4;i>=1;i-- )
{
for( j=1;j<=i;j++ )
printf("* ");
printf("\n");
}
}
#include<stdio.h>
void main()
{
int i,j,s=4;
for( i=1;i<=5;i++ )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=2*i-1;j++ )
printf("* ");
printf("\n");
s--;
}
s=1;
for( i=4;i>=1;i-- )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=2*i-1;j++)
printf("* ");
printf("\n");
s++;
}
}
#include<stdio.h>
void main()
{
int i,j,s=4;
for( i=1;i<=5;i++ )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=2*i-1;j++ )
{
if( j==1 || j==2*i-1 )
printf("* ");
else
printf(" ");
}
printf("\n");
s--;
}
s=1;
for( i=4;i>=1;i-- )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=2*i-1;j++)
{
if( j==1 || j==2*i-1)
printf("* ");
else
printf(" ");
}
printf("\n");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
79 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
s++;
}
}
22. WAP to print following pattern.
#include<stdio.h>
void main()
{
int i,j,s=0;
for( i=5;i>=1;i-- )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=2*i-1;j++ )
printf("* ");
printf("\n");
s++;
}
s=3;
for( i=2;i<=5;i++ )
{
for( j=1;j<=s;j++)
printf(" ");
for( j=1;j<=2*i-1;j++)
printf("* ");
printf("\n");
s--;
}
}
23. WAP to print following pattern.
#include<stdio.h>
void main()
{
int i,j,s=0;
for( i=5;i>=1;i-- )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=2*i-1;j++ )
{
if( j==1 || j==2*i-1 || i==5)
printf("* ");
else
printf(" ");
}
printf("\n");
s++;
}
s=3;
for( i=2;i<=5;i++ )
{
for( j=1;j<=s;j++)
printf(" ");
for( j=1;j<=2*i-1;j++)
{
if( j==1 || j==2*i-1 || i==5)
printf("* ");
else
printf(" ");
}
printf("\n");
s--;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
81 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
}
}
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=5;j++ )
{
if( i==1 || j==3 )
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
#include<stdio.h>
void main()
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
82 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=5;j++ )
{
if( i==1 || i==3 || i==5 || j==1 )
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=5;j++ )
{
if( j==1 || j==5 || i==j )
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
27. WAP to print following.
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=5;j++ )
{
if( i==1 || i==5 || i+j==6 )
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
28. WAP to print following.
#include<stdio.h>
void main()
{
int i,j;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
84 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
for( i=1;i<=5;i++ )
{
for( j=1;j<=5;j++ )
{
if( i==j || i+j==6 )
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=5;j++ )
{
if( i==1 || j==3 || ( i==5 && j<=3 )||( j==1 && i>=3 ))
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
30. WAP to print following.
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
85 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=5;j++ )
{
if( i==1 || i==5 || j==1 || (j==5 && i>=3) || ( i==3 && j>=3))
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
#include<stdio.h>
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
86 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=5;j++)
{
if( j==1 || j==5 || (i==j && j<=3) || (i+j==6 && j>=3))
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=5;j++)
{
if( j==1 || j==5 || (i+j==6 && i>=3) || (i==j && i>=3))
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
33. WAP to print following.
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
87 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=5;j++ )
{
if(i==3||j==3||(j==1&&i<=3)||(j==5&&i>=3)||(i==1&&j>=3)||(i==5&&j<=3))
printf("* ");
else
printf(" ");
}
printf("\n");
}
}
#include<stdio.h>
void main()
{ int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=i;j++)
printf("%d ",i);
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
88 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("\n");
}
}
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=i;j++ )
printf("%d ",j);
printf("\n");
}
}
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++)
{
for( j=i;j>=1;j-- )
#include<stdio.h>
void main()
{
int i,j,s=4;
for( i=1;i<=5;i++ )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=i;j++ )
printf("%d ",j);
for( j=i-1;j>=1;j-- )
printf("%d ",j);
printf("\n");
s--;
}
}
#include<stdio.h>
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
90 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
void main()
{
int i,j;
for( i=1;i<=5;i++ )
{
for( j=1;j<=i;j++ )
printf("%d ",j);
printf("\n");
}
for( i=4;i>=1;i--)
{
for( j=1;j<=i;j++)
printf("%d ",j);
printf("\n");
}
}
#include<stdio.h>
void main()
{
char ch='A';
int i,j;
for( i=1;i<=5;i++)
{
for( j=1;j<=i;j++ )
printf("%c ",ch);
printf("\n");
ch++;
}
}
#include<stdio.h>
void main()
{
char ch='E';
int i,j;
for( i=5;i>=1;i--)
{
for( j=1;j<=i;j++ )
printf("%c ",ch);
printf("\n");
ch--;
}
}
#include<stdio.h>
void main()
{
char ch;
int i,j;
for( i=1;i<=5;i++ )
{
ch='A';
for( j=1;j<=i;j++ )
{
printf("%c ",ch);
ch++;
}
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
92 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("\n");
}
}
#include<stdio.h>
void main()
{
char ch;
int i,j;
for( i=5;i>=1;i-- )
{
ch='A';
for( j=1;j<=i;j++ )
{
printf("%c ",ch);
ch++;
}
printf("\n");
}
}
#include<stdio.h>
void main()
{
char ch;
int i,j,s=4;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
93 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
for( i=1;i<=5;i++ )
{
ch='A';
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=i;j++ )
{
printf("%c ",ch);
ch++;
}
printf("\n");
s--;
}
}
#include<stdio.h>
void main()
{
char ch='A';
int i,j,s=4;
for( i=1;i<=5;i++ )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=i;j++ )
printf("%c ",ch);
printf("\n");
ch++;
s--;
}
}
#include<stdio.h>
void main()
{
char ch='E';
int i,j,s=0;
for( i=5;i>=1;i-- )
{
for( j=1;j<=s;j++ )
printf(" ");
for( j=1;j<=i;j++ )
printf("%c ",ch);
printf("\n");
s++;
ch--;
}
}
#include<stdio.h>
void main()
{
char ch;
int i,j;
for( i=1;i<=5;i++ )
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
95 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
ch='A';
for( j=1;j<=i;j++ )
{
printf("%c ",ch);
ch++;
}
printf("\n");
}
for( i=4;i>=1;i-- )
{
ch='A';
for( j=1;j<=i;j++)
{
printf("%c ",ch);
ch++;
}
printf("\n");
}
}
Do-While :-
❖ It is a control statement.
❖ It is a conditional statement.
❖ It is an iterative statement.
❖ It is used to execute the code repeated.
❖ It is a post test loop.
❖ The body will execute at least once.
❖ It is used when we don’t know exact count of iteration.
❖ Syntax :
do
{
body;
}while( condition );
Enter a number : 3
Do you want to enter one more number? y
Enter a number : 4
Do you want to enter one more number? y
Enter a number : 5
Do you want to enter one more number? n
Even count=2
Odd count=3
3. WAP to develop EVM application.
#include<stdio.h>
void main()
{
int tc=0,cc=0,pc=0,ch;
do
{
printf("\n1. Taj Mahal");
printf("\n2. China Wall");
printf("\n3. Pyramid");
printf("\n4. Exit");
printf("\n\nEnter your vote : ");
scanf("%d",&ch);
switch( ch )
{
case 1: tc++; break;
case 2: cc++; break;
case 3: pc++; break;
case 4: break;
default: printf("Invalid Vote");
}
}while( ch!=4 );
printf("\nTaj Mahal votes=%d",tc);
printf("\nChina wall votes=%d",cc);
printf("\nPyramid votes=%d",pc);
}
Output: 1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
98 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
Break :-
❖ It is a control statement.
❖ It is an un conditional statement.
❖ It is used to stop the loop at middle.
❖ It can be used only with switch, while, for, do-while.
Example :
#include<stdio.h>
void main()
{
int no;
for(no=1;no<=100;no++)
{
printf("%5d",no);
if( no==10 )
break;
}
}
Output :-
1 2 3 4 5 6 7 8 9 10
Continue :-
❖ It is a control statement.
❖ It is a decision making statement.
❖ It is a un conditional statement.
❖ It is used to skip the rest of the code of current iteration.
❖ It can be used only with while, for, do-while.
❖ Example :
#include<stdio.h>
void main()
{
int no;
for(no=1;no<=100;no++)
{
if( no%2==0 )
continue;
printf("%5d",no);
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
100 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
}
}
Output :
1 3 5 7 9 …. 99
Goto :-
❖ It is a control statement.
❖ It is a un conditional statement.
❖ It is a decision making statement.
❖ It is used to move the control from one location to another location.
❖ It move the cursor to the specified lable.
❖ Label is a name which always ends with “:” symbol.
Example :
#include<stdio.h>
void main()
{
printf("\none");
goto ss;
printf("\ntwo");
printf("\nthree");
ss:
printf("\nfour");
printf("\nfive");
}
Output :
one
four
five
WAP to print 1 to 100 without using loop.
#include<stdio.h>
void main()
{
int no=1;
nrt:
printf("%5d",no);
no++;
if( no<=100)
goto nrt;
}
Output :
1 2 3 4 5 6 … 100
}
}
Output:
Welcome
Explanation :
If( printf(“Welcome”))
If( 7 )
If( True )
Arrays :
It is a group of elements.
❖ The elements should be same data type.
❖ The elements must be continuously in memory.
❖ All the elements can be identified by a common name.
Index :-
To indentify the elements of an array we have to use a +ve integer it is called
index. It is also called as sub script. It always begins with 0. It is called lower
boundary. It always ends with size-1. It is called upper boundary.
Way2 :-
We can store array elements by assigning individual elements.
Way3 :-
We can store array elements using input operation :
Syntax:
#include<stdio.h>
void main()
{
int a[100],n,i;
Output :
Enter array size : 5
Enter 5 elements10 20 30 40 50
Array elements in reverse
50 40 30 20 10
3. WAP to read n elements into an array. Find max and min elements.
#include<stdio.h>
void main()
{
int a[100],n,i,max,min;
///read array size.
printf("Enter array size : ");
scanf("%d",&n);
/// Read array elements
printf("Enter %d elements",n);
for( i=0;i<n;i++)
scanf("%d",&a[i]);
///find max and min
max=min=a[0];
for( i=0;i<n;i++)
{
if( a[i] > max)
max=a[i];
if( a[i] < min )
min=a[i];
}
printf("Max=%d",max);
printf("\nMin=%d",min);
}
Output :
Enter array size : 6
Enter 6 elements10 8 5 12 15 9
Max=15
Min=5
4. WAP to read n elements into an array, search elements. Print whether the
search element is in array or not.
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
107 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
#include<stdio.h>
void main()
{
int a[100],n,i,sno,flag=0;
/// read array size
printf("Enter array size : ");
scanf("%d",&n);
/// read array elements
printf("Enter %d elements",n);
for( i=0;i<n;i++ )
scanf("%d",&a[i] );
/// read search number
printf("Enter search number : ");
scanf("%d",&sno);
/// Search operation
for( i=0;i<n;i++ )
{
if( a[i]==sno )
flag=1;
}
if( flag==1 )
printf("Element found");
else
printf("Element not found");
}
Output 1 :
Enter array size : 6
Enter 6 elements10 5 15 20 25 30
Enter search number : 20
Element found
Output2:
Enter array size : 6
Enter 6 elements10 20 30 40 50 60
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
108 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Enter search number : 35
Element not found
#include<stdio.h>
void main()
{
int a[100],n,i,j,t;
/// read array size
printf("Enter array size : ");
scanf("%d",&n);
/// read array elements
printf("Enter %d elements : ",n);
for( i=0;i<n;i++ )
scanf("%d",&a[i] );
/// Sort the array.
for( i=0;i<n;i++ )
{
for( j=i+1;j<n;j++)
{
if( a[i] > a[j] )
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
/// print the result
printf("Sorted array\n");
for( i=0;i<n;i++ )
printf("%5d",a[i]);
}
Output :
Enter array size : 6
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
109 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Enter 6 elements :
5
12
4
1
6
8
Sorted array
1 4 5 6 8 12
#include<stdio.h>
void main()
{
int a[100],n,i,t;
/// read array size
printf("Enter array size : ");
scanf("%d",&n);
/// read array elements
printf("Enter %d elements : ",n);
for( i=0;i<n;i++ )
scanf("%d",&a[i] );
/// Swap adjacent elements
for( i=0;i<n-1;i+=2 )
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
/// print the result
printf("Array after swapping adjacent elements=");
for( i=0;i<n;i++ )
printf("%5d",a[i]);
}
Output :
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
110 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Enter array size : 6
Enter 6 elements : 1 2 3 4 5 6
Array after swapping adjacent elements= 2 1 4 3 6 5
7. WAP to read n elements into an array. Swap the elements with index.
#include<stdio.h>
void main()
{
int a[100],b[100],n,i;
/// read array size
printf("Enter array size : ");
scanf("%d",&n);
/// read array elements.
printf("Enter %d elements",n);
for( i=0;i<n;i++ )
scanf("%d",&a[i] );
/// Swap elements with index.
for( i=0;i<n;i++ )
// b[index]=value
// b[value]=index
b[ a[i] ]=i;
/// print the result
printf("Array after swap index with elements \n");
for( i=0;i<n;i++ )
printf("%5d",b[i]);
}
Output :
Enter array size : 5
Enter 5 elements3 4 0 2 1
Array after swap index with elements
2 4 3 0 1
8. WAP to find the missing number of an array.
#include<stdio.h>
void main()
{
int a[100],n,i,sum=0,missing;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
111 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
///read array size
printf("enter array size : ");
scanf("%d",&n);
/// read array elements
printf("Enter %d elements",n);
for( i=0;i<n;i++ )
{
scanf("%d",&a[i] );
sum=sum+a[i];
}
missing=(n+1)*(n+2)/2-sum;
printf("Missing number =%d",missing);
}
Output :
enter array size : 6
Enter 6 elements1 2 3 5 6 7
Missing number =4
9. WAP to read n elements into an array, a search number. Print whether the
search number is in array or not using binary search operation.
#include<stdio.h>
void main()
{
int a[100],n,i,sno,L,R,M;
///read array size
printf("Enter array size : ");
scanf("%d",&n);
/// read array elements
printf("Enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
/// read search number.
printf("Enter search number : ");
scanf("%d",&sno);
/// Binary Search operation.
L=0;
R=n-1;
M=(L+R)/2;
while( L<=R )
{
if( sno==a[M])
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
112 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("Element found at %d",M);
break;
}
else if( sno<a[M])
R=M-1;
else
L=M+1;
M=(L+R)/2;
}
if( L>R )
printf("Element not found");
}
Output :
Enter array size : 7
Enter 7 elements10 12 20 25 29 45 50
Enter search number : 45
Element found at 5
2-D arrays :-
It is a group of one dimensional arrays of same size and same data type.
2. WAP to read mxn elements into an array, find the transpose matrix.
#include<stdio.h>
void main()
{
int a[10][10],t[10][10],m,n,i,j;
///read array size
printf("Enter array size : ");
scanf("%d%d",&m,&n);
/// read array elements
printf("enter %d elements",m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
///Generate transpose matrix.
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
116 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
for(i=0;i<m;i++)
for(j=0;j<n;j++)
t[j][i]=a[i][j];
/// print transpose matrix.
printf("Transpose matrix=\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%5d",t[i][j]);
printf("\n");
}
}
Output :
Enter array size : 2 3
enter 6 elements
123
456
Transpose matrix=
1 4
2 5
3 6
#include<stdio.h>
void main()
{
int a[10][10],m,n,i,j;
///read array size
printf("Enter array size : ");
scanf("%d%d",&m,&n);
if( m!=n )
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
117 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("Lower diagonal matrix can not be generated");
else
{
for( i=0;i<m;i++)
for(j=0;j<n;j++)
{
if( i>=j )
a[i][j]=1;
else
a[i][j]=0;
}
printf("Lower diagonal matrix is \n");
for( i=0;i<m;i++ )
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
}
Output :
Enter array size : 4 4
Lower diagonal matrix is
1 0 0 0
1 1 0 0
1 1 1 0
1 1 1 1
#include<stdio.h>
void main()
{
int a[10][10],m,n,i,j;
///read array size
printf("Enter array size : ");
scanf("%d%d",&m,&n);
if( m!=n )
printf("Upper diagonal matrix can not be generated");
else
{
for( i=0;i<m;i++)
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
118 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
for(j=0;j<n;j++)
{
if( i<=j )
a[i][j]=1;
else
a[i][j]=0;
}
printf("Upper diagonal matrix is \n");
for( i=0;i<m;i++ )
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
}
Output:
Enter array size : 3 3
Upper diagonal matrix is
1 1 1
0 1 1
0 0 1
5. WAP to read mxn elements into an array. Print whether it is symmetric
matrix or not.
#include<stdio.h>
void main()
{
int a[10][10],m,n,i,j,flag=1;
///read array size
printf("Enter array size : ");
scanf("%d%d",&m,&n);
///read array elements
printf("Enter %d elements",m*n);
Output :
Enter array size : 3 3
Enter 9 elements1 2 3 2 8 9 3 9 6
Yes, it is Symmetric matrix
6. WAP to read mxn elements into an array. Print whether it is identity
matrix or not.
#include<stdio.h>
void main()
{
int a[10][10],m,n,i,j,flag=1;
///read array size
printf("Enter array size : ");
scanf("%d%d",&m,&n);
///read array elements
7. WAP to read mxn elements into 2 arrays, find the addition array.
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,i,j;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
121 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
/// read array size.
printf("Enter array size : ");
scanf("%d%d",&m,&n);
///read elements into A
printf("Enter %d elements into A : ",m*n);
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )
scanf("%d",&a[i][j]);
/// read elements into B
printf("enter %d elements into B : ",m*n);
for(i=0;i<m;i++ )
for( j=0;j<n;j++ )
scanf("%d",&b[i][j]);
/// find result array.
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )
c[i][j]=a[i][j] + b[i][j];
/// print the addition array
printf("Resultant array is \n");
for( i=0;i<m;i++ )
{
for( j=0;j<n;j++ )
printf("%5d",c[i][j]);
printf("\n");
}
}
Output :
Enter array size : 2 3
Enter 6 elements into A :
123
456
enter 6 elements into B :
10 20 30
40 50 60
Resultant array is
11 22 33
44 55 66
8. WAP to read mxn elements into 2 arrays, find the subtraction array.
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,i,j;
/// read array size.
printf("Enter array size : ");
scanf("%d%d",&m,&n);
///read elements into A
printf("Enter %d elements into A : ",m*n);
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )
scanf("%d",&a[i][j]);
/// read elements into B
printf("enter %d elements into B : ",m*n);
for(i=0;i<m;i++ )
for( j=0;j<n;j++ )
scanf("%d",&b[i][j]);
/// find result array.
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )
c[i][j]=a[i][j] - b[i][j];
/// print the addition array
printf("Resultant array is \n");
for( i=0;i<m;i++ )
{
for( j=0;j<n;j++ )
printf("%5d",c[i][j]);
printf("\n");
}
}
Output :
Enter array size : 2
2
Enter 4 elements into A :
Output 1:
Enter size of A : 2
3
Enter size of B: 5
2
Multiplication not possible
Output 2:
Enter size of A : 2
2
Enter size of B: 2
2
Enter 4 elements into A : 1
2
3
4
Enter 4 elements into B : 1
2
3
4
Resultant array is
7 10
15 22
Strings :-
❖ It is a character array.
❖ String is a group of characters.
❖ It always ends with null character (\0)
#include<stdio.h>
void main()
{
char str[100];
int len;
/// read the string
printf("Enter a string : ");
scanf("%s",&str);
///find the length
for( len=0;str[len]!='\0';len++);
printf("Length=%d",len);
}
Output :
Enter a string : ramana
Length=6
#include<stdio.h>
void main()
{
char str[100],rev[100];
int i,j,len,flag=1;
/// read a string
printf("Enter a string : ");
scanf("%s",&str);
///reverse the string
for( len=0;str[len]!='\0';len++);
for(i=0,j=len-1;str[i]!='\0';i++,j--)
rev[j]=str[i];
rev[len]='\0';
for(i=0;str[i]!='\0';i++)
if( str[i] != rev[i] )
flag=0;
if( flag==1 )
printf("Palindrome");
else
printf("Not a palindrome");
}
Output1 :
Enter a string : dad
Palindrome
Output2:
Enter a string : nrt
Not a palindrome
#include<stdio.h>
void main()
{
char str[100];
int i;
printf("Enter a string : ");
scanf("%s",&str);
for( i=0;str[i]!='\0';i++)
{
if( str[i]>='A' && str[i]<='Z' )
str[i]=str[i]+32;
}
printf("result=%s",str);
}
Output1 :
Enter a string : RAmana
result=ramana
Output :
Enter a string : rama,raman are brothers. rama got 80%. raman
got 75%.
Alphabets count=35
Vowels count=14
Consonants count=21
Digits count=4
Spaces=8
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
129 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Symbols=6
Functions :-
❖ A big program can be break down into pieces, these are called
functions.
❖ Function is a group of related statements those are meant for
specific task.
❖ Function is a piece of code which is meant for specific task.
Syntax :
ReturnType FunctionName( arguments )
{
Body;
Return a;
}
Types of functions :-
These are classified into 2 types.
1. Built in functions :- these are already defined in system. These are
called system defined functions or library functions or standard
library functions.
Example :
Printf(), scanf(), gets(), getchar(), getche(), putchar()…
2. User defined functions :- these are defined by user.
Models of functions :-
A function can be defined in 4 models based on arguments passes and return value.
Model Arguments Return Example
1 With With int sum( int,int )
2 With Without void sum( int,int )
3 Without With int sum( )
4 Without Without void sum()
Stages of a function :-
A function can be define in 3 stages.
1. Declaration / prototype
2. Definition
3. Calling
Terminology :-
Note :
1. The actual arguments and formal parameters should be match in count.
2. The actual arguments and formal parameters should be match in data type.
3. The actual arguments and formal parameters should be match in sequence.
4. The actual arguments and formal parameters need not be match in names.
1. WAP to find area of a rectangle using functions of with arguments and with
return model.
#include<stdio.h>
int area(int l,int w)
{
return l*w;
}
void main()
{
int area(int,int);
int l,w,a;
printf("Enter length,width : ");
scanf("%d%d",&l,&w);
a=area(l,w);
printf("Area=%d",a);
}
Output:
Enter 2 numbers
10
20
a=20
b=10
3. WAP to find area of circle using functions of without arguments and with
return.
#include<stdio.h>
float area()
{
int r;
printf("Enter radius : ");
scanf("%d",&r);
return 3.14*r*r;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
132 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
}
void main()
{
float area();
float a;
a=area();
printf("Area=%f",a);
}
Output :
Enter radius : 10
Area=314.000000
Recursion functions :-
If a function called by itself then it is called recursion functions.
1. WAP to read a number. Find the factorial using recursion functions.
Fact( 1 )=1
Fact(2)=2*1=2*fact(1)
Fact(3)=3*2*1=3*fact(2)
Fact(10)=10*fact(9)
Fact(n)=n*fact(n-1)
#include<stdio.h>
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
133 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
long int fact(int n)
{
if( n==1 )
return 1;
else
return n*fact(n-1);
}
void main()
{
long int fact(int);
int n;
long int f;
printf("Enter a number : ");
scanf("%d",&n);
f=fact(n);
printf("Factorial=%ld",f);
}
Output :
Enter a number : 5
Factorial=120
#include<stdio.h>
int fib(int n)
{
if( n==1 )
return 0;
else if( n==2 )
return 1;
else
return fib(n-1)+fib(n-2);
}
void main()
{
int n;
for( n=1;n<=10;n++ )
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
134 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("%5d",fib(n));
}
Output :
0 1 1 2 3 5 8 13 21 34
A B R
4 6 4
6 4 2
4 2 0
#include<stdio.h>
void GCD(int a,int b)
{
int r=a%b;
if( r==0 )
printf("GCD=%d",b);
else
GCD(b,r);
}
void main()
{
void GCD(int,int);
int a,b;
printf("enter 2 numbers : ");
scanf("%d%d",&a,&b);
GCD(a,b);
}
Output :
enter 2 numbers : 6
4
GCD=2
No Q R
No Q R
10 5 0
5 2 1
2 1 0
1 0 1
#include<stdio.h>
void dec2bin(int no)
{
int q=no/2;
int r=no%2;
if( q==0 )
printf("%d",r);
else
{
dec2bin(q);
printf("%d",r);
}
}
void main()
{
void dec2bin(int);
int no;
printf("Enter a number : ");
scanf("%d",&no);
dec2bin(no);
}
Output :
Enter a number : 10
1010
#include<stdio.h>
1 moved from S to V
2 moved from S to D
1 moved from V to D
3 moved from S to V
1 moved from D to S
2 moved from D to V
1 moved from S to V
4 moved from S to D
1 moved from V to D
2 moved from V to S
1 moved from D to S
3 moved from V to D
1 moved from S to V
2 moved from S to D
1 moved from V to D
read( a,n );
disp( a,n );
}
Output:
Enter Array Size : 5
Enter 5 elements : 10 20 30 40 50
Array is : 10 20 30 40 50
10. WAP to read mxn elements into an array, print it on screen using
functions.
#include<stdio.h>
void read( int a[10][10],int m,int n )
{
int i,j;
printf("Enter %d elements : ",m*n);
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )
scanf("%d",&a[i][j]);
}
void disp( int a[10][10],int m,int n)
{
int i,j;
printf("Array is : \n");
for( i=0;i<m;i++ )
{
for( j=0;j<n;j++ )
printf("%5d",a[i][j]);
printf("\n");
}
}
void main()
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
141 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
int a[10][10],m,n;
printf("Enter Array size : ");
scanf("%d%d",&m,&n);
read( a,m,n );
disp( a,m,n );
}
Output:
Enter Array size : 2 3
Enter 6 elements : 10 20 30 40 50 60
Array is :
10 20 30
40 50 60
11. WAP to read mxn elements into 2 arrays, find the addition array using
functions.
#include<stdio.h>
int read( int a[10][10],int m,int n)
{
int i,j;
printf("Enter %d elements : ",m*n);
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )
scanf("%d",&a[i][j]);
}
void disp( int a[10][10],int m,int n)
{
int i,j;
printf("Result is : \n");
for( i=0;i<m;i++ )
{
for( j=0;j<n;j++ )
printf("%5d",a[i][j]);
printf("\n");
}
}
void add( int a[10][10],int b[10][10],int c[10][10],int m,int n)
{
int i,j;
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )
c[i][j]=a[i][j]+b[i][j];
}
void main()
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
142 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
{
int a[10][10],b[10][10],c[10][10],m,n;
printf("enter array size : ");
scanf("%d%d",&m,&n);
read( a,m,n );
read( b,m,n );
add( a,b,c,m,n );
disp( c,m,n);
}
Output:
enter array size : 2 2
Enter 4 elements : 10 20 30 40
Enter 4 elements : 1 2 3 4
Result is :
11 22
33 44
12. WAP to read mxn elements into an array, print the array using functions
and using single for loop.
#include<stdio.h>
void read(int a[10][10],int m,int n)
{
int i,row,col;
printf("Enter %d elements : ",m*n);
for( i=0;i<m*n;i++ )
{
row=i/n;
col=i%n;
scanf("%d",&a[row][col]);
}
}
void disp( int a[10][10],int m,int n)
{
int i,row,col;
printf("Array is : \n");
for( i=0;i<m*n;i++ )
{
row=i/n;
col=i%n;
if( col==0 )
printf("\n");
printf("%5d",a[row][col]);
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
143 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
}
}
void main()
{
int a[10][10],m,n;
printf("Enter array size : ");
scanf("%d%d",&m,&n);
read( a,m,n );
disp( a,m,n );
}
Output:
Enter array size : 2 3
Enter 6 elements : 10 20 30 40 50 60
Array is :
10 20 30
40 50 60
13. WAP to read a string, find the max character using functions.
#include<stdio.h>
void read( char str[100] )
{
printf("Enter a string : ");
scanf("%s",str);
}
void findMax( char str[100] )
{
int max=str[0];
int i;
for( i=0;str[i]!='\0';i++ )
{
if( str[i]>max )
max=str[i];
}
printf("Max Character=%c",max);
}
void main()
{
char str[100];
read( str );
findMax(str);
}
Output:
Enter a string : rama
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
144 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Max Character=r
14. WAP to read a string, find the min character using functions
#include<stdio.h>
void read( char str[100] )
{
printf("Enter a string : ");
scanf("%s",str);
}
void findMin( char str[100] )
{
int min=str[0];
int i;
for( i=0;str[i]!='\0';i++ )
{
if( str[i]<min )
min=str[i];
}
printf("Min Character=%c",min);
}
void main()
{
char str[100];
read( str );
findMin(str);
}
Output:
Enter a string : rama
Min Character=a
#include<stdio.h>
void read( char str[100])
{
printf("Enter a string : ");
scanf("%s",str);
}
void copy( char str[100],char des[100] )
{
int i;
for( i=0;str[i]!='\0';i++ )
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
145 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
des[i]=str[i];
}
des[i]='\0';
}
void disp( char str[100],char des[100])
{
printf("Original String =%s",str);
printf("\nCopied string=%s",des);
}
void main()
{
char str[100],des[100];
read( str );
copy( str, des );
disp( str, des );
}
Output:
Enter a string : india
Original String =india
Copied string=india
Output:
Enter a string : rama
Enter a string : rao
First string before merge = rama
Second string before merge = rao
First string after merge = ramarao
Second string after merge = rao
Output:
Enter a string : rama is a good boy
String after conversion = Rama Is A Good Boy
21. WAP to read password and confirmation password. Print whether they
are equal or not using function.
#include<stdio.h>
void read( char str[100])
{
char ch;
int i=0;
do
{
ch=getch();
if( ch!=13 )
printf("*");
str[i]=ch;
i++;
}while( ch!=13 );
str[i]='\0';
}
void test( char pword[100],char cpword[100])
{
int flag=1,i;
for( i=0;pword[i]!='\0';i++)
{
if( pword[i]!=cpword[i])
flag=0;
}
if( cpword[i]!='\0')
flag=0;
if( flag==1 )
printf("\nPassword matched with confirmation password");
else
printf("\nPassword does not matched with confirmation password");
}
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
150 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
void main()
{
char pword[100],cpword[100];
printf("Enter Password : ");
read( pword);
printf("\nEnter a Confirmation Password : ");
read( cpword);
printf("\nPassword=%s",pword);
printf("\nConfirmation Password=%s",cpword);
test( pword, cpword);
}
Output 1:
Enter Password : ****
Enter a Confirmation Password : ****
Password=rama
Confirmation Password=sita
Password does not matched with confirmation password
Output 2:
Enter Password : ****
Enter a Confirmation Password : ****
Password=rama
Confirmation Password=rama
Password matched with confirmation password
22. WAP without using main method and print welcome message.
#include<stdio.h>
#define india main
void india()
{
printf("Welcome");
}
23. WAP to find addition of 2 numbers without using any operator and using
functions.
#include<stdio.h>
int sum(int a,int b)
{
int r=printf("%*c%*c",a,' ',b,' ');
return r;
}
void main()
{
int a,b,r;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
151 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("Enter 2 numbers : ");
scanf("%d%d",&a,&b);
r=sum(a,b);
printf("Sum=%d",r);
}
Output:
Enter 2 numbers : 5 7
Sum=12
Explanation :
A=5
B=7
int r=printf("%*c%*c",a,' ',b,' ');
int r=printf(“%*C%*c”,5,’ ‘,7,’ ‘);
int r=printf(“%5c%7c”,’ ‘,’ ‘);
int r=print(“ “); // print 12 spaces
int r=12;
24. WAP to read a number. Print whether it is odd or even using loop and
don’t use “%” symbol and using functions.
#include<stdio.h>
void test(int no)
{
while( no>1 )
{
no=no-2;
}
if( no==1 )
printf("Odd number");
else
printf("even number");
}
void main()
{
int no;
printf("Enter a number : ");
scanf("%d",&no);
test(no);
}
Output :
Enter a number : 46
even number
Output:
Enter amount : 5233
2000 x 2
500 x 2
200 x 1
20 x 1
10 x 1
2x1
1x1
Output:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99
Output:
Storage classes
=======================================================
❖ Storage classes are the keywords.
❖ These describe Place of variable, default value of variable, life
span of variable, scope of a variable.
❖ Storage class are classified into 4 types.
a. Auto
b. Static
c. Register
d. External
1. Auto :-
-----------------------------
❖ Auto means automatic variable.
❖ By default all the local variables are automatic variables.
❖ Automatic variables will be created in memory every time the
function starts its execution.
❖ Automatic variables will be deleted every time when the function
terminates.
#include<stdio.h>
int sum(int no)
{
Auto int s=0;
s=s+no;
return s;
}
void main()
{
int no,s;
for( no=1;no<=10;no++ )
s=sum( no );
printf("Sum of 1 to 10 natural numbers = %d",s);
}
Output:
Sum of 1 to 10 natural numbers = 10
2. Static :
----------------------------------
Static variable creates when first time the function starts its execution
and remain in memory until program termination.
Example :
#include<stdio.h>
int sum(int no)
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
157 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
{
static int s=0;
s=s+no;
return s;
}
void main()
{
int no,s;
for( no=1;no<=10;no++ )
s=sum( no );
printf("Sum of 1 to 10 natural numbers = %d",s);
}
Output:
Sum of 1 to 10 natural numbers = 55
Register :-
--------------------------------
Register storage class create the variables in CPU register instead of creating
variable in memory.
It will fasten the program execution.
Example :
#include<stdio.h>
void main()
{
register int no;
for( no=1;no<=1000;no++)
{
printf("%5d",no);
}
}
3. Extern :-
------------------------------------
External variable can be used in other file also.
Example :
-------------------------- a.c -------------------------
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
158 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
extern int a=10,b=20;
--------------------------- b.c ---------------------------
#include<stdio.h>
#include "a.c"
void main()
{
printf("a=%d",a);
printf("\nb=%d",b);
}
Or
If you want to create forward declaration variables then we have to use
extern keyword.
Example :
#include<stdio.h>
void main()
{
extern int no;
printf("%d",no);
}
int no=10;
Structures :-
❖ Structure is a group of elements with different data types.
❖ It always begins with struct keyword.
Syntax :
struct StructureName
{
Datatype variable1;
Datatype variable2;
……
Datatype variable;
}structure_variable;
Example :
struct Student
{
int rno;
char sname[100],bname[30];
}s;
Output :
Enter book name, rate : C
199
Book name=C
Rate=199
Output
enter book name, rate : C
199
Enter book name, rate : Java
299
book name=C
Book rate=199
Book name=Java
Book rate=299
Nested Structures :-
If we define a structure with in another structure then it is called nested structure.
Example :
#include<stdio.h>
struct College
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
162 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
char cname[100],add[100];
struct student
{
int rno;
char sname[100];
}s;
}c;
void main()
{
printf("Enter cname,cadd, rno,sname : ");
scanf("%s%s%d%s",&c.cname,&c.add,&c.s.rno,&c.s.sname);
printf("College=%s",c.cname);
printf("\nAddress=%s",c.add);
printf("\nRno=%d",c.s.rno);
printf("\nStudent=%s",c.s.sname);
}
Output :
Enter cname,cadd, rno,sname : VVIT
namburu
501
raja
College=VVIT
Address=namburu
Rno=501
Student=raja
Union
❖ It is as same as the structure.
❖ It always begins with union keyword.
❖ It allocate memory to only one variable which require highest memory.
❖ All the members of union share this common memory.
Example :
#include<stdio.h>
union student
{
int rno;
char sname[100];
char cname[20];
};
void main()
{
union student s;
Enum :-
It is a user defined data type
It is used to define group of constants .
It always begins with enum keyword.
Example 1 :
#include<stdio.h>
enum ascii{a=97,b=98,c=99,d=100};
void main()
{
printf("a=%d",a);
printf("\nb=%d",b);
printf("\nc=%d",c);
printf("\nd=%d",d);
}
Output :
a=97
b=98
c=99
d=100
Example 2:
#include<stdio.h>
enum ascii{a=97,b,c,d};
void main()
{
printf("a=%d",a);
printf("\nb=%d",b);
printf("\nc=%d",c);
printf("\nd=%d",d);
}
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
164 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Output :
a=97
b=98
c=99
d=100
example 3:
#include<stdio.h>
enum ascii{a,b,c,d};
void main()
{
printf("a=%d",a);
printf("\nb=%d",b);
printf("\nc=%d",c);
printf("\nd=%d",d);
}
output :
a=0
b=1
c=2
d=3
typedef :-
It is a user defined data type.
It is used to create a new data type.
It creates nickname to existing data type.
It creates another name for existing data type.
example 1 :
#include<stdio.h>
void main()
{
typedef int integer;
integer no=100;
printf("no=%d",no);
}
Output :
No=100
Example 2 :
#include<stdio.h>
void main()
{
typedef char character;
character ch='a';
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
165 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("ch=%c",ch);
}
Output :
Ch=’a’
Pointer :-
Advantages :-
❖ We can use the data in indirect way.
❖ It can give solution complex application.
❖ Dynamic memory allocation.
❖ Dynamic memory deallocation.
Disadvantages :-
❖ It is so dangerous.
❖ It is tough to learn and understand.
❖
Pointer operators :-
You can use only 2 operators on pointers.
a. &
b. *
& :-
❖ It gives the address of operand.
❖ It is called address operator.
* :-
-------------
❖ It gives the value of variable to which the pointer points.
❖ It is called value at address operator.
Pointer Operations :-
++ operation :-
After perform ++ operation the pointer points to the next element.
-- operation :-
After perform - - operation the pointer points to the previous element.
Pointer assignment :-
We can assign a pointer in 2 ways.
Way 1 :-
We can assign a pointer with address of normal variable.
int a=10,*b;
b=&a;
Way2 :-
We can assign a pointer variable with another pointer variable.
Pointer arithmetics :-
We can perform arithmetic operation using pointers.
#include<stdio.h>
void main()
{
int a=10,b=5,*x,*y;
x=&a;
y=&b;
printf("Sum=%d",*x + *y);
printf("\nSub=%d",*x - *y);
printf("\nMul=%d",*x * *y);
printf("\nDiv=%d",*x / *y );
}
Output :
Sum=15
Sub=5
Mul=50
Div=2
Call by value :-
In this model we call the called function using values. The modifications those are
done in called function does not reflect on the values of calling function in call by
value model.
#include<stdio.h>
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
}
void main()
{
int a=10,b=20;
printf("\nBefore swap");
printf("\na=%d",a);
printf("\nb=%d",b);
swap(a,b);
printf("\nAfter swap");
printf("\na=%d",a);
printf("\nb=%d",b);
}
Output :
Before swap
a=10
b=20
After swap
a=10
b=20
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
170 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Call by reference :-
In this model we call the called function using reference/address. The modifications
those are done in called function directly reflect on values of calling function in call
by reference model.
#include<stdio.h>
void swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
void main()
{
int a=10,b=20;
printf("\nBefore swap");
printf("\na=%d",a);
printf("\nb=%d",b);
swap(&a,&b);
printf("\nAfter swap");
printf("\na=%d",a);
printf("\nb=%d",b);
}
Output :
Before swap
a=10
b=20
After swap
a=20
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
171 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
b=10
Pointer arrays :-
We can use a pointer variable as an array.
#include<stdio.h>
#include<malloc.h>
void main()
{
int *a,n,i;
/// read array size
printf("enter array size : ");
scanf("%d",&n);
/// read array elements
a=(int*)malloc(n*sizeof(int));
printf("enter %d elements",n);
for( i=0;i<n;i++ )
scanf("%d",a+i);
/// print array elements
printf("Array is \n");
for( i=0;i<n;i++)
printf("%5d",*(a+i));
}
Output:
enter array size : 5
enter 5 elements10
20
30
40
50
Array is
10 20 30 40 50
Note :
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
172 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
&a[i]➔ a+i
A[i] ➔ *(a+i)
3. WAP to read N elements into an array, find the odd elements count, even
elements count using pointers.
#include<stdio.h>
#include<malloc.h>
void main()
{
int *a,n,i,oc=0,ec=0;
printf("Enter array size : ");
scanf("%d",&n);
a=(int *)malloc( n*sizeof(int));
printf("Enter %d elements : ",n);
for( i=0;i<n;i++ )
scanf("%d",a+i);
for( i=0;i<n;i++ )
{
if( *(a+i)%2==0 )
ec++;
else
oc++;
}
printf("Odd numbers count=%d",oc);
printf("\nEven numbers count=%d",ec);
}
Output:
Enter array size : 7
Enter 7 elements : 1 5 7 10 25 14 100
Odd numbers count=4
Even numbers count=3
4. WAP to read N single digit elements into an array, spell all the numbers
using pointers.
#include<stdio.h>
#include<malloc.h>
void main()
{
int *a,n,i;
printf("Enter array size : ");
scanf("%d",&n);
a=(int*)malloc( n*sizeof(int));
printf("Enter %d single digit elements : ",n);
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
174 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
for( i=0;i<n;i++ )
scanf("%d",a+i);
for( i=0;i<n;i++ )
{
switch( *(a+i) )
{
case 0: printf("\nZero");break;
case 1: printf("\nOne"); break;
case 2: printf("\nTwo"); break;
case 3: printf("\nThree"); break;
case 4: printf("\nFour"); break;
case 5: printf("\nFive"); break;
case 6: printf("\nSix"); break;
case 7: printf("\nSeven"); break;
case 8: printf("\nEight"); break;
case 9: printf("\nNine"); break;
}
}
}
Output:
Enter array size : 5
Enter 5 single digit elements : 1 5 3 7 6
One
Five
Three
Seven
Six
5. WAP to read N elements into an array, find the squares using pointers.
#include<stdio.h>
#include<malloc.h>
void main()
{
int *a,n,i;
printf("Enter array size : ");
scanf("%d",&n);
a=(int*)malloc( n*sizeof(int));
printf("Enter %d elements : ",n);
for( i=0;i<n;i++ )
scanf("%d",a+i);
for( i=0;i<n;i++ )
*(a+i)=*(a+i) * *(a+i);
printf("Squares for given array are : ");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
175 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
for( i=0;i<n;i++ )
printf("%5d",*(a+i));
}
Output:
Enter array size :
5
Enter 5 elements : 1 3 2 4 5
Squares for given array are : 1 9 4 16 25
6. WAP to read N elements into an array, find the max and min element of
array using pointers.
#include<stdio.h>
#include<malloc.h>
void main()
{
int *a,n,i,max,min;
printf("Enter array size : ");
scanf("%d",&n);
a=(int*)malloc( n*sizeof(int));
printf("Enter %d elements : ",n);
for(i=0;i<n;i++ )
scanf("%d",a+i);
max=min=*(a+0);
for( i=0;i<n;i++)
{
if( *(a+i)>max )
max=*(a+i);
if( *(a+i)<min )
min=*(a+i);
}
printf("Maximum=%d",max);
printf("\nMinimum=%d",min);
}
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
176 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
Output:
Enter array size : 6
Enter 6 elements : 10 5 2 20 18 15
Maximum=20
Minimum=2
Output:
Enter array size : 6
Enter 6 elements : 10 2 5 40 25 7
multiples of 10 elements count=2
8. WAP to read N elements into 2 arrays, find the addition array using pointers.
#include<stdio.h>
#include<malloc.h>
void main()
{
int *a, *b, *c, n,i;
printf("Enter array size : ");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
b=(int*)malloc(n*sizeof(int));
c=(int*)malloc(n*sizeof(int));
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
177 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
printf("\nEnter %d elements into A : ",n);
for( i=0;i<n;i++ )
scanf("%d",a+i);
printf("\nEnter %d elements into B : ",n);
for( i=0;i<n;i++ )
scanf("%d",b+i);
for( i=0;i<n;i++ )
*(c+i)=*(a+i) + *(b+i);
printf("Resultant array is : ");
for( i=0;i<n;i++ )
printf("%5d",*(c+i));
}
Output:
Enter array size : 5
Enter 5 elements into A : 10 20 30 40 50
Enter 5 elements into B : 1 2 3 4 5
Resultant array is : 11 22 33 44 55
9. WAP to find nearest palindrome number for a given number using pointer.
#include<stdio.h>
int palindrome(int no)
{
int dup=no,rem,rev=0;
while( no!=0 )
{
rem=no%10;
rev=rev*10+rem;
no=no/10;
}
if( rev==dup )
return 1;
else
return 0;
}
void findNearestPalindrome( int no )
{
int i=0;
while( 1 )
{
if( palindrome( no+i)==1 )
{
printf("Nearest palindrome number is %d",no+i);
return;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
178 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
}
if( palindrome( no-i)==1 )
{
printf("Nearest palindrome umber is %d",no-i);
return;
}
i++;
}
}
void main()
{
int no;
printf("Enter a number : ");
scanf("%d",&no);
findNearestPalindrome( no );
}
Output:
Enter a number: 102
Nearest palindrome number is 101
Files
File :-
it is a group of related information which is stored under a name.
Types of files :-
Files are 2 types.
1. Text files
2. Binary files.
Text file :-
It is in human readable format. It contains the data in the form of alphabets, digits
and symbols.
Binary file :-
It is a machine readable file. It contains the data in the form of 0 and 1.
Operations of files :
1. Create a file.
2. Open a file.
3. Close a file.
4. Read the file.
5. Write the file.
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
179 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
6. Append the file.
File modes :-
1. W :
❖ It stands for write mode.
❖ It allows to write.
❖ It creates a new file.
❖ If the file already exists then it deletes and recreate the file.
2. R:
❖ It stands for read mode.
❖ It allows to read.
❖ It opens an existing file.
❖ If the file does not exist then it produce error.
3. A :-
❖ It stands for append mode.
❖ It allow to append.
❖ If opens an existing file.
❖ If the file does exist then it produce error.
4. W+ :-
❖ It stands for write plus mode.
❖ It allows to write and read.
❖ It creates a new file.
❖ If the file already exists then it deletes and recreate the file.
5. R+
❖ It stands for read plus mode.
❖ It allows to read and write.
❖ It opens an existing file.
❖ If the file does not exist then it produce error.
6. A+ :-
❖ It stands for append plus mode.
❖ It allow to append and read.
❖ If opens an existing file.
❖ If the file does exist then it produce error.
7. Wb
❖ It stands for write binary mode.
❖ It allows to write.
❖ It creates a new file.
❖ If the file already exists then it deletes and recreate the file.
8. Rb
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
180 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: [email protected]
C lANGUAGE
❖ It stands for read binary mode.
❖ It allows to read.
❖ It opens an existing file.
❖ If the file does not exist then it produce error.
9. Ab :-
❖ It stands for append binary mode.
❖ It allow to append.
❖ If opens an existing file.
❖ If the file does exist then it produce error.
File handling functions: -
1. Fopen() :- it is used to open a specified file in specified mode.
2. Fclose() :- it is used to close a file.
3. Fcloseall() :- it is used to close all the opened files.
4. Fgetc() :- it is used to read a character from a file.
5. Fputc() :- it is used to put/store a character in a file.
6. Fgets() :- it is used to read a string from file.
7. Fputs() :- it is used to put a string in a file.
8. Fread() :- it is used to read a structured information from a file.
9. Fwrite() :- it is used to write a structured information to a file.
10. Fscanf() :- it is used to read any type of data from a file.
11. Fprintf() :- it is used to write any type of data in a file.
12. Feof() :- it is used to test whether the file pointer reached to end of file or
not.
13. Ftell() :- it is used to identify the location of a file pointer.
14. Rewind () :- it is used to move the file pointer to the beginning of a file.
15. Fseek() :- it is used to move the file pointer from one location to another
location.
Output :
Rno=501
Sname=santosh
Cname=ECE
Output:
Student.txt copied to dup.txt
Output :
Student.txt and Employee.txt are merged into Merged.txt
Output :
data seperated