C LANGUAGE
Introduction:-
C is an of fspring of Basic Combined Programming Language (BPCL) called B developed in 1960 at Cambridge University.
The b language is modified by “DENIS RITCHIE” And was implemented at Bell laboratory in 1972. ‘C’ was mainly in academic environment but eventually with release of complier for commercial purpose and the increasing popularity of UNIX, it began to gain wide spread seaport of computer Professionals.
Definition of C:-
It is a programming language designed to help process certain kinds of data consisting of number, characters and string and to provide useful output known as in formations.
The task of processing the data is to execute a sequence of pre-defined instruction called program.
C Token:-
The smaller unit of the programming language is called C token.
It can’t be further sub-divided.
It has following parts:-
(1) Keywords
(2) Identifiers
(3) Constants (a) numeric
(b) Character
(4) Variables
Data type:-
There are four types of data types:-
(1) Fundamental data types (a) Integer data type
(b) Floating point data type
(c) Character data type
(d) Double data type
(2) User defined data types
(3) Derived data types (a) Array
(b) Function
(c) Structure
(d) Pointer
Operators:-
An operator is a symbol that tells the computer to perform certain mathematical and logical manipulation. It is used in program to manipulate data and variables.
There are eight types of operators:-
(1) Arithmetic operators
(2) Relational operators
(3) Logical operators
(4) Assignment operators
(5) Increment and Decrement operators
(6) Conditional operators
(7) Bitwise operators
(8) Special operators
Decision making and branching:-
We have a number of situations where we may have to change the order of execution of certain statements based on certain conditions, or repeat a group of statements until certain specified conditions are met.
This involves a kind of decision making to see whether a particular condition has occurred or not and then direct the computer to execute certain statements accordingly.
To support these statements we have:-
(1) if statements
(2) switch statements
(3) conditional operator statements
(4) goto statements
(1) if statement—used to control the flow of execution of statements.
Syntax:-
if (test expression)
The if statement may be implemented in different forms like:-
(a) simple if statement
(b) if……else statement
(c) nested if……else statement
(d) else if ladder statement
Decision making and looping:-
Looping capabilities enable us to develop concise programs containing repetitive processes without the use of goto statements.
There are three loop constructs for performing loop operations:-
1.) There while statement
2.) The do statement
3.) The for statement
1.) The while statement :-
Syntax:-
While (test condition)
{
body of the loop
}
2.) The do statement :-
Syntax:-
Do
{
body of the loop
}
while (test –condition);
3.) The for statement :-
Syntax:-
For (initialization; test-condition; increment/ decrement)
{
body of the loop
}
Array is the collection of similar type of data elements at the contiguous location of the memory.
Features ---
· collection of similar type of data
· allocation in array is contiguous
· member of array can be referred by using subscribing.
Types----
1.) one dimensional array
Syntax: - type variable –name [size];
2.) Two dimensional array
Syntax: - type array-name [row-size][column-size];
.
3.) multi dimensional array
Syntax:-type array name [s1] [s2] [s3]……..[sm];
Functions:-
A function is a self contained block of code that performs a particular task. Programs built using functions are much easier to understand, debug and test.
C functions are classified into two categories:-
(1) Library functions e.g. - printf, scanf etc.
(2) User-defined functions e.g. - main
Structures:-
It creates a format that may be used to declare structure variable. It helps to organize complex data in a more meaningful way. It is a powerful concept that we can use in program design.
Syntax:-
Struct tag_name
{
data_type member_1;
data_type member_2;
……………………..
…………………….
}
Pointer:-
Pointer is used because:-
Ø It enables more efficient handling of the data tables.
Ø It reduces the length and complexity of a program.
Ø It increases the execution speed.
Ø It saves the data storage space in memory.
THE PROGRAMS BASED ON THE GIVEN THEORIES ARE AS FOLLOWS:-
1. /* generating the following format 1
12
123
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6 */
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
for(i=1;i<=6;i++)
{
for(j=1;j<=1; j++)
{
printf(“%d”,j);
}
printf(\n”);
}
getch();
}
OUTPUT:-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
2. / * generate the series of squares of 2 to 15 */
#include<stdio.h>
#include<conio.h>
void main()
{
int i=2, result;
clrscr();
do
{
result=i*i;
printf(“\n Square of %d is %d”,i ,result);
i++;
}
while(i<16);
getch();
}
OUTPUT:-
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
Square of 6 is 36
Square of 7 is 49
3./* check whether even or odd */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rem;
clrscr();
printf(“Enter the number to be checked ”);
scanf(“%d”,&n);
rem=n%2;
if(rem==0)
puts(“THE NUMBER IS EVEN”);
else
puts(“THE NUMBER IS ODD”);
getch();
}
OUTPUT:-
Enter the number to be checked 36
THE NUMBER IS EVEN
Enter the number to be checked 11
THE NUMBER IS ODD
4. /* To find the simple interest */
#include<stdio.h>
#include<conio.h>
void main()
{
int amount,si,principle,rate,time;
clrscr();
printf(“Enter the principle, time and rate”);
scanf(“%d%d%d”,&principle,&time,&rate);
si=(principle*time*rate)/100;
amount=si+principle;
printf(“\n\t Simple interest on Rs.%d\n\t at the rate of%d percent\n\t
for%d year\n\t is:Rs.%d”,principle,rate,time,si);
getch();
}
OUTPUT:-
Enter the principle, time and rate 1000, 10, 10
Simple interest on Rs. 1000
at the rate of 10 percent
is:Rs.1000
5. /*Average of “n” number of inputs */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,sum=0,avg;
clrscr();
printf(“Enter the number of inputs:”);
scanf(“%d”,&n);
for(i=1;i<=n ;i++)
{
printf(“\t\t Enter the number:”);
scanf(“%d”,&j);
sum=sum + j;
}
avg= sum/n;
printf (“The average of %d numbers=%d”, n,avg);
getch();
}
OUTPUT:-
Enter the number of inputs: 3
Enter the number: 2
3
4
The average of 3 numbers= 3
6. /*Transposing a 2-D matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,x=1;
int a[4][3],t[3][4];
clrscr();
printf(“Enter the elements of the array”);
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,a[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
t[j][i]=a[i][j];
printf(“Elements after transposing the matrix%d”,t[j][i]);
}
}
getch();
}
OUTPUT:-
Enter the elements of the array 2
3
4
5
6
7
8
9
10
11
12
13
Elements after transposing the matrix 2
6
10
3
7
11
4
8
12
5
9
13
7. /* wap to print whether the input charcter is in upper-case or lower-case */
#include<stdio.h>
#include<ctype.h>
main()
{
char alpha;
clrscr();
printf(" enter the alphabet : ");
scanf("%c",& alpha);
{
if(alpha >= 65 && alpha < 97)
printf(" the character %c is in upper-case",alpha);
else
printf(" the character %c is in lower-case",alpha);
}
getch();
}
OUTPUT:
enter the alphabet : a
the character a is in lower-case
enter the alphabet : H
the character H is in upper-case
8. /* wap to generate table of given numbers */
#include<stdio.h>
#include<conio.h>
void table(int);
main()
{
int num;
clrscr();
printf("\n enter value :");
scanf ("%d",& num);
table (num);
getch();
}
void table (int x)
{
int table = 1,i,result;
printf("\n THE TABLE IS : \n ");
for(i=1;i<=10;i++)
{
result = x*i;
printf("\n %d * %d = %d"x,i,result);
}
}
OUTPUT:
enter value : 5
THE TABLE IS :
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
9. /* wap to generate a series of numbers */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(j = 1;j <= 6;j ++)
{
for(i = 6;i >= j;i --)
{
printf(" %d ",i);
}
printf("\n");
}
getch();
}
OUTPUT:
6 5 4 3 2 1
6 5 4 3 2
6 5 4 3
6 5 4
6 5
6
10./* wap to generate a series */
#include<stdio.h>
#include<conio.h>
main()
{
int k,l,i,j;
clrscr();
for(k=3,l=1;k>0;k--,l++)
{
for(i=0;i<k-1;i++)
{
printf(" ");
}
for(j=1;j<=l;j++)
{
printf("5 ");
}
printf("\n");
}
for(k=2,l=3;k>0;k--,l-=2)
{
for(i=k-1;i<=1;i++)
{
printf(" ");
}
for(j=1;j<=l;j++)
{
printf("5 ");
}
printf("\n");
}
getch();
}
OUTPUT:
5
5 5
5 5 5
5 5
5
11. /* wap to generate a series of stars */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(j = 1;j <= 6;j ++)
{
for(i = 1;i <= j;i ++)
{
printf(" * ",j);
}
printf("\n");
}
getch();
}
OUTPUT:
*
* *
* * *
* * * *
* * * * *
* * * * * *
12. /* wap to sum the series of the order 1/2 + 1/3 +.....+ 1/n */
#include<stdio.h>
#include<conio.h>
main()
{
int x=1,num;
float y=2,sum=0;
clrscr();
printf("\n enter value for num(length) :");
scanf("%d",& num);
while(y <= num)
{
sum = sum + (x / y);
y ++;
}
printf("\n the sum is %f",sum);
getch();
}
OUTPUT:
enter value for num(length) :4
the sum is : 1.083333
13. /* wap to generate a series */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,p = 1;
clrscr();
for(i=7;i>=1;i-=2)
{
for(j=1;j<=i;j++)
{
printf("%d",p);
}
p++;
printf("\n");
for(k=7;k>=i;k-=2)
printf(" ");
}
getch();
}
OUTPUT:
1111111
22222
333
4
14. /* wap to generate a table of a number using function */
#include<stdio.h>
#include<conio.h>
main()
{
int num;
clrscr();
printf("\n enter the number : ");
scanf("%d",& num);
table(num);
getch();
}
table(int x)
{
int i,result;
for(i = 1; i <= 10; i ++)
{
result = x * i;
printf("\n \n %d * %d = %d ",x,i,result);
}
}
OUTPUT:
enter the value : 5
the factorial of 5 is 120
15. /* wap to add 2-d matrix & display it */
#include<stdio.h>
#include<conio.h>
main()
{
int arr [3][3],xyz [3][3],abc [3][3];
int row,col;
clrscr();
printf("\n enter the elements of first matrix : ");
for(row = 0; row < 3; row++)
{
for(col = 0; col < 3; col++)
{
scanf("%d",&arr[row][col]);
}
}
printf("\n enter the elements of second matrix : ");
for(row = 0; row < 3; row ++)
{
for(col = 0; col < 3; col ++)
{
scanf("%d",&xyz[row][col]);
}
}
printf("\n first matrix is :\n");
for(row = 0; row < 3; row ++)
{
for(col = 0; col < 3; col ++)
{
printf("%5d",arr[row][col]);
}
printf("\n");
}
printf("\n second matrix is :\n");
for(row = 0; row < 3; row ++)
{
for(col = 0; col < 3; col ++)
{
printf("%5d",xyz[row][col]);
}
printf("\n");
}
printf("\n sum of the two matrix is :\n");
for(row = 0; row < 3; row ++)
{
for(col = 0; col < 3; col ++)
{
abc[row][col] = arr[row][col] + xyz[row][col];
}
}
for(row = 0; row < 3; row ++)
{
for(col = 0; col < 3; col ++)
{
printf("%5d",abc[row][col]);
}
printf("\n");
}
getch();
}
OUTPUT:
enter the elements of first matrix :
enter the elements of second matrix :
first matrix is :
1 2 3
4 5 6
7 8 9
second matrix is :
9 8 7
6 5 4
3 2 1
sum of the two matrix is :
10 10 10
10 10 10
10 10 10
16. /* wap to mutiply 2-D array */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,a[3][3],b[3][3],c[3][3];
clrscr();
printf("\n enter matrix a : ");
for(i = 0; i <= 2; i ++)
{
for(j = 0; j <= 2; j ++)
{
scanf("%d", & a[i][j]);
}
}
printf("\n enter matrix b : ");
for(i = 0; i <= 2; i ++)
{
for(j = 0; j <= 2; j ++)
{
scanf("%d", & b[i][j]);
}
}
printf("\n FIRST MATRIX IS :\n");
for(i = 0; i <= 2; i ++)
{
for(j = 0; j <= 2; j ++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
printf("\n SECOND MATRIX IS :\n");
for(i = 0; i <= 2; i ++)
{
for(j = 0; j <= 2; j ++)
{
printf("%5d",b[i][j]);
}
printf("\n");
}
printf("\n PRODUCT OF TWO MATRICES IS:\n");
{
for(i = 0; i <= 2; i ++)
{
for(j = 0; j <= 2; j ++)
{
c[i][j] = 0;
for(k = 0; k <= 2; k ++)
{
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
}
}
}
for(i = 0; i <= 2; i ++)
{
for(j = 0; j <= 2; j ++)
{
printf("%5d",c[i][j]);
}
printf("\n");
}
}
getch();
}
OUTPUT:
enter matrix a :
enter matrix b :
FIRST MATRIX IS :
1 1 1
1 1 1
1 1 1
SECOND MATRIX IS :
1 1 1
1 1 1
1 1 1
PRODUCT OF TWO MATRICES IS :
3 3 3
3 3 3
3 3 3
17. /* wap to obtain Fibonacci series */
#include<stdio.h>
#include<conio.h>
main()
{
int a = 0,b = 1,c,i,n = 10;
clrscr();
printf("\n %d",a);
printf("\n %d",b);
for(i = 1; i <= n-2; i ++)
{
c = a + b;
printf("\n %d",c);
a = b;
b = c;
}
getch();
}
OUTPUT:
0
1
1
2
3
5
8
13
21
34
18. /* wap to convert temperature from fahrenheit to celsius */
#include<stdio.h>
#include<conio.h>
main()
{
float fahrenheit,celsius;
clrscr();
printf("\n enter the value of fahrenheit temperature : ");
scanf("%f",&fahrenheit);
celsius = (fahrenheit - 32.0) / 1.8;
printf("\n celsius temperature is %f",celsius);
getch();
}
OUTPUT:
enter the value of fahrenheit temperature : 56
celsius temperature is 13.333333
19. /* wap for linear searching */
#include<stdio.h>
#include<conio.h>
main()
{
int arr[10],i,a,pos,found;
clrscr();
printf("\n enter the values of an array : ");
for(i = 0; i < 10; i ++)
{
printf("\n \t arr[%d] = ",i);
scanf("%d",& arr[i]);
}
printf("\n enter the value to be searched : ");
scanf("%d",&a);
for(i = 0; i < 10; i ++)
{
if(arr[i] == a)
{
pos = i;
found = 1;
break;
}
}
if(found == 1)
printf("\n ***** %d : IS PRESENT IN ARR[%d] LOCATION OF THE ARRAY . *****" ,a,pos);
else
printf("\n %d : IS NOT PRESENT IN THE ARRAY .",a);
getch();
}
OUTPUT:
enter the values of an array :
arr[0] = 12
arr[1] = 31
arr[2] = 45
arr[3] = 11
arr[4] = 56
arr[5] = 3
arr[6] = 21
arr[7] = 9
arr[8] = 78
arr[9] = 15
enter the value to be searched : 3
***** 3 : IS PRESENT IN ARR[5] LOCATION OF THE ARRAY . *****
20. /* wap for bubble sorting */
#include<stdio.h>
#include<conio.h>
main()
{
int arr[10],x,y,temp;
clrscr();
printf("\n ENTER THE ELEMENTS OF THE ARRAY : \n");
for(x = 0; x < 10; x ++)
{
printf("\n \t arr[%d] = ",x);
scanf("%d",&arr[x]);
}
for(x = 10; x > 0; x --)
{
for(y = 0; y < x; y ++)
{
if(arr[y] > arr[y + 1])
{
temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
printf("\n THE ARRAY IN SORTED ORDER IS : ");
printf("\n \n ***** BUBBLE SORT ***** ");
for(x = 0; x < 10; x ++)
printf("\n \n \t arr[%d] = %d",x,arr[x]);
getch();
}
OUTPUT:
ENTER THE ELEMENTS OF THE ARRAY:
arr[0] = 51
arr[1] = 72
arr[2] = 59
arr[3] = 12
arr[4] = 31
arr[5] = 1
arr[6] = 43
arr[7] = 34
arr[8] = 77
arr[9] = 9
THE ARRAY IN SORTED ORDER IS :
***** BUBBLE SORT *****
arr[0] = 1
arr[1] = 9
arr[2] = 12
arr[3] = 31
arr[4] = 34
arr[5] = 43
arr[6] = 51
arr[7] = 59
arr[8] = 72
arr[9] = 77
21. /* wap to convert binary to decimal */
#include<stdio.h>
#include<conio.h>
main()
{
int i = 1, bin, dec = 0, rem;
clrscr();
printf("\n enter the number : ");
scanf("%d",& bin);
while (bin > 0)
{
rem = bin % 10;
dec = dec + rem * i;
bin = bin / 10;
i = i * 2;
}
printf("\n decimal is : %d",dec);
getch();
}
OUTPUT:
enter the number : 1101
decimal is : 13
enter the number : 1001
decimal is : 9
22. /* wap to convert decimal to binary */
#include<stdio.h>
#include<conio.h>
main()
{
int i = 1, bin = 0, dec , rem;
clrscr();
printf("\n enter the number : ");
scanf("%d",& dec);
while (dec > 0)
{
rem = dec % 2;
bin = bin + rem * i;
dec = dec / 2;
i = i * 10;
}
printf("\n binary is : %d",bin);
getch();
}
OUTPUT:
enter the number : 13
binary is : 1101
enter the number : 9
binary is : 1001
23. /* wap for string copy & string length using function */
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
char copy(char *,char *);
char length(char *);
main()
{
char s[10],t[10];
int i;
clrscr();
printf("\n ***** ENTER THE STRING ***** \n SOURCE STRING : ");
scanf("%s",&s);
copy(s,t);
length(s);
printf("\n ***** COPIED STRING ***** \n TARGET STRING : %s ",t);
getch();
}
char copy(char *s,char *t)
{
while(*s != '\0')
{
*t = *s;
t ++;
s ++;
}
*t = '\0';
}
char length(char *s)
{
int i = 0;
while(*s != '\0')
{
s ++;
i ++;
}
printf("\n ********************************** ");
printf("\n THE LENGTH OF THE STRING IS : %d",i);
printf("\n ********************************** ");
}
OUTPUT:
***** ENTER THE STRING *****
SOURCE STRING : chandan
**********************************
THE LENGTH OF THE STRING IS : 7
**********************************
***** COPIED STRING *****
TARGET STRING : chandan
24. /* wap to check whether the given number is armstrong number or not */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,s=0;
clrscr();
printf("enter the number : ");
scanf("%d",&i);
k = i;
while(i > 0)
{
j = i % 10;
s = s + (j * j * j);
i = i / 10;
}
if(k == s)
printf(" \n %d : is an armstrong number ",k);
else
printf(" \n %d : is not an armstrong number ",k);
getch();
}
OUTPUT:
enter the number : 153
153 : is an armstrong number
25. /* wap to check whether an year is leap year or not */
#include<stdio.h>
#include<conio.h>
main()
{
int x;
clrscr();
printf("enter year : ");
scanf("%d",&x);
if((x % 4)==0 && x % 100 != 0 || x % 400 == 0)
printf("\n * * leap year * *");
else
printf("\n * * not leap year * *");
getch();
}
OUTPUT:
enter year : 2004
* * leap year * *
26. /* wap to generate a series of numbers */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(j = 6;j >= 1;j --)
{
for(i = j;i >= 1;i --)
{
printf(" %d ",j);
}
printf("\n");
}
getch();
}
OUTPUT :
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
27. /* wap to generate a series of numbers */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(j = 1;j <= 6;j ++)
{
for(i = 1;i <= j;i ++)
{
printf(" %d ",j);
}
printf("\n");
}
getch();
}
OUTPUT:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6