C Programming by Programmer's Love Incomplete PDF
C Programming by Programmer's Love Incomplete PDF
Be Programmer
C – Language History
C TOKENS:
C tokens are the basic buildings blocks in C language which are constructed together to write a C program.
Each and every smallest individual units in a C program are known as C tokens.
C tokens are of six types. They are, (KICS-OS)
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special symbols
6. Operators
1. Keyword:
Keywords are pre-defined words in a C compiler.
The words in program which meaning is already known by compiler is called keyword.
C language supports 32 keywords which are given below.
2. Identifiers:
Example:
1. int a; //correct
2. int 1a; //incorrect
3. int a1; //correct
4. int a@1; //incorrect
5. int a_1; //correct
OUTPUT:-
Global variable are-
a = 50 b = 80
Local Variables are -
Values: m = 10 n = 5
-------------------------------------------------------------------------------------------------------
Data Types: C data types are defined as the data storage format that a variable can store a data.
There are four basic data types in C language. They are,
Note: sizeof(var_name) function is used to find the memory space allocated for each C data
types.
Demonstrate sizeof() function:
#include <stdio.h>
#include <limits.h>
void main()
{
int a;
char b;
float c;
double d;
clrscr();
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
getch() ;
}
Output:-
Storage size for int data type:2
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8
3. DOUBLE: Double data type is also same as float data type which allows up-to
10 digits after decimal.
FORMAT SPECIFIER:
The format specifier is used during input and output. It is a way to tell the compiler what
type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c,
%d, %f, etc.
1. Integer%d
2. Float%f
3. Char%c
4. String%s etc.
Enum example in C:
enum month { Jan, Feb, Mar }
{
enum MONTH { Jan , Feb, Mar };
enum MONTH month = Mar;
if(month == 0)
printf("Value of Jan");
else if(month == 1)
printf("Month is Feb");
if(month == 2)
printf("Month is Mar");
}
Output:-
Month is March
4. C – Operators:
Arithmetic operators
Assignment operators
Relational operators
Logical operators
Bit wise operators
Conditional operators (ternary operators)
Increment/decrement operators
Special operators
#include <stdio.h>
#include<conio.h>
void main()
{
int a=40,b=20,
int add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}
Output:-
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
2. Assignment Operators: In C programs, values for the variables are assigned using
assignment operators.
For example, if the value “10” is to be assigned for the variable “sum”, it can
be assigned as “sum = 10;”
2. Compound assignment operators (Example: +=, -=, *=, /=, %=, &=, ^= )
OPERATORS EXAMPLE/DESCRIPTION
= sum = 10;
10 is assigned to variable sum
+= sum += 10;
This is same as sum = sum + 10
-= sum -= 10;
This is same as sum = sum – 10
*= sum *= 10;
This is same as sum = sum * 10
/= sum /= 10;
This is same as sum = sum / 10
%= sum %= 10;
This is same as sum = sum % 10
&= sum&=10;
This is same as sum = sum & 10
^= sum ^= 10;
This is same as sum = sum ^ 10
Que:In this program, values from 0 – 9 are summed up and total “45” is displayed as
output.Assignment operators such as “=” and “+=” are used in this program to assign the
values and to sum up the values.
# include <stdio.h>
#include<conio.h>
void main()
{
int Total=0; //assign 0 in variable total
int i;
clrscr();
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Total+i
}
printf("Total = %d", Total);
getch();
}
OUTPUT:
Total = 45
3. Relational Operators: Relational operators are used to find the relation between
two variables. i.e. to compare the values of two variables in a C program.
Operators Example/Description
> x > y (x is greater than y)
< x < y (x is less than y)
>= x >= y (x is greater than or equal to y)
<= x <= y (x is less than or equal to y)
== x == y (x is equal to y)
!= x != y (x is not equal to y)
int m=40,n=20;
clrscr();
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
Output:- m and n are not equal
----------------------------------------------------------------
4. Logical Operators: These operators are used to perform logical operations on the given
expressions. There are 3 logical operators in C language. They are, logical AND (&&), logical
OR (||) and logical NOT (!).
Operators Example/Description
&& (logical (x>5)&&(y<5)
AND) It returns true when both conditions are true
|| (logical OR) (x>=10)||(y>=10)
It returns true when at-least one of the
condition is true
! (logical NOT) !((x>5)&&(y<5))
It reverses the state of the operand “((x>5) &&
(y<5))”
If “((x>5) && (y<5))” is true, logical NOT
operator makes it false
EXAMPLE PROGRAM FOR LOGICAL OPERATORS IN C:
#include <stdio.h>
#include <conio.h>
void main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf("&& Operator : Both conditions are true\n");
}
if (o>p || p!=20)
{
printf("|| Operator : Only one condition is true\n");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are true\n");
}
else
{
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}
}
OUTPUT:
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false
These operators are used to perform bit operations. Decimal values are converted into binary
values which are the sequence of bits and bit wise operators work on these bits.
Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR), << (left shift)
and >> (right shift).
TRUTH TABLE FOR BIT WISE OPERATION & BIT WISE OPERATORS:
x=00101000
y= 01010000
In this example program, bit wise operations are performed as shown above and output is displayed in decimal
format.
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
}
OUTPUT:
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
Conditional operators return one value if condition is true and returns another value is condition is false.
This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional
statements.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,b=4;
clrscr();
(a>b?printf("a is greater !!"):printf("B is greater !!"));
getch();
}
OUTPUT: B is greater !!
7. SPECIAL OPERATORS IN C: Below are some of the special operators that the C programming
language offers.
Operators Description
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
getch()
}
OUTPUT:
8. C – Increment/decrement Operators: Increment operators are used to increase the value of the
variable by one and decrement operators are used to decrease the value of the variable by one in C
programs.
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
EXAMPLE PROGRAM FOR INCREMENT OPERATORS IN C:
In this program, value of “i” is incremented one by one from 1 up to 9 using “i++” operator and output is
displayed as “1 2 3 4 5 6 7 8 9”.
In this program, value of “I” is decremented one by one from 20 up to 11 using “i–” operator and output
is displayed as “20 19 18 17 16 15 14 13 12 11”.
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
OUTPUT: 20 19 18 17 16 15 14 13 12 11
Pre increment operator (++i) Value of i is incremented before assigning it to the variable i
Post increment operator (i++) Value of i is incremented after assigning it to the variable i
Pre decrement operator (--i) Value of i is decremented before assigning it to the variable i
Step 1 : In above program, value of “i” is incremented from 0 to 1 using pre-increment operator.
Step 2 : This incremented value “1” is compared with 5 in while expression.
Step 3 : Then, this incremented value “1” is assigned to the variable “i”.
Above 3 steps are continued until while expression becomes false and output is displayed as “1 2 3 4”.
Step 1 : In above program, value of “i” is decremented from 10 to 9 using pre-decrement operator.
Step 2 : This decremented value “9” is compared with 5 in while expression.
Step 3 : Then, this decremented value “9” is assigned to the variable “i”.
Above 3 steps are continued until while expression becomes false and output is displayed as “9 8 7 ”.
Operators Description
Printing Statements in C:
Printf(“Statement”);
Example1:
Printf(“Hello world”); o/p: Hello World
Example2:
Printf(“Value of A is %d”,a); o/p: Value of A is 10.
Taking input in c:
Scanf(“%d”,&var_name);
Example:
int a;
Printf(“Enter the value of A:”);
scanf(“%d”,&a);
o/p: Enter the value of A: 10
2. #include<conio.h>: This header file store the function definition for console(window) related
functions like clrscr() and getch() function.
a. Clrscr(): this function is used to clear the previous output in the screen.
b. getch(): This function is used to capture/hold the output window of the
program.
3. #include<math.h>: This header file store the function definition for mathematical related
operation functions like pow() and sqrt().
a. pow(): this function is used for calculating power of any integer value.
Syntax: pow(number,power);
Example:
int num=10;
int res;
res=pow(num,2);
printf(“The Square of 10 is %d”,res);
o/p: The Square of 10 is 100.
b. Sqrt(): this function is used for calculating the square root of any number.
Syntax: sqrt(number);
Example:
int num=4;
int res;
res=sqrt(num);
printf(“The Square root of 4 is %d”,res);
o/p: The Square root of 4 is 2.
First Program in C:
W.A.P for Print “Hello World ” statement.
----------------------------------------------------------------------
W.A.P for addition of two number.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1=2,num2=4;
int sum;
clrscr();
printf("Values:\n \tnum1=%d \n\tnum2=%d",num1,num2);
sum=num1+num2;
printf("\nAdd=%d",sum);
getch();
}
Output: Values:
Num1:2
Num2:4
Add=6
----------------------------------------------------------------------
W.A.P for addition of two number the values take from user.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
int sum;
clrscr();
printf("Enter the no1:");
scanf("%d",&num1);
printf("\nEnter the no2:");
scanf("%d",&num2);
sum=num1+num2;
printf("\nAddition=%d",sum);
getch();
}
Output: Enter the no1: 2
Enter the no2: 2
Addition=4
Swapping of two number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,b=4,temp;
clrscr();
printf("Before Swaping:\n \t value of a=%d and b=%d",a,b);
printf("\n-----------------------");
temp=a;
a=b;
b=temp;
printf("\nAfter Swaping: \n \t value of a=%d and b=%d",a,b);
getch();
}
----------------------------------------------------------------------
W.A.P to find the Area of circle.
#include<stdio.h>
#include<conio.h>
void main()
{
float r;
float pi=3.14;
float res;
clrscr();
printf("Enter the radius:");
scanf("%f",&r);
res=pi*r*r;
printf(“\n Area of circle:%f",res);
getch();
}
Output: Enter the radius: 2
Area of circle: 7.67
----------------------------------------------------------------------
W.A.P to find the circumference of circle.
#include<stdio.h>
#include<conio.h>
void main()
{
float r;
float pi=3.14;
float res;
clrscr();
printf("Enter the radius:");
scanf("%f",&r);
res=2*pi*r;
printf("\nCircumference of circle:%f",res);
getch();
}
Output: Enter the radius: 2
Circumference of circle: 12.560000
-----------------------------------------------------------------------
Algorithm: Alogrithm is the set of rule and instructions that step by step define the program.
Flowchart:
Where,
1. argc counts the number of arguments. It counts the file name as the first argument.
2. argv[] contains the total number of arguments. The first argument is the file name always.
Example:
#include <stdio.h>
}
else{
printf("First argument is: %s\n", argv[1]);
}
}
Output:
Run: program.exe hello
O/p:
Program name is: program
First argument is: hello
Control Statement in C:
1. Multiple IF statement:
SYNTAX:
if(condition)
{ -------
-------
}
if (condition)
{ -------
-------
}
Etc….
2. if…else statement:
syntax:
if (condition)
{ -------
-------
}
else
{ -------
-------
}
Syntax:
if (condition)
{ -------
-------
}
else if (condition)
{ -------
-------
}
else
{ -------
-------
}
4. Nested if:
syntax
if (condition)
{
if (condition)
{ -------
-------
}
else
{
-------
}
}
else
{ ------
------
}
QUE1. W. A. P take two number from user and find out which number is greater or numbers
are equals.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the Value for A:");
scanf("%d",&a);
QUE2. W. A. P take one number from user and find out number is odd or even.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter the Number:");
scanf("%d",&num);
if(num%2==0)
{
Printf(“Number is Even”)
}
else
{
printf(“Number is Odd");
}
getch();
}
Output:
QUE3. W. A. P take one number from user and check number is positive / negative / zero.
QUE4. W. A. P to check whether triangle is equilateral , isosceles, scalene or right angle
triangle.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int s1,s2,s3;
clrscr();
printf("Enter the sides:");
scanf("%d%d%d",&s1,&s2,&s3);
if(s1==s2 && s1==s3)
{
printf("Triangle is equilateral ....");
}
else if((s1==s2 && s1!=s3)||(s1==s3 && s1!=s2))
{
printf("Triangle is Isoscales Triangle ....");
}
else
{
printf("Triangle is Scalene....");
}
if(((pow(s1,2)+pow(s2,2))==pow(s3,2))||((pow(s2,2)+pow(s3,2))==pow(s1,2)))
{
printf("\nTriangle is Right angle Triangle !!!");
}
getch();
}
------------------------------------------------------------------------------------------------------------
Que. Check whether enter year is Leap year or not.
else
{
printf("Year is Not Leap Year !!!");
}
getch();
}
-------------------------------------------------------------------------------------------------------
Que:W.A.P to print the sum of squares of all odd numbers from 1-50.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=50;i++)
{
if(i%2!=0)
{
sum=sum+pow(i,2);
}
}
printf("Sum:%d",sum);
getch();
}
Que:W.A.P Which reads mark of three subject from Student and check whether student
is Pass/Fail if student is pass display percentage among with msg.
5. Switch Case:
#include<stdio.h>
#include<conio.h>
void main()
{
int week;
clrscr();
printf("Enter the choice for Day:");
scanf("%d",&week);
switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf(“Thursday”);
break;
case 5:
printf(“Friday”);
break;
case 6:
printf(“Saturday”);
break;
case 7:
printf(“Sunday”);
break;
default:
printf("Invalid input day");
break;
}
getch();
}
---------------------------------------------------------------------------------------------------------
QUE. W. A. P to get the number and print the number in words (0-9) only.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter the number:");
scanf("%d",&num);
switch(num)
{
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 Number Enter");
break;
}
getch();
}
------------------------------------------------------------------------------------------
4. continue:
The continue statement skips the current iteration of the loop and continues with
the next iteration.
Its syntax is:
continue;
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
printf(" %d",i);
}
getch();
}
Output: 1 2 3 4 6 7 8 9 10 (here 5 is skip due to continue statement)
--------------------------------------------------------------------------------------------------------
Que: W.A.P for making calculator, take two numbers from user and do operation as per user
choice (Addition, Subtraction, Multiplication, Division) use (+,-,*,/ ) for the operations .
#include<conio.h>
#include<stdio.h>
void main()
{
char opr;
int a,b;
float res=0;
int flag=0;
clrscr();
printf("Enter the two numbers:");
scanf("%d%d",&a,&b);
printf("Enter which operation we like to perform (+,-,*,/):");
scanf("%s",&opr);
switch(opr)
{
case '+':
res=a+b;
break;
case '-':
res=a-b;
break;
case '*':
res=a*b;
break;
case '/':
res=a/b;
break;
default:
printf("Sorry !! bhai ... You have enter wrong operation try again !!");
flag=1;
break;
}
if(flag!=1)
{ printf("\n Result: %f",res);
}
getch();
}
Output Case-1: Enter the two numbers: 2
2
Enter which operation we like to perform (+,-,*,/): +
Result: 4.0
Output Case-2: Enter the two numbers: 2
2
Enter which operation we like to perform (+,-,*,/): add
Sorry !! bhai ... You have enter wrong operation try again !!
---------------------------------------------------------------------------------------------
Loop in C:
Types of Loop:
1. for loop:
2. while loop:
3. do..while loop:
1. for loop:
Syntax:
for (initialization; condition; updation)
{
//statements body
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
getch();
}
Output: 1 2 3 4 5 6 7 8 9 10
2. While loop:
Syntax:
Pyramid Programms-
1. Star Patterns:
Example-1:
****
****
****
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
----------------------------------------------------------------------------------------------------
Example-2:
*
**
***
****
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
----------------------------------------------------------------------------------------------------
Example-3:
****
***
**
*
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
Example 4: * * * *
* * *
* * * *
* * *
* *
* *
* *
* *
*
*
Solution:
1. Let, consider table with 6 rows i.e. outer loop (i) and 7 columns inner loop (j) and for drawing
the * with respect to particular condition for drawing particular pattern.
2. Divide the number of condition logic to above pattern.
3. Get logic for
First Logic- row 1:
(i===0 && j%3!=0) print *
Second Logic - row 2,3:
(i===1 && j%3==0) || (i===2 && j%3==0) print *
Third Logic- row 4,5,6:
Let Consider (I-J)==2
I=3 & j=1 (3-1=2) print *
I=4 & j=2 (4-2=2) print *
I=5 & j=3 (5-3=2) print *
printf("\n");
}
getch();
}
---------------------------------------------------------------------------------------------------------------------
2. Number Paterns:
Example-1:
1111
1111
1111
1111
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
printf("1”);
}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------
Example-2:
1
11
111
1111
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("1");
}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------
Example-3:
1
22
333
4444
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------
Example-4:
1
23
456
7 8 9 10
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,v=1;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",v);
v=v+1;
}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------
Example-5:
4444
333
22
1
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,v=4;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("%d",v);
}
V=v-1;
printf("\n");
}
getch();
}
OR
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,v;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
V=5-i;
printf("%d",v);
}
printf("\n");
}
getch();
}
Example-6:
1111
222
33
4
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------
Example-7:
1
12
123
1234
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
OR
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,v=1;
clrscr();
for(i=1;i<=4;i++)
{
v=1;
for(j=1;j<=i;j++)
{
printf("%d",v);
v=v+1;
}
printf("\n");
}
getch();
}
----------------------------------------------------------------------------------------------------------------------------- ---------
3. Alphabets Pattern:
Example-1:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",ch);
}
printf("\n");
}
getch();
}
Output:
A
AA
AAA
AAAA
Example-2:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",ch);
}
ch=ch+1;
printf("\n");
}
getch();
}
Output:
A
BB
CCC
DDDD
Example-3:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("%c",ch);
}
printf("\n");
}
getch();
}
Output:
AAAA
AAA
AA
A
Example-4:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("%c",ch);
}
Ch=ch+1;
printf("\n");
}
getch();
}
Output:
AAAA
BBB
CC
D
Example-5:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=68;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("%c",ch);
}
Ch=ch-1;
printf("\n");
}
getch();
}
Output:
DDDD
CCC
BB
A
Example-6:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
Ch=65;
for(j=1;j<=i;j++)
{
printf("%c",ch);
ch=ch+1;
}
Ch=ch+1;
printf("\n");
}
getch();
}
Output:
A
AB
ABC
ABCD
Example-7:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
Ch=65;
for(j=1;j<=5-i;j++)
{
printf("%c",ch);
ch=ch+1;
}
Ch=ch+1;
printf("\n");
}
getch();
}
Output:
ABCD
ABC
AB
A
Example-6:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",ch);
ch=ch+1;
}
printf("\n");
}
getch();
}
Output:
A
BC
CDE
DEFG
Task-1:
1
24
135
2468
13579
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
if(i%2==0)
{
for(j=2;j<=i+i;j=j+2)
{
printf("%d",j);
}
}
else
{
for(j=1;j<=i+i;j++)
{
if(j%2!=0)
{
printf("%d",j);
}
}
}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------------------------
Que: W.A.P to check the number is prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num;
int flag=0;
clrscr();
printf("EnterNo=");
scanf("%d",&num);
if(num==1)
{
flag=1;
}
for(i=2;i<num;i++)
{
if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("Prime");
}
else
{
printf("Not Prime");
}
getch();
}
----------------------------------------------------------------------------------------------------------------
W.A.P to convert case of alphabet.
1. If user enter lower case result is upper case.
2. If user enter upper case result is lower case.
3. If user enter any characters rather than alphabets show “Sorry!! Invalid Characters”
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter any Character:");
scanf("%c",&ch);
if(ch>=65 && ch<=90)
{
ch=ch+32;
printf("Lower case is : %c",ch);
}
else if(ch>=97 && ch<=122)
{
ch=ch-32;
printf("Upper case: %c",ch);
}
else
{
printf("Sorry !!! Invalid Character Entered!!");
}
getch();
}
Output:
------------------------------------------------------------------------------------------------------------------------------------------------------------
Function in C Programming:
A function is a block of statements that used to perform a specific task.
Types:
1. Pre-define function:
2. User-define Function:
1. Pre-define Function: getch(), pow(),sqrt(), printf(), scanf() etc – These are the functions which
already have a definition in header files (.h files like stdio.h), so we just call them whenever there is a
need to use them.
a. Pow()
This pow() function is use for calculate power of any number. It has two integer value
argument first is number and another is its power.
Syntax: pow(number,power);
Example: pow(5,2); //it return answer 25
Pow(2,3); //it return answer 8
b. Sqrt()
This function is use for calculate square root of any number. It has one argument i.e. number
and it return square root of that particular number.
Syntax: sqrt(number);
Example: sqrt(25); //it return 5
Example: W.A.P to demonstrate pre-define function pow() and sqrt() using header file
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num,res;
char ch;
clrscr();
printf("What operation would you like to perform (Square=s/Square root=r):");
scanf("%c",&ch);
printf("Enter the Number:");
scanf("%d",&num);
if(ch=='s')
{
res=pow(num,2);
}
else if(ch=='r')
{
res=sqrt(num);
}
else
{
printf("Invalid choice !!!");
}
printf("Result=%d",res);
getch();
}
Output:
What operation would you like to perform (Square=s/Square root=r): s
Enter the number: 2
Result=4
What operation would you like to perform (Square=s/Square root=r): r
Enter the number: 4
Result=2
--------------------------------------------------------------------------------------------------------
2. User-define Function:
3. Function Body/defination:
Function definition contains the block of code to perform a specific task. In our
example, adding two numbers and returning it.
Syntax:
returnType functionName(type1 argument1, type2 argument2, ...)
{
//body of the function
}
Example:
Types of User-Define Function with respect to parameters and return values are:
a. Function with parameter and return value
b. Function with parameter and no return value.
c. Function with no parameter and return value.
d. Function with no parameter and no return value.
Function with default Argument: function can have default values of it's parameters. It means while
defining a function, We set some values bydefault. It makes function more generic.
#include<stdio.h>
#include<conio.h>
void Add(int,int);
void main()
{
int num1,num2;
clrscr();
printf("Enter the Value of Num2:");
scanf("%d",&num2);
Add(num1=10,num2);
getch();
}
void Add(int n1,int n2)
{
int sum;
sum=n1+n2;
printf("Addition:%d",sum);
#include <stdio.h>
#include<conio.h>
int addNumbers(int,int); // function prototype
void main()
{
int n1,n2,sum;
clrscr();
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
getch();
}
--------------------------------------------------------------------------------------------------------------------
Que2: Addition of two numbers using user-define function : Function with parameters and
no return value:
#include <stdio.h>
#include<conio.h>
void addNumbers(int a, int b); // function prototype
void main()
{
int n1,n2;
clrscr();
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
addNumbers(n1, n2); // function call
getch();
}
void addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
printf(“Sum=%d”,result);
}
---------------------------------------------------------------------------------------------------------------------
Que3: Addition of two numbers using user-define function : Function with no parameters
and return value:
#include <stdio.h>
#include<conio.h>
int addNumbers(); // function prototype
void main()
{
int sum;
clrscr();
sum = addNumbers(); // function call
printf("sum = %d",sum);
getch();
}
Que4: Addition of two numbers using user-define function : Function with no parameters
and no return value:
#include <stdio.h>
#include<conio.h>
void addNumbers(); // function prototype
void main()
{
clrscr();
addNumbers(); // function call
getch();
}
void addNumbers() // function definition
{
int a,b,result;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
result = a+b;
printf("sum = %d",result);
}
----------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int Rfact,num;
clrscr();
printf("Enter the Number:");
scanf("%d",&num);
Rfact=factorial(num);
printf("Factorial of %d is %d.",num,Rfact);
getch();
}
int factorial(int num)
{
int i,fact=1;
for(i=1;i<=num;i++)
{
fact=fact*i;
}
return fact;
}
---------------------------------------------------------------------------------------------------------------------
1. Call by Value:
Que. W.A.P to demonstrate call by value , by swaping of two number using swap function.
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int a=2,b=4;
clrscr();
printf("Before Swaping:\n \t value of a=%d and b=%d",a,b);
printf("\n-----------------------");
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\nAfter Swaping: \n \t value of a=%d and b=%d",a,b);
}
2. Call by Reference:
#include<stdio.h>
#include<conio.h>
void show(int *,int *);
void main()
{
int a=2,b=4;
clrscr();
printf("Before Swaping:\n \t value of a=%d and b=%d",a,b);
printf("\n-----------------------");
show(&a,&b);
printf("\nAfter Swaping: \n \t value of a=%d and b=%d",a,b);
getch();
}
void show(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
Que. W.A.P to find the factorial of number using function.
Recursion Function in C:
A function that calls itself is known as a recursive function. And, this technique is known as
recursion.
Syntax:
int main()
{
recursion();
}
void recursion()
{
Statements…;
recursion(); /* function calls itself */
}
return (n*factorial(n-1));
}
Output: Factorial=120
Execution:
factorial(5) call
5!=1 so 5*factorial(4)
4!=1 so 5*(4*factorial(3))
3!=1 so 5*4*3*(3*factorial(2))
2!=1 so 5*4*3*2(2*factorial(1))
1=1 so 5*4*3*2*1 //(5*4*3*2*1)=120
-----------------------------------------------------------------------------------------------
Que: W.A.P to find GCD of two Number Using Recursive Function:
----------------------------------------------------------------------------------------------------------------------------
Pointer in C:
The pointer in C language is a variable which stores the address of another variable. This
variable can be of type int, char, array, function, or any other pointer.
Consider, One Example-
int a=10;
int *p=&a; // declaring pointer variable.
This pointer variable p store the address of variable a.
Note:
& Use for store the address of variable
* Use for access the value at address.
QUE: W.A.P to Demonstrate Pointer:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int *p; //declaring pointer Variable
clrscr();
p=&a; //assign address of another variable in pointer variable.
printf("Address of a is =%d",&a);
printf("Address of P is =%d",p);
printf("\nValue at address of P is:%d",*p);
getch();
}
Output: Address of a is= 8005
Address of P is= 8005
Value at address of P is: 10
Need of Pointer:
1. To pass arguments by reference.
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a=10;
int b=20;
clrscr();
printf("Before Swap: \n \t Value of A=%d and B=%d",a,b);
swap(&a,&b);
printf("\nAfter Swap: \n \t Value of A=%d and B=%d",a,b);
getch();
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a= *b;
*b=temp;
}
Output: Before Swap:
Value of A=10 and B=20
After Swap:
Value of A=20 and B=10
2. For accessing array elements.
int main()
{
int arr[] = { 100, 200, 300, 400 };
// Compiler converts below to *(arr + 2).
printf("%d ", arr[2]);
scanf("%d%d",&num1,&num2);
cal(&num1,&num2,&add,&sub,&mul,&div);
printf("Add=%f",add);
printf("\nSub=%f",sub);
printf("\nMul=%f",mul);
printf("\nDiv=%f",div);
getch();
}
void cal(int *num1,int *num2,float *add,float *sub,float *mul,float *div)
{
*add=*num1+*num2;
*sub=*num1-*num2;
*mul=*num1**num2;
*div=*num1/ *num2;
}
Output:
Enter the two Number: 20 10
Add=30.00
Sub=10.00
Mul=200.00
Div=2.00
-------------------------------------------------------------------------------------------------------------
Array: Array is the collection of elements having similar data type. Also having contiguous
memory allocation.
Types:
1. One dimensional Array:
Array declaration by specifying size
int n = 5;
int var[n];
------ it create 5 variables i.e. var[0],var[1],var[2]……upto var[4].
Array Scanning:
Method 1:
int a[10];
printf(“Enter the element 1:”);
scanf(“%d”,&a[0]);
printf(“Enter the element 2:”);
scanf(“%d”,&a[1]);
printf(“Enter the element 3:”);
scanf(“%d”,&a[2]);
-----
----- and so on.
Method 2:
int a[10],i;
for(i=0;i<=9;i++)
{
Printf(“Enter the %dth element:”,i+1);
Scanf(“%d”,a[i]);
}
Array Printing:
Method 1:
int a[10];
printf(“Element 1 is %d”,a[0]);
printf(“Element 2 is %d”,a[1]);
printf(“Element 3 is %d”,a[2]);
-------------
----------- and so on.
Method 2:
int a[10],i;
for(i=0;i<=9;i++)
{
Printf(“Element %d is: %d”,i+1,a[i]);
}
Array Initialization
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
declare an array of user specified size
int n = 2,m = 2;
int var[n][m];
------ it create array of two row and two columns.
a[o][o] a[0][1]
a[1][0] a[1][1]
Array Scanning:
Method 1:
int a[2][2];
printf(“Enter the element:”);
scanf(“%d”,&a[0][0]);
printf(“Enter the element:”);
scanf(“%d”,&a[0][1]);
printf(“Enter the element:”);
scanf(“%d”,&a[1][0]);
----- and so on.
Method 2:
int a[2][2];
int i,j;
for(i=0;i<2;i++)
{
For(j=0;j<2;j++)
{
Printf(“Enter the element“);
Scanf(“%d”,a[i][j]);
}
}
Array Printing:
Method 1:
int a[2][2];
printf(“Element 1 is %d”,a[0][0]);
printf(“Element 2 is %d”,a[0][1]);
printf(“Element 3 is %d”,a[1][0]);
-------------
----------- and so on.
Method 2:
int a[2][2];
int i,j;
for(i=0;i<2;i++)
{
For(j=0;j<2;j++)
{
Printf(“%d”,a[i][j]);
}
Printf(“\n”);
}
----------------------------------------------------------------------------------------------------------------
Que.W.A.P to reverse of the number.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev=0,rem;
clrscr();
printf("Enter the Number:");
scanf("%d",&num);
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
printf("Reverse=%d",rev);
getch();
}
Que1: W.A.P to demonstrate array take the array element from user and display on user screen.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int a[10];
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("\n-----------------------------------------");
printf("\nYour entered Elements are:");
for(i=0;i<10;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Que2: W.A.P to take the array element from user and display sum of all elements on user
screen.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int a[10],sum=0;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("\n-----------------------------------------");
printf("\nYour entered Elements are:");
for(i=0;i<10;i++)
{
printf("\n%d",a[i]);
sum=sum+a[i];
}
printf("\n------------------------------------------");
printf("\nSum of all elements:%d",sum);
getch();
}
Que3: W.A.P to read n number of values in an array and display it in reverse order..
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int n;
int a[100];
clrscr();
printf("Enter how may number you want in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("--------------------------------");
printf("\nEntered elements are:");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
printf("\n-------------------------");
printf("\nReverse Order are:");
for(i=n-1;i>=0;i--)
{
printf("\n%d",a[i]);
}
getch();
}
Que4: W.A.P to Search an element in one dimensional array using linear search method.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={1,2,9,4,8,6,7,0};
int i,search,found=0;
clrscr();
printf("Array Elements is:\n");
for(i=0;i<=7;i++)
{
printf(" %d\n",a[i]);
}
printf("Enter the Element to be search:");
scanf("%d",&search);
for(i=0;i<=7;i++)
{
if(a[i]==search)
{
printf("Number %d is found at location %d",search,i+1);
found=1;
break;
}
}
if(found==0)
{
printf("Number is not found");
}
getch();
}
Output Case-1: Array element is: 1 2 9 4 8 6 7 0
Enter the element to be search: 9
Number 9 is found at location 3
Output Case-2: Array element is: 1 2 9 4 8 6 7 0
Enter the element to be search: 5
Number is not found
Que5. Write a program in C to copy the elements of one array into another array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int a[10];
int b[10];
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("\n-----------------------------------------");
printf("\nYour entered Elements in array1 is:");
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}
printf("\n------------------------------------------\n");
printf("Copied elements from array1 to array2 are:\n");
for(i=0;i<10;i++)
{
b[i]=a[i];
printf(" %d",b[i]);
}
getch();
}
Que6. W.A.P to Find out max and min element from the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int a[10];
int b[10];
int max,min;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("\n-----------------------------------------");
printf("\nYour entered Elements in array1 is:");
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}
max=a[0];
min=a[0];
for(i=0;i<10;i++)
{
if(a[i]>max)
{
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}
}
printf("\nMax=%d",max);
printf("\nMin=%d",min);
getch();
}
Que7. W.A.P to Find out Number of odd and even element from the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int odd=0,even=0;
int a[10];
int b[10];
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("\n-----------------------------------------");
printf("\nYour entered Elements in array1 is:");
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}
for(i=0;i<10;i++)
{
if(a[i]%2==0)
{
even++;
}
else
{
odd++;
}
}
printf("\nEven numbers are %d and odd is %d",even,odd);
getch();
}
Exercise on two dimensional Array:
Que1. Write a program in C to store elements in an two dimensional array and print it.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int a[2][3];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element:");
scanf("%d",&a[i][j]);
}
}
printf("\n------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
getch();
}
Que2. Write a program in C to store elements in an two dimensional array and print sum of all
elements in it.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int sum=0;
int a[2][3];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element:");
scanf("%d",&a[i][j]);
}
}
printf("\n------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
sum=sum+a[i][j];
}
printf("\n");
}
printf("\n--------------------------------\n");
printf("Sum of all elements in array is %d",sum);
getch();
}
Que3. W.A.P to find the sum of two matrixes of two dimensional array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int a[2][3];
int b[2][3];
int c[2][3];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element%d for Array1:",i+1);
scanf("%d",&a[i][j]);
}
}
printf("\n-------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
printf("\n------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element%d for Array2:",i+1);
scanf("%d",&b[i][j]);
}
}
printf("\n----------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",b[i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n--------------------------\n");
printf("Sum of two array Matrix is as follows:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",c[i][j]);
}
printf("\n");
}
getch();
}
Que4. W.A.P to find the two matrixes of two dimensional array are equal or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int equal=1;
int a[2][3];
int b[2][3];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element%d for Array1:",i+1);
scanf("%d",&a[i][j]);
}
}
printf("\n-------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
printf("\n------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element%d for Array2:",i+1);
scanf("%d",&b[i][j]);
}
}
printf("\n----------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",b[i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]!=b[i][j])
{
equal=0;
}
}
}
if(equal==1)
{
printf("Both Array are exactly equal !!!");
}
else
{
printf("Both Array are not equals !!!!");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int a[2][3];
int b[2][3];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element%d for Array1:",i+1);
scanf("%d",&a[i][j]);
}
}
printf("\n-------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
printf("\n------------------------------\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
b[i][j]=a[j][i];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d",b[i][j]);
}
printf("\n");
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------
String Handling:(String Functions)
String is an collection (array) of characters. The difference between a character array and
a string is the string is terminated with a special character ‘\0’.
We will see how to compare two strings, concatenate strings, copy one string to another &
perform various string manipulation operations. We can perform such operations using the pre-
defined functions of “string.h” header file.
How to declare strings: Declaring a string is as simple as declaring a one dimensional array. Below is
the basic syntax for declaring a string.
#include<stdio.h>
#include<conio.h>
Void main()
{
char str[] = "Adi N Tech";
printf("%s",str);
getch();
}
Output: Adi N Tech
Que2: W.A.P to read string from user.
#include<stdio.h>
#include<conio.h>
Void main()
{
char str[20];
clrscr();
printf(“Enter the String:”);
scanf(“%s”,&str);
printf("Entered String is %s",str);
getch();
}
Output: Enter the string: Adi
Entered string is Adi
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[20];
int len;
clrscr();
printf("Enter any String:");
scanf("%s",&str);
len=strlen(str);
printf("Length of String is %d",len);
getch();
}
Output: Enter any String: Adi
Length of String is 3
2. Strcpy():
The strcpy(destination, source) function copies the source string in destination.
#include<stdio.h>
#include<conio.h>
void main ()
{
char str1[20];
char str2[20];
clrscr();
printf("Enter any String1:");
scanf("%s",&str1);
printf("Enter any String2:");
scanf("%s",&str2);
strcpy(str1,str2);
printf("String copied successfully !! \n\tString1=%s\n\tString2=%s",str1,str2);
getch();
}
3. Strcat():
#include<stdio.h>
#include<conio.h>
void main ()
{
char str1[20];
char str2[20];
clrscr();
printf("Enter any String1:");
scanf("%s",&str1);
printf("Enter any String2:");
scanf("%s",&str2);
strcat(str1,str2);
printf("String concatenation successfully !! \n\tString1=%s\n\tString2=%s",str1,str2);
getch();
}
4. Strcmp():
#include<stdio.h>
#include<conio.h>
void main ()
{
char str1[20];
char str2[20];
clrscr();
printf("Enter any String1:");
scanf("%s",&str1);
printf("Enter any String2:");
scanf("%s",&str2);
if(strcmp(str1,str2)==0)
{
printf("Both Strings are equal !! ");
}
else
{
printf("Both Strings are not equal !!");
}
getch();
}
5. Strrev():
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[20];
clrscr();
printf("Enter any String:");
scanf("%s",&str);
printf("Reverse of String is %s",strrev(str));
getch();
}
6. Strlwr():
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[20];
clrscr();
printf("Enter any String:");
scanf("%s",&str);
printf("Lower case of String is %s",strlwr(str));
getch();
}
7. Strupr():
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[20];
clrscr();
printf("Enter any String:");
scanf("%s",&str);
printf("Upper case of String is %s",strupr(str));
getch();
}
----------------------------------------------------------------------------------------------------------------------------
Storage classes in C:
----------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[20],ch;
int i,count=0;
clrscr();
printf("Enter any String:");
scanf("%s",&str);
printf("Enter the Character:");
scanf("%s",&ch);
for(i=0;i<=strlen(str);i++)
{
if(str[i]==ch)
{
count++;
}
}
printf("%c is available %d times in string !!",ch,count);
getch();
}
Output: Enter any String: Programmers
Enter the Character: m
m is available 2 time in String !!
---------------------------------------------------------------------
Que2: W.A.P to search vowels, consonants, digits and spaces in given string statement.
#include <stdio.h>
#include<conio.h>
void main()
{
char line[150];
int i, vowels=0, consonants=0, digits=0, spaces=0;
clrscr();
printf("Enter a line of string: ");
gets(line);
for(i=0;i<=strlen(line);i++)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
vowels++;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
consonants++;
}
else if(line[i]>='0' && line[i]<='9')
{
digits++;
}
else if (line[i]==' ')
{
spaces++;
}
}
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
getch();
}
----------------------------------------------------------------------------------------------------------------------------
Structure:
Arrays allow to define similar type of variables that can hold several data items of the same kind
(data types). Similarly structure is another user defined data type available in C that allows to create a
elements with different data items (data types). Structures are used to represent a record.
Suppose you want to keep record details of employees in company. You might want to track the
following attributes about each employee –
4. Name
5. Id
6. Salary
7. Designation
8. Department… etc.
Declaring Structure: To define a structure, you must use the struct keyword –
Example:
struct employee
{
Char empName[20];
Int empId;
Float empSalary;
};
struct employee emp1; //create str_var
struct employee emp2;
#include <stdio.h>
#include <conio.h>
struct employee {
char name[50];
int id;
float salary;
} e1;
void main() {
printf("Enter information:\n");
printf("Enter name: ");
scanf(“%s”,&e1.name);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s\n", e1.name);
printf("Id: %d\n", e1.id);
printf(“Salary: %f\n", e1.salary);
getch();
}
Output
Enter information:
Enter name: Adi
Enter Employee Id: 101
Enter salary: 4.5
Displaying Information:
Name: Adi
Roll number: 101
Marks: 4.5
Union:
A union is a special data type available in C that allows to store different data types in the
same memory location. You can define a union with many members, but only one member can contain
a value at any given time. Unions provide an efficient way of using the same memory location
variables for multiple-variables.
Syntax:
union union_name {
int i;
float f;
char str[20];
} union_var;
Example:
union Data {
int i;
float f;
char str[20];
} data;
----------------------------------------------------------------------------------------------------------------------------
File Handling:
Que: Read the number from the user and store in a file info.txt.
#include"stdio.h"
#include"conio.h"
void main()
{
FILE *fp;
int num;
char choice='y';
clrscr();
fp=fopen("info.txt","w");
while(choice=='y')
{
printf("Enter the number :");
scanf("%d",&num);
fprintf(fp,"%d\n",num);
printf("Do you wish to continue[y/n] :");
fflush(stdin);
scanf("%c",&choice);
}
printf("Information stored in a file info.txt");
fclose(fp);
getch();
} */
/*Read the info from the file and display on the user screen*/
/*void main()
{
FILE *fp;
int num;
clrscr();
fp=fopen("info.txt","r");
while(fscanf(fp,"%d",&num)!=EOF)
{
printf("%d\n",num);
}
fclose(fp);
getch();
}*/
//binary files
/*Read number from the user and store in a binary file infobin.txt*/
/*void main()
{
FILE *fp;
int num;
char choice='y';
clrscr();
fp=fopen("infobin.txt","wb");
while(choice=='y')
{
printf("Enter the number :");
scanf("%d",&num);
fwrite(&num,sizeof(num),1,fp);
printf("Do you wish to continue[y/n] :");
fflush(stdin);
scanf("%c",&choice);
}
printf("Information stored in a file infobin.txt");
fclose(fp);
getch();
}*/
//read the number from binary file and display on the user screen
/*void main()
{
FILE *fp;
int num;
clrscr();
fp=fopen("infobin.txt","rb");
while(fread(&num,sizeof(num),1,fp)==1)
{
printf("%d\n",num);
}
fclose(fp);
getch();
}*/
/*read the info from the file and display on the user screen*/
void main()
{
struct emp
{
char name[20];
int age;
float bs;
};
struct emp e;
FILE *fp;
clrscr();
fp=fopen("empinfo.txt","rb");
while(fread(&e,sizeof(e),1,fp)==1)
{
printf("%s %d %f\n",e.name,e.age,e.bs);
}
fclose(fp);
getch();
}
---------------------------------------------------------------------------------------------------------------