0% found this document useful (0 votes)
6 views

C_Programming_Practice

The document is a book titled 'C Programming: Practice' by Hitesh Mohapatra, published in May 2020, focusing on practical C programming skills. It is structured into three modules covering basic programming structures, simple statements and operators, and advanced types. The book is aimed at learners with a basic understanding of C, providing examples and exercises to enhance programming capabilities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C_Programming_Practice

The document is a book titled 'C Programming: Practice' by Hitesh Mohapatra, published in May 2020, focusing on practical C programming skills. It is structured into three modules covering basic programming structures, simple statements and operators, and advanced types. The book is aimed at learners with a basic understanding of C, providing examples and exercises to enhance programming capabilities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 106

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/341099983

C Programming: Practice

Book · May 2020

CITATIONS READS
0 2,731

1 author:

Hitesh Mohapatra
KIIT University
122 PUBLICATIONS 838 CITATIONS

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Special Session : Fuzzy Extension and its Applications in Engineering and Sciences: Theory, Models, and Applications (FEAESTMA) View project

Smart City View project

All content following this page was uploaded by Hitesh Mohapatra on 02 May 2020.

The user has requested enhancement of the downloaded file.


Author: Hitesh Mohapatra
Preface:
This book is devoted to practical C programming. C is currently the premier language for
software developers. That’s because it’s widely distributed and standard. C is still the language of
choice for robust, portable programming.

This book emphasizes the programming skills you will need to do real-world programming. It
practices the learner with simple programming’s. The prerequisite for this book is the learner
must have basic C fundamental. (including conception, design, code, methods, debugging,
release, documentation, maintenance, and revision).

This handbook is written for people with previous programming experience or programmers who
already know C and want to improve their style and reliability.

Specific instructions are given for producing and running programs MS-DOS/Windows users,
instructions are included for Borland C++, Turbo C++, and Microsoft Visual C++.

How This Book is Organized:


A proper walk always starts with crawling. In Module I the basic programming structures teaches,
how to crawl. This module guides the basic programming practice. This module teaches how a
program need to write and execute. Next, it leads towards insights of programming by discussing
variable, looping, condition making etc.

Module II describes all of the other simple statements and operators that are used in
programming. You’ll also learn how to organize these statements into simple functions.

In Module III we take our basic declarations and statements and learn how they can be used in the
construction of advanced types such as structures, unions, and classes.

Acknowledgments:
Thanks to my teachers for their valuable review on this book and guidance.

Author:

HITESH MOHAPATRA received the B.E. degree in Information Technology from Biju Patnaik
University of Technology, Odisha in 2006, the MTech degree in CSE from Biju Patnaik
University of Technology, Odisha in 2009. He is currently a full time Ph.D. scholar at Veer
Surendra Sai University of Technology, Burla, India since 2017. He has contributed 30 research
(SCI/SCOPUS) level papers, 12 international/national conferences and 1 book on Software
Engineering. He has 10 years of teaching experience both in industry and academic. His research
interests include wireless sensor network, smart city, smart grid and smart water.

ISBN-10: 1726820874 1 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra

Contents

SERIAL NUMBER PAGE NUMBER

Module 1 02-29

Module 2 31-90

Module 3 92-101

ISBN-10: 1726820874 2 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra

Dedicated to my Students

Citation Details:

TY - BOOK
T1 - C Programming: Practice
A1 - Mohapatra, H.
SN - 9781726820875
T3 - 1 Series
UR -
https://books.google.co.in/books?id=mmNBvQE
ACAAJ
Y1 - 2018
PB - INDEPENDENTLY PUBLISHED

@book{mohapatra2018c,
title={C Programming: Practice},
author={Mohapatra, H.},
isbn={9781726820875},
series={1 Series},
url={https://books.google.co.in/books?id=mmN
BvQEACAAJ},
year={2018},
publisher={INDEPENDENTLY PUBLISHED}
}

Book, C Programming: Practice, Mohapatra, H, 9781726820875, INDEPENDENTLY


PUBLISHED ,https://books.google.co.in/books?id=mmNBvQEACAAJ, 2018.

ISBN-10: 1726820874 3 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra

Module-I

ISBN-10: 1726820874 4 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
1) /* Write a C program to check whether a given integer
number is positive or negative*/

#include <stdio.h>
#include <conio.h>

void main()
{
int number;
clrscr();

printf("Enter a number\n");
scanf ("%d", &number);

if (number > 0)
printf ("%d, is a positive number\n", number);
else
printf ("%d, is a negative number\n", number);

/*-----------------------------
Output
Enter a number
-5
-5, is a negative number

RUN2
Enter a number
89
89, is a positive number
------------------------------*/

ISBN-10: 1726820874 5 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
2) /* Write a C program to find the area of a triangle, given three sides*/

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
int s, a, b, c, area;
clrscr();

printf("Enter the values of a,b and c\n");


scanf ("%d %d %d", &a, &b, &c);

/* compute s*/

s = (a + b + c) / 2;

area = sqrt ( s * (s-a) * (s-b) * (s-c));

printf ("Area of a triangale = %d\n", area);


}

/*-----------------------------
Output

Enter the values of a, b and c


3
4
5
Area of a triangle = 6
------------------------------*/

ISBN-10: 1726820874 6 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
3) /* Write a C program to find the area of a circle given the radius*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define PI 3.142
void main( )
{
float radius, area;
clrscr();

printf("Enter the radius of a circle\n");


scanf ("%f", &radius);

area = PI * pow (radius,2);

printf ("Area of a circle = %5.2f\n", area);


}

/*-----------------------------
Output

RUN1
Enter the radius of a circle
3.2
Area of a circle = 32.17

RUN 2
Enter the radius of a circle
6
Area of a circle = 113.11

------------------------------*/

ISBN-10: 1726820874 7 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
4) /* Write a C program to check whether a given integer is odd or even*/

#include <stdio.h>
#include <conio.h>

void main()
{
int ival, remainder;

clrscr();

printf("Enter an integer :");


scanf ("%d", &ival);

remainder = ival % 2;

if (remainder == 0)
printf ("%d, is an even integer\n", ival);
else
printf ("%d, is an odd integer\n", ival);

/*-----------------------------
Output

RUN1

Enter an integer :13


13, is an odd integer

RUN2
Enter an integer: 24
24, is an even integer

---------------------------------*/

ISBN-10: 1726820874 8 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
5) /*Write a C program to generate and print first N FIBONACCI numbers*/

#include <stdio.h>
void main()
{
int fib1=0, fib2=1, fib3, N, count=0;

printf("Enter the value of N\n");


scanf("%d", &N);

printf("First %d FIBONACCI numbers are ...\n", N);


printf("%d\n",fib1);
printf("%d\n",fib2);
while( count < N-2)
{
fib3 = fib1 + fib2;
count ++;
printf("%d\n",fib3);
fib1 = fib2;
fib2 = fib3;
}
}

/*--------------------------
Output

Enter the value of N


5
First 5 FIBONACCI numbers are ...
0
1
1
2
3
-------------------------------*/

ISBN-10: 1726820874 9 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
6) /* Write a C program to find the GCD and LCM of two integers
output the results along with the given integers. Use Euclids' algorithm*/

#include <stdio.h>
#include <conio.h>

void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
clrscr();

printf("Enter two numbers\n");


scanf("%d %d", &num1,&num2);

if (num1 > num2)


{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = num1 % num2;
while(remainder !=0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}

gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d \n", num1,num2,gcd);
printf("LCM of %d and %d = %d \n", num1,num2,lcm);

} /* End of main() */
/*------------------------
Output
RUN 1
Enter two numbers
5
15
GCD of 5 and 15 = 5 , LCM of 5 and 15 = 15
------------------------------*/
ISBN-10: 1726820874 10 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
7) /* Write a C program to find the biggest of three numbers*/

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int a, b, c;

clrscr();

printf("Enter the values of a,b and c\n");


scanf ("%d %d %d", &a, &b, &c);

printf ("a = %d\tb = %d\tc = %d\n", a,b,c);

if ( a > b)
{
if ( a > c)
{
printf ("A is the greatest among three\n");
}
else
{
printf ("C is the greatest among three\n");
}
}
else if (b > c)
{
printf ("B is the greatest among three\n");
}
else
printf ("C is the greatest among three\n");

/*-----------------------------
Output
Enter the values of a,b and c
23 32 45
a = 23 b = 32 c = 45
C is the greatest among three

RUN2
Enter the values of a,b and c
234
ISBN-10: 1726820874 11 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
678
195
a = 234 b = 678 c = 195
B is the greatest among three

------------------------------*/

ISBN-10: 1726820874 12 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
8) /* Write a C program to convert the given binary number into its equivalent
decimal */

#include <stdio.h>
void main()
{
int num, bnum, dec = 0, base = 1, rem ;
printf("Enter the binary number(1s and 0s)\n");
scanf("%d", &num); /*Enter maximum five digits or use long int*/
bnum = num;

while ( num > 0)


{
rem = num % 10;
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
}

printf("The Binary number is = %d\n", bnum);


printf("Its decimal equivalent is =%d\n", dec);

} /* End of main() */

/*---------------------------------------------------
Outpput
Enter the binary number(1s and 0s)
1010
The Binary number is = 1010
Its decimal equivalent is =10
----------------------------------------------------*/

ISBN-10: 1726820874 13 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
9) /* Write a C program to accept a figure code and find the areas of different
geometrical figures such as circle, square, rectangle etc using switch */

#include <stdio.h>
void main()
{
int fig_code;
float side,base,length,bredth,height,area,radius;

printf("-------------------------\n");
printf(" 1 --> Circle\n");
printf(" 2 --> Rectangle\n");
printf(" 3 --> Triangle\n");
printf(" 4 --> Square\n");
printf("-------------------------\n");

printf("Enter the Figure code\n");


scanf("%d",&fig_code);

switch(fig_code)
{
case 1: printf("Enter the radius\n");
scanf("%f",&radius);
area=3.142*radius*radius;
printf("Area of a circle=%f\n", area);
break;

case 2: printf("Enter the bredth and length\n");


scanf("%f %f",&bredth, &length);
area=bredth *length;
printf("Area of a Reactangle=%f\n", area);
break;
case 3: printf("Enter the base and height\n");
scanf("%f %f",&base,&height);
area=0.5 *base*height;
printf("Area of a Triangle=%f\n", area);
break;
case 4: printf("Enter the side\n");
scanf("%f",&side);
area=side * side;
printf("Area of a Square=%f\n", area);
break;

default: printf("Error in figure code\n");


break;

ISBN-10: 1726820874 14 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
} /* End of switch */

} /* End of main() */

/*----------------------------------------------------
Output
Run 1
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
2
Enter the bredth and length
2
6
Area of a Reactangle=12.000000
Run 2
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
3
Enter the base and height
5
7
Area of a Triangle=17.500000
------------------------------------------------------*/

ISBN-10: 1726820874 15 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
10) /* Write a C program to accept an integer find the sum of the digits in it */

#include <stdio.h>

void main()
{
long num, temp, digit, sum = 0;

printf("Enter the number\n");


scanf("%ld", &num);

temp = num;

while(num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}

printf("Given number =%ld\n", temp);


printf("Sum of the digits %ld =%ld\n", temp, sum);
} /* End of main()*/

/*---------------------------------------
Output
Enter the number
123456
Given number =123456
Sum of the digits 123456 =21
----------------------------------------*/

ISBN-10: 1726820874 16 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
11) /* Write a c program to convert given number of days to a measure of time *
given in years, weeks and days. For example 375 days is equal to 1 year * 1
week and 3 days (ignore leap year) */

#include <stdio.h>
#define DAYSINWEEK 7
void main()
{
int ndays, year, week, days;

printf("Enter the number of days\n");


scanf("%d",&ndays);

year = ndays/365;
week = (ndays % 365)/DAYSINWEEK;
days = (ndays%365) % DAYSINWEEK;

printf ("%d is equivalent to %d years, %d weeks and %d days\n",


ndays, year, week, days);
}

/*-----------------------------------------------
Output
Enter the number of days
375
375 is equivalent to 1 years, 1 weeks and 3 days

Enter the number of days


423
423 is equivalent to 1 years, 8 weeks and 2 days

Enter the number of days


1497
1497 is equivalent to 4 years, 5 weeks and 2 days
---------------------------------------------------*/

ISBN-10: 1726820874 17 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
12) /* Write a c program to swap the contents of two numbers *
* using bitwise XOR operation. Don't use either the *
* temporary variable or arithmetic operators */

#include <stdio.h>

void main()
{
long i,k;

printf("Enter two integers\n");


scanf("%ld %ld",&i,&k);

printf("\nBefore swapping i= %ld and k = %ld",i, k);

i = i^k;
k = i^k;
i = i^k;

printf("\nAfter swapping i= %ld and k = %ld",i,k);

/*------------------------------------------
Output
Enter two integers
23 34

Before swapping i= 23 and k = 34


After swapping i= 34 and k = 23
------------------------------------------*/

ISBN-10: 1726820874 18 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
13) /* Write a C program to accept a decimal number and convert it binary and
count the number of 1's in the binary number */

#include <stdio.h>

void main()
{
long num, dnum, rem, base = 1, bin = 0, no_of_1s = 0;

printf("Enter a decimal integer\n");


scanf("%ld", &num);

dnum = num;

while( num > 0 )


{
rem = num % 2;
if ( rem == 1 ) /*To count no.of 1s*/
{
no_of_1s++;
}
bin = bin + rem * base;
num = num / 2 ;
base = base * 10;
}
printf("Input number is = %d\n", dnum);
printf("Its Binary equivalent is = %ld\n", bin);
printf("No.of 1's in the binary number is = %d\n", no_of_1s);

} /* End of main() */

/*--------------------------------------------------
Output
Enter a decimal integer
75
Input number is = 75
Its Binary equivalent is = 1001011
No.of 1's in the binary number is = 4

RUN2
Enter a decimal integer
128
Input number is = 128
Its Binary equivalent is = 10000000
No.of 1's in the binary number is = 1
-----------------------------------------------------*/
ISBN-10: 1726820874 19 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
14) /* Write a C program to accept an integer and reverse it */

#include <stdio.h>
void main()
{
long num, rev = 0, temp, digit;

printf("Enter the number\n");


scanf("%ld", &num);

temp = num;

while(num > 0)
{
digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}

printf("Given number = %ld\n", temp);


printf("Its reverse is = %ld\n", rev);
}

/* ---------------------------
Output
Enter the number
123456
Given number = 123456
Its reverse is = 654321
------------------------------*/

ISBN-10: 1726820874 20 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
15)/* write a C program to find and output all the roots of a quadratic equation,
for non-zero coefficients. In case of errors your program should report suitable
error message*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

void main()
{
float A, B, C, root1, root2;
float realp, imagp, disc;

clrscr();

printf("Enter the values of A, B and C\n");


scanf("%f %f %f", &A,&B,&C);

/* If A = 0, it is not a quadratic equation */


if( A==0 || B==0 || C==0)
{
printf("Error: Roots cannot be determined\n");
exit(1);
}
else
{
disc = B*B - 4.0*A*C;
if(disc < 0)
{
printf("Imaginary Roots\n");
realp = -B/(2.0*A) ;
imagp = sqrt(abs(disc))/(2.0*A);
printf("Root1 = %f +i %f\n",realp, imagp);
printf("Root2 = %f -i %f\n",realp, imagp);
}
else if(disc == 0)
{
printf("Roots are real and equal\n");
root1 = -B/(2.0*A);
root2 = root1;
printf("Root1 = %f \n",root1);
printf("Root2 = %f \n",root2);
}
else if(disc > 0 )
{
ISBN-10: 1726820874 21 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
printf("Roots are real and distinct\n");
root1 =(-B+sqrt(disc))/(2.0*A);
root2 =(-B-sqrt(disc))/(2.0*A);
printf("Root1 = %f \n",root1);
printf("Root2 = %f \n",root2);
}
}

} /* End of main() */

/*---------------------------
Output
RUN 1
Enter the values of A, B and C
321
Imaginary Roots
Root1 = -0.333333 +i 0.471405
Root2 = -0.333333 -i 0.471405

RUN 2
Enter the values of A, B and C
121
Roots are real and equal
Root1 = -1.000000
Root2 = -1.000000

RUN 3
Enter the values of A, B and C
352
Roots are real and distinct
Root1 = -0.666667
Root2 = -1.000000
---------------------------------*/

ISBN-10: 1726820874 22 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
16) /* Write a C program to reverse a given integer number and check
whether it is a palindrome. Output the given numbers with suitable
message */

#include <stdio.h>
#include <conio.h>

void main()
{
int num, temp, digit, rev = 0;

clrscr();

printf("Enter an integer\n");
scanf("%d", &num);

temp = num; /* original number is stored at temp */

while(num > 0)
{
digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}

printf("Given number is = %d\n", temp);


printf("Its reverse is = %d\n", rev);

if(temp == rev )
printf("Number is a palindrome\n");
else
printf("Number is not a palindrome\n");
}

/*------------------------
Output
RUN 1
Enter an integer
12321
Given number is = 12321
Its reverse is = 12321
Number is a palindrome

RUN 2
Enter an integer
3456
ISBN-10: 1726820874 23 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
Given number is = 3456
Its reverse is = 6543
Number is not a palindrome
-----------------------------------*/

ISBN-10: 1726820874 24 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
17) /* Write a c program to compute the surface area and volume of a cube */

#include <stdio.h>
#include <math.h>

void main()
{
float side, surfArea, volume;

printf("Enter the length of a side\n");


scanf("%f", &side);

surfArea = 6.0 * side * side;

volume = pow (side, 3);

printf("Surface area = %6.2f and Volume = %6.2f\n", surfArea, volume);


}

/*------------------------------------------
Output
Enter the llngth of a side
4
Surface area = 96.00 and Volume = 64.00

------------------------------------------*/

ISBN-10: 1726820874 25 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
18) /* Write a c program to find whether a given year is leap year or not */

#include <stdio.h>

void main()
{
int year;

printf("Enter a year\n");
scanf("%d",&year);

if ( (year % 4) == 0)
printf("%d is a leap year",year);
else
printf("%d is not a leap year\n",year);
}
/*------------------------------------------
Output
Enter a year
2000
2000 is a leap year

RUN2
Enter a year
2007
2007 is not a leap year
------------------------------------------*/

ISBN-10: 1726820874 26 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
19) /* Write a C program to compute the value of X ^ N given X and N as inputs */

#include <stdio.h>
#include <math.h>

void main()
{
long int x,n,xpown;
long int power(int x, int n);

printf("Enter the values of X and N\n");


scanf("%ld %ld",&x,&n);

xpown = power (x,n);

printf("X to the power N = %ld\n");


}

/*Recursive function to computer the X to power N*/

long int power(int x, int n)


{
if (n==1)
return(x);
else if ( n%2 == 0)
return (pow(power(x,n/2),2)); /*if n is even*/
else
return (x*power(x, n-1)); /* if n is odd*/
}

/*-------------------------------------
Output
Enter the values of X and N
25
X to the power N = 32

RUN2
Enter the values of X and N
44
X to the power N ==256

RUN3
Enter the values of X and N
52
X to the power N = 25
-----------------------------------------*/
ISBN-10: 1726820874 27 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
20) /* Write a c program to multiply given number by 4 using bitwise operators */

#include <stdio.h>

void main()
{
long number, tempnum;

printf("Enter an integer\n");
scanf("%ld",&number);
tempnum = number;
number = number << 2; /*left shift by two bits*/

printf("%ld x 4 = %ld\n", tempnum,number);


}

/*------------------------------
Output
Enter an integer
15
15 x 4 = 60

RUN2
Enter an integer
262
262 x 4 = 1048
---------------------------------*/

ISBN-10: 1726820874 28 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
21)/* Write a C program to find the sum of first 50 natural numbers *
using for loop */

#include <stdio.h>

void main()
{
int num,sum=0;

for(num = 1; num <= 50; num++)


{
sum = sum + num;
}

printf("Sum = %4d\n", sum);


}
/*---------------------------
Output
Sum = 1275
----------------------------*/

ISBN-10: 1726820874 29 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
22) /* Write a C program to accept two integers and check if they are equal */
#include <stdio.h>
void main()
{
int m,n;

printf("Enter the values for M and N\n");


scanf("%d %d", &m,&n);

if(m == n )
printf("M and N are equal\n");

else
printf("M and N are not equal\n");

} /* End of main() */

/*------------------------------------
output
Enter the values for M and N
34 45
M and N are not equal
-------------------------------------*/

ISBN-10: 1726820874 30 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra

23) /* Write a C program to find the factorial of a given number */


#include <stdio.h>
void main()
{
int i,fact=1,num;

printf("Enter the number\n");


scanf("%d",&num);

if( num <= 0)


fact = 1;
else
{
for(i = 1; i <= num; i++)
{
fact = fact * i;
}
} /* End of else */

printf("Factorial of %d =%5d\n", num,fact);

} /* End of main() */
/*-------------------------------------------
Output
Enter the number
5
Factorial of 5 = 120

---------------------------------------------*/

ISBN-10: 1726820874 31 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra

Module-II

ISBN-10: 1726820874 32 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
1]/* Write a C program to sort N numbers in ascending order using Bubble sort and print
both the given and the sorted *
array with suitable headings */

#include <stdio.h>
#include <conio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, N, temp;

clrscr();

printf("Enter the value of N\n");


scanf("%d",&N);

printf("Enter the elements one by one\n");


for(i=0; i<N ; i++)
{
scanf("%d",&array[i]);
}
printf("Input array is\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
/* Bubble sorting begins */
for(i=0; i< N ; i++)
{
for(j=0; j< (N-i-1) ; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted array is...\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
} /* End of main*/
/*----------------------------------
Output
Enter the value of N
5
ISBN-10: 1726820874 33 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
Enter the elements one by one
390
234
111
876
345
Input array is
390
234
111
876
345
Sorted array is...
111
234
345
390
876
---------------------------*/

ISBN-10: 1726820874 34 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
2]/* Write a C program to accept N numbers sorted in ascending order and to search for a
given number using binary search. Report success or failure in the form of suitable
messages */

#include <stdio.h>
#include <conio.h>

void main()
{
int array[10];
int i, j, N, temp, keynum;
int low,mid,high;

clrscr();

printf("Enter the value of N\n");


scanf("%d",&N);

printf("Enter the elements one by one\n");


for(i=0; i<N ; i++)
{
scanf("%d",&array[i]);
}
printf("Input array elements\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
/* Bubble sorting begins */
for(i=0; i< N ; i++)
{
for(j=0; j< (N-i-1) ; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted array is...\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}

printf("Enter the element to be searched\n");


scanf("%d", &keynum);

ISBN-10: 1726820874 35 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
/* Binary searching begins */

low=1;
high=N;

do
{
mid= (low + high) / 2;
if ( keynum < array[mid] )
high = mid - 1;
else if ( keynum > array[mid])
low = mid + 1;
} while( keynum!=array[mid] && low <= high); /* End of do- while */

if( keynum == array[mid] )


{
printf("SUCCESSFUL SEARCH\n");
}
else
{
printf("Search is FAILED\n");
}

} /* End of main*/
/*----------------------------------
Output
Enter the value of N
4
Enter the elements one by one
3
1
4
2
Input array elements
3
1
4
2
Sorted array is...
1
2
3
4
Enter the element to be searched
4
SUCCESSFUL SEARCH
---------------------------*/

ISBN-10: 1726820874 36 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra

3)/* Write a C program to evaluate the given polynomial *


* P(x)=AnXn + An-1Xn-1 + An-2Xn-2+... +A1X + A0, by *
* reading its coefficients into an array. [Hint: Rewrite *
* the polynomial as *
* P(x) = a0 + x(a1+x(a2+x(a3+x(a4+x(...x(an-1+xan)))) *
* and evaluate the function starting from the inner loop]*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAXSIZE 10
void main()
{
int a[MAXSIZE];
int i, N,power;
float x, polySum;

clrscr();

printf("Enter the order of the polynomial\n");


scanf("%d", &N);

printf("Enter the value of x\n");


scanf("%f", &x);

/*Read the coefficients into an array*/

printf("Enter %d coefficients\n",N+1);
for (i=0;i <= N;i++)
{
scanf("%d",&a[i]);
}

polySum = a[0];

for (i=1;i<= N;i++)


{
polySum = polySum * x + a[i];
}

power = N; /*power--;*/

printf("Given polynomial is:\n");


for (i=0;i<= N;i++)
{
if (power < 0)
{
break;
}
ISBN-10: 1726820874 37 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra

/* printing proper polynomial function*/


if (a[i] > 0)
printf(" + ");
else if (a[i] < 0)
printf(" - ");
else
printf (" ");
printf("%dx^%d ",abs(a[i]),power--);
}
printf("\nSum of the polynomial = %6.2f\n",polySum);
}
/*-----------------------------------------------------
Output
RUN 1
Enter the order of the polynomial
2
Enter the value of x
2
Enter 3 coefficients
3
2
6
Given polynomial is:
+ 3x^2 + 2x^1 + 6x^0
Sum of the polynomial = 22.00

RUN 2
Enter the order of the polynomial
4
Enter the value of x
1
Enter 5 coefficients
3
-5
6
8
-9
Given polynomial is:
+ 3x^4 - 5x^3 + 6x^2 + 8x^1 - 9x^0
Sum of the polynomial = 3.00
-----------------------------------------------------*/

ISBN-10: 1726820874 38 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
4)/* Write a C program to read a string and check whether it is
a palindrome or not (without using library functions). Output
the given string along with suitable message */
/*(explicitly)*/
#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
char string[25], revString[25]={'\0'};
int i,length = 0, flag = 0;

clrscr();

fflush(stdin);
printf("Enter a string\n");
gets(string);

for (i=0; string[i] != '\0'; i++) /*keep going through each */


{ /*character of the string */
length++; /*till its end */
}

for (i=length-1; i >= 0 ; i--)


{
revString[length-i-1] = string[i];
}

/*Compare the input string and its reverse. If both are equal
then the input string is palindrome. Otherwise it is
not a palindrome */

for (i=0; i < length ; i++)


{
if (revString[i] == string[i])
flag = 1;
else
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome\n", string);
else
printf("%s is not a palindrome\n", string);

}
/*----------------------------------------------------
Output
RUN 1
Enter a string
ISBN-10: 1726820874 39 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
madam
madam is a palindrome

RUN 2
Enter a string
Madam
Madam is not a palindrome

RUN 3
Enter a string
good
good is not a palindrome
----------------------------------------------------------*/

ISBN-10: 1726820874 40 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
5]/* Write a C program read a sentence and count the number of number of vowels and
consonants in the given sentence. Output the results on two lines with suitable headings */

#include <stdio.h>
#include <conio.h>

void main()
{
char sentence[80];
int i, vowels=0, consonants=0, special = 0;

clrscr();

printf("Enter a sentence\n");
gets(sentence);

for(i=0; sentence[i] != '\0'; i++)


{
if((sentence[i] == 'a'||sentence[i] == 'e'||sentence[i] == 'i'||
sentence[i] == 'o'||sentence[i] == 'u') ||(sentence[i] == 'A'||
sentence[i] == 'E'||sentence[i] == 'I'|| sentence[i] == 'O'||
sentence[i] == 'U'))
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
if (sentence[i] =='\t' ||sentence[i] =='\0' || sentence[i] ==' ')
{
special = special + 1;
}
}
consonants = consonants - special;
printf("No. of vowels in %s = %d\n", sentence, vowels);
printf("No. of consonants in %s = %d\n", sentence, consonants);
}
/*----------------------------------------
Output
Enter a sentence
Good Morning
No. of vowels in Good Morning = 4
No. of consonants in Good Morning = 7
-----------------------------------------*/

ISBN-10: 1726820874 41 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
6]/* Write a C program to read N integers and store them in an array A, and so find the
sum of all these elements using pointer. Output the given array and the computed sum
with suitable heading */
#include <stdio.h>
#include <conio.h>
#include <malloc.h>

void main()
{
int i,n,sum=0;
int *a;

clrscr();

printf("Enter the size of array A\n");


scanf("%d", &n);

a=(int *) malloc(n*sizeof(int)); /*Dynamix Memory Allocation */

printf("Enter Elements of First List\n");


for(i=0;i<n;i++)
{
scanf("%d",a+i);
}

/*Compute the sum of all elements in the given array*/


for(i=0;i<n;i++)
{
sum = sum + *(a+i);
}
printf("Sum of all elements in array = %d\n", sum);
} /* End of main() */
/*----------------------------
Output
Enter the size of array A
4
Enter Elements of First List
10
20
30
40
Sum of all elements in array = 100
-------------------------------------*/

ISBN-10: 1726820874 42 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
7]/* Write a C program to read N integers (zero, +ve and -ve) *
* into an array A and to *
* a) Find the sum of negative numbers *
* b) Find the sum of positive numbers and *
* c) Find the average of all input numbers *
* Output the various results computed with proper headings

#include <stdio.h>
#include <conio.h>
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, N, negsum=0, posum=0;
float total=0.0, averg;

clrscr();

printf ("Enter the value of N\n");


scanf("%d", &N);

printf("Enter %d numbers (-ve, +ve and zero)\n", N);


for(i=0; i< N ; i++)
{
scanf("%d",&array[i]);
fflush(stdin);
}

printf("Input array elements\n");


for(i=0; i< N ; i++)
{
printf("%+3d\n",array[i]);
}

/* Summing begins */
for(i=0; i< N ; i++)
{

if(array[i] < 0)
{
negsum = negsum + array[i];
}
else if(array[i] > 0)
{
posum = posum + array[i];
}
else if( array[i] == 0)
{
;
ISBN-10: 1726820874 43 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
}
total = total + array[i] ;
}
averg = total / N;
printf("\nSum of all negative numbers = %d\n",negsum);
printf("Sum of all positive numbers = %d\n", posum);
printf("\nAverage of all input numbers = %.2f\n", averg);
} /*End of main()*/
/*-------------------------------------
Output
Enter the value of N
5
Enter 5 numbers (-ve, +ve and zero)
5
-3
0
-7
6
Input array elements
+5
-3
+0
-7
+6

Sum of all negative numbers = -10


Sum of all positive numbers = 11

Average of all input numbers = 0.20


--------------------------------------*/

ISBN-10: 1726820874 44 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
8]/* Write a C program to input real numbers and find the * * mean, variance and
standard deviation */

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float avrg, var, SD, sum=0, sum1=0;

clrscr();

printf("Enter the value of N\n");


scanf("%d", &n);
printf("Enter %d real numbers\n",n);
for(i=0; i<n; i++)
{
scanf("%f", &x[i]);
}

/* Compute the sum of all elements */

for(i=0; i<n; i++)


{
sum = sum + x[i];
}
avrg = sum /(float) n;

/* Compute varaience and standard deviation */

for(i=0; i<n; i++)


{
sum1 = sum1 + pow((x[i] - avrg),2);
}
var = sum1 / (float) n;
SD = sqrt(var);

printf("Average of all elements = %.2f\n", avrg);


printf("Varience of all elements = %.2f\n", var);
printf("Standard deviation = %.2f\n", SD);
} /*End of main()*/
/*--------------------------
Output
Enter the value of N
6
Enter 6 real numbers
12
ISBN-10: 1726820874 45 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
34
10
50
42
33
Average of all elements = 29.66
Varience of all elements = 213.89
Standard deviation = 14.62
-------------------------------------*/

ISBN-10: 1726820874 46 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
9]/* Write a C program to read two matrices A (MxN) and B(MxN) and perform addition
OR subtraction of A and B. Find the trace of the resultant matrix. Output the given matrix,
their sum or Differences and the trace */

#include <stdio.h>
#include <conio.h>

void main()
{
int A[10][10], B[10][10], sumat[10][10], diffmat[10][10];
int i, j, M,N, option;

void trace (int arr[][10], int M, int N);

clrscr();

printf("Enter the order of the matrice A and B\n");


scanf("%d %d", &M, &N);

printf("Enter the elements of matrix A\n");


for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf("%d",&A[i][j]);
}
}

printf("MATRIX A is\n");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}

printf("Enter the elements of matrix B\n");


for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf("%d",&B[i][j]);
}
}

printf("MATRIX B is\n");
for(i=0; i<M; i++)
{
ISBN-10: 1726820874 47 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
for(j=0; j<N; j++)
{
printf("%3d",B[i][j]);
}
printf("\n");
}
printf("Enter your option: 1 for Addition and 2 for Subtraction\n");
scanf("%d",&option);

switch (option)
{
case 1: for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
sumat[i][j] = A[i][j] + B[i][j];
}
}

printf("Sum matrix is\n");


for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",sumat[i][j]) ;
}
printf("\n");
}
trace (sumat, M, N);
break;

case 2:for(i=0; i<M; i++)


{
for(j=0; j<N; j++)
{
diffmat[i][j] = A[i][j] - B[i][j];
}
}
printf("Difference matrix is\n");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",diffmat[i][j]) ;
}
printf("\n");
}

trace (diffmat, M, N);


break;
ISBN-10: 1726820874 48 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
}

} /* End of main() */

/*Function to find the trace of a given matrix and print it*/

void trace (int arr[][10], int M, int N)


{
int i, j, trace = 0;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
if (i==j)
{
trace = trace + arr[i][j];
}
}
}
printf ("Trace of the resultant matrix is = %d\n", trace);

}
/*----------------------------------
Output:

Enter the order of the matrice A and B


22
Enter the elements of matrix A
11
22
MATRIX A is
1 1
2 2
Enter the elements of matrix B
33
44
MATRIX B is
3 3
4 4
Enter your option: 1 for Addition and 2 for Subtraction
1
Sum matrix is
4 4
6 6
Trace of the resultant matrix is = 10
---------------------------------------------*/

ISBN-10: 1726820874 49 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
10]/* Write a C program to read two strings and concatenate them (without using library
functions). Output the concatenated string along with the given string
*/

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
char string1[20], string2[20];
int i,j,pos;

strset(string1, '\0'); /*set all occurrences in two strings to NULL*/


strset(string2,'\0');

printf("Enter the first string :");


gets(string1);
fflush(stdin);

printf("Enter the second string:");


gets(string2);

printf("First string = %s\n", string1);


printf("Second string = %s\n", string2);
/*To concate the second stribg to the end of the string
travserse the first to its end and attach the second string*/

for (i=0; string1[i] != '\0'; i++)


{
; /*null statement: simply trvsering the string1*/
}

pos = i;

for (i=pos,j=0; string2[j]!='\0'; i++)


{
string1[i] = string2[j++];
}
string1[i]='\0'; /*set the last character of string1 to NULL*/

printf("Concatenated string = %s\n", string1);


}
/*---------------------------------------
Output
Enter the first string :CD-
Enter the second string:ROM
First string = CD-
Second string = ROM
Concatenated string = CD-ROM
ISBN-10: 1726820874 50 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
11]/* Write a C program to read N names, store them in the form of an array and sort
them in alphabetical order. Output the give names and the sorted names in two columns
side by side with suitable heading */

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
char name[10][8], Tname[10][8], temp[8];
int i, j, N;

clrscr();
printf("Enter the value of N\n");
scanf("%d", &N);

printf("Enter %d names\n", N);


for(i=0; i< N ; i++)
{
scanf("%s",name[i]);
strcpy (Tname[i], name[i]);
}
for(i=0; i < N-1 ; i++)
{
for(j=i+1; j< N; j++)
{
if(strcmpi(name[i],name[j]) > 0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input Names\tSorted names\n");
printf("------------------------------------------\n");
for(i=0; i< N ; i++)
{
printf("%s\t\t%s\n",Tname[i], name[i]);
}
printf("------------------------------------------\n");

} /* End of main() */
/*--------------------------------
Output
Enter the value of N
3
Enter 3 names
ISBN-10: 1726820874 51 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
Monica
Lucky
Anand

----------------------------------------
Input Names Sorted names
----------------------------------------
Monica Anand
Lucky Lucky
Anand Monica
----------------------------------------
---------------------------------------- */

ISBN-10: 1726820874 52 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
12]/* Write a C program to read a matrix A (MxN) and to find the following using functions
*
a) Sum of the elements of each row *
b) Sum of the elements of each column *
c) Find the sum of all the elements of the matrix *
Output the computed results with suitable headings */
#include <stdio.h>
#include <conio.h>

void main()
{
int arr[10][10];
int i, j, row, col, rowsum, colsum,sumall=0;

/* Function declaration */
int Addrow(int A[10][10], int k, int c);
int Addcol(int A[10][10], int k, int c);

clrscr();

printf("Enter the order of the matrix\n");


scanf("%d %d", &row, &col);

printf("Enter the elements of the matrix\n");


for(i=0; i<row; i++)
{
for(j=0; j< col; j++)
{
scanf("%d", &arr[i][j]);
}
}

printf("Input matrix is\n");


for(i=0; i<row; i++)
{
for(j=0;j<col;j++)
{
printf("%3d", arr[i][j]);
}
printf("\n");
}

/* computing row sum */


for(i=0; i<row; i++)
{
rowsum = Addrow(arr,i,col);
printf("Sum of row %d = %d\n", i+1, rowsum);
}

for(j=0; j<col; j++)


ISBN-10: 1726820874 53 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
{
colsum = Addcol(arr,j,row);
printf("Sum of column %d = %d\n", j+1, colsum);
}

/* computation of all elements */


for(j=0; j< row; j++)
{
sumall = sumall + Addrow(arr,j,col);
}

printf("Sum of all elements of matrix = %d\n", sumall);

/* Function to add each row */


int Addrow(int A[10][10], int k, int c)
{
int rsum=0, i;
for(i=0; i< c; i++)
{
rsum = rsum + A[k][i];
}
return(rsum);
}

/* Function to add each column */


int Addcol(int A[10][10], int k, int r)
{
int csum=0, j;
for(j=0; j< r; j++)
{
csum = csum + A[j][k];
}
return(csum);
}

/*----------------------------
Output
Enter the order of the matrix
3
3
Enter the elements of the matrix
123
456
789
Input matrix is
1 2 3
4 5 6
7 8 9
ISBN-10: 1726820874 54 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
Sum of row 1 = 6
Sum of row 2 = 15
Sum of row 3 = 24
Sum of column 1 = 12
Sum of column 2 = 15
Sum of column 3 = 18
Sum of all elements of matrix = 45

--------------------------------------*/

ISBN-10: 1726820874 55 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
13]/* Write a C program to input N numbers (integers or reals) and store them in an
array. Conduct a linear search for a given key number and report success or failure in the
form of a suitable message */

#include <stdio.h>
#include <conio.h>
void main()
{
int array[10];
int i, N, keynum, found=0;

clrscr();

printf("Enter the value of N\n");


scanf("%d",&N);

printf("Enter the elements one by one\n");


for(i=0; i<N ; i++)
{
scanf("%d",&array[i]);
}
printf("Input array is\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
printf("Enter the element to be searched\n");
scanf("%d", &keynum);

/* Linear search begins */


for ( i=0; i < N ; i++)
{
if( keynum == array[i] )
{
found = 1;
break;
}
}
if ( found == 1)
printf("SUCCESSFUL SEARCH\n");
else
printf("Search is FAILED\n");

} /* End of main */
/*------------------------------------
Output
RUN 1
Enter the value of N
5
Enter the elements one by one
ISBN-10: 1726820874 56 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
23
12
56
43
89
Input array is
23
12
56
43
89
Enter the element to be searched
56
SUCCESSFUL SEARCH

RUN 2
Enter the value of N
3
Enter the elements one by one
456
213
879
Input array is
456
213
879
Entee the element to be searched
1000
Search is FAILED
--------------------------------------*/

ISBN-10: 1726820874 57 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
14]/* Write a C program to read A (MxN), find the transpose of a given matrix and output
both the input matrix and the transposed matrix. */

#include <stdio.h>
#include <conio.h>

void main()
{
int i,j,M,N;
int A[10][10], B[10][10];

int transpose(int A[][10], int r, int c); /*Function prototype*/


clrscr();

printf("Enter the order of matrix A\n");


scanf("%d %d", &M, &N);

printf("Enter the elements of matrix\n");


for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
scanf("%d",&A[i][j]);
}
}

printf("Matrix A is\n");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}

/* Finding Transpose of matrix*/


for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
B[i][j] = A[j][i];
}
}

printf("Its Transpose is\n");


for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
ISBN-10: 1726820874 58 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
printf("%3d",B[i][j]);
}
printf("\n");
}

} /*End of main()*/
/*---------------------------------------
Output
Enter the order of matrix A
33
Enter the elements of matrix
1
2
3
4
5
6
7
8
9
MatrixxA is
1 2 3
4 5 6
7 8 9
Its Transpose is
1 4 7
2 5 8
3 6 9
-----------------------------*/

ISBN-10: 1726820874 59 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
15]/* Write a C program to read an English sentence and replace lowercase characters by
uppercase and vice-versa. Output the given sentence as well as the case converted sentence
on two different lines. */

#include <stdio.h>
#include <ctype.h>
#include <conio.h>

void main()
{
char sentence[100];
int count, ch, i;

clrscr();

printf("Enter a sentence\n");
for(i=0; (sentence[i] = getchar())!='\n'; i++)
{
;
}

sentence[i]='\0';

count = i; /*shows the number of chars accepted in a sentence*/

printf("The given sentence is : %s",sentence);

printf("\nCase changed sentence is: ");


for(i=0; i < count; i++)
{
ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);
putchar(ch);
}

} /*End of main()*/
/*------------------------------
Output
Enter a sentence
Mera Bharat Mahan
The given sentence is : Mera Bhaaat Mahan
Case changed sentence is: mERA bHARAT mAHAN
------------------------------------------------*/

ISBN-10: 1726820874 60 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
16]/* Write a C program to sort given N elements using SELECTION sort method using
functions
a) To find maximum of elements
b) To swap two elements */

#include <stdio.h>
#include <conio.h>

void main()
{
int array[10];
int i, j, N, temp;

int findmax(int b[10], int k); /* function declaration */


void exchang(int b[10], int k);

clrscr();

printf("Enter the value of N\n");


scanf("%d",&N);

printf("Enter the elements one by one\n");


for(i=0; i<N ; i++)
{
scanf("%d",&array[i]);

printf("Input array elements\n");


for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}

/* Selection sorting begins */


exchang(array,N);

printf("Sorted array is...\n");


for(i=0; i< N ; i++)
{
printf("%d\n",array[i]);
}

} /* End of main*/

/* function to find the maximum value */


int findmax(int b[10], int k)
{
int max=0,j;
for(j = 1; j <= k; j++)
ISBN-10: 1726820874 61 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
{
if ( b[j] > b[max])
{
max = j;
}
}
return(max);
}

void exchang(int b[10],int k)


{
int temp, big, j;
for ( j=k-1; j>=1; j--)
{

big = findmax(b,j);
temp = b[big];
b[big] = b[j];
b[j] = temp;
}
return;
}

/*-----------------------------
Output
Enter the value of N
5
Enter the elements one by one
45
12
90
33
78
Input array elements
45
12
90
33
78
Sorted array is
12
33
45
78
90
-----------------------------------*/

ISBN-10: 1726820874 62 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
17]/* Write a C program to accept a string and a substring and
check if the substring is present in the given string */

#include<stdio.h>
#include<conio.h>
void main()
{
char str[80],search[10];
int count1=0,count2=0,i,j,flag;

clrscr();

puts("Enter a string:");
gets(str);

puts("Enter search substring:");


gets(search);

while (str[count1]!='\0')
count1++;

while (search[count2]!='\0')
count2++;

for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts("SEARCH SUCCESSFUL!");
else
puts("SEARCH UNSUCCESSFUL!");
getch();
}
/*------------------------------
Output
Enter a string:
Hello how are you?
Enter search substring:
How SEARCH SUCCESSFUL!
ISBN-10: 1726820874 63 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
18]/* Program to accepts two strings and compare them. Finally it prints whether both are
equal, or first string is greater than the second or the first string is less than the second
string */

#include<stdio.h>
#include<conio.h>

void main()
{
int count1=0,count2=0,flag=0,i;
char str1[10],str2[10];

clrscr();
puts("Enter a string:");
gets(str1);

puts("Enter another string:");


gets(str2);
/*Count the number of characters in str1*/
while (str1[count1]!='\0')
count1++;
/*Count the number of characters in str2*/
while (str2[count2]!='\0')
count2++;
i=0;

/*The string comparison starts with the first character in each string and
continues with subsequent characters until the corresponding characters
differ or until the end of the strings is reached.*/

while ( (i < count1) && (i < count2))


{
if (str1[i] == str2[i])
{
i++;
continue;
}
if (str1[i]<str2[i])
{
flag = -1;
break;
}
if (str1[i] > str2[i])
{
flag = 1;
break;
}
}
if (flag==0)
printf("Both strings are equal\n");
ISBN-10: 1726820874 64 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
if (flag==1)
printf("String1 is greater than string2\n", str1, str2);
if (flag == -1)
printf("String1 is less than string2\n", str1, str2);
getch();
}
/*----------------------------------------
Output
Enter a string:
happy
Enter another string:
HAPPY
String1 is greater than string2

RUN2
Enter a string:
Hello
Enter another string:
Hello
Both strings are equal

RUN3
Enter a string:
gold
Enter another string:
silver
String1 is less than string2
----------------------------------------*/

ISBN-10: 1726820874 65 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
19]/* Write a c program to find the length of a string
without using the built-in function also check whether it is a palindrome or not */

#include <stdio.h>
#include <string.h>

void main()
{
char string[25], revString[25]={'\0'};
int i,length = 0, flag = 0;

clrscr();
fflush(stdin);

printf("Enter a string\n");
gets(string);

for (i=0; string[i] != '\0'; i++) /*keep going through each


{ /*character of the string */
length++; /*till its end */
}
printf("The length of the string \'%s\' = %d\n", string, length);

for (i=length-1; i >= 0 ; i--)


{
revString[length-i-1] = string[i];
}
/*Compare the input string and its reverse. If both are equal then the input string is
palindrome. Otherwise it is
not a palindrome */

for (i=0; i < length ; i++)


{
if (revString[i] == string[i])
flag = 1;
else
flag = 0;
}

if (flag == 1)
printf ("%s is a palindrome\n", string);
else
printf("%s is not a palindrome\n", string);

/*----------------------------------------------------
Output
Enter a string
madam
ISBN-10: 1726820874 66 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
The length of the string 'madam' = 5
madam is a palindrome

RUN2
Enter a string
good
The length of the string 'good' = 4
good is not a palindrome
----------------------------------------------------------*/

ISBN-10: 1726820874 67 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
20]/* Write a C program to accept a matrix of given order and
interchange any two rows and columns in the original matrix*/

#include <stdio.h>
void main()
{
static int m1[10][10],m2[10][10];
int i,j,m,n,a,b,c, p, q, r;

printf ("Enter the order of the matrix\n");


scanf ("%d %d",&m,&n);

printf ("Enter the co-efficents of the matrix\n");


for (i=0; i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d,",&m1[i][j]);
m2[i][j] = m1[i][j];
}
}

printf ("Enter the numbers of two rows to be exchnged \n");


scanf ("%d %d", &a,&b);

for (i=0;i<m;++i)
{
c = m1[a-1][i]; /* first row has index is 0 */
m1[a-1][i] = m1[b-1][i];
m1[b-1][i] = c;
}

printf ("Enter the numbers of two columns to be exchnged\n");


scanf ("%d %d",&p,&q);

printf ("The given matrix is \n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
printf (" %d",m2[i][j]);
printf ("\n");
}

for (i=0;i<n;++i)
{
r = m2[i][p-1]; /* first column index is 0 */
m2[i][p-1] = m2[i][q-1];
m2[i][q-1] = r;
}

ISBN-10: 1726820874 68 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
printf ("The matix after interchnging the two rows(in the original matrix)\n");
for (i=0; i<m; ++i)
{
for (j=0; j<n; ++j)
{
printf (" %d",m1[i][j]);
}
printf ("\n");
}

printf("The matix after interchnging the two columns(in the original matrix)\n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
printf (" %d", m2[i][j]);
printf ("\n");
}
}
/*-------------------------------------------------------
output
Enter the order of the matrix
33
Enter the co-efficents of the matrix
124
579
306
Enter the numbers of two rows to be exchnged
12
Enter the numbers of two columns to be exchnged
23
The given matrix is
124
579
306
The matix after interchnging the two rows (in the original matrix)
579
124
306
The matix after interchnging the two columns(in the original matrix)
142
597
360
-------------------------------------------------------------------------*/

ISBN-10: 1726820874 69 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
21]/* Program to accept two strings and concatenate them i.e. The second string is
appended to the end of the first string */

#include <stdio.h>
#include <string.h>

void main()
{
char string1[20], string2[20];
int i,j,pos;

printf("Enter the first string :");


gets(string1);
fflush(stdin);

printf("Enter the second string:");


gets(string2);

printf("First string = %s\n", string1);


printf("Second string = %s\n", string2);

/*To concate the second stribg to the end of the string


travserse the first to its end and attach the second string*/

for (i=0; string1[i] != '\0'; i++) ;

pos = i;

for (i=pos, j=0; string2[j]!='\0'; i++, j++)


{
string1[i] = string2[j];
}

string1[i]='\0'; /*set the last character of string1 to NULL*/

printf("Concatenated string = %s\n", string1);


}
/*---------------------------------------
Output
Enter the first string :CD-
Enter the second string:ROM
First string = CD-
Second string = ROM
Concatenated string = CD-ROM
----------------------------------------*/

ISBN-10: 1726820874 70 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
22]/* Write a C Program to accept two matrices and check if they are equal */

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void main()
{
int A[10][10], B[10][10];
int i, j, R1, C1, R2, C2, flag =1;

printf("Enter the order of the matrix A\n");


scanf("%d %d", &R1, &C1);

printf("Enter the order of the matrix B\n");


scanf("%d %d", &R2,&C2);

printf("Enter the elements of matrix A\n");


for(i=0; i<R1; i++)
{
for(j=0; j<C1; j++)
{
scanf("%d",&A[i][j]);
}
}

printf("Enter the elements of matrix B\n");


for(i=0; i<R2; i++)
{
for(j=0; j<C2; j++)
{
scanf("%d",&B[i][j]);
}
}

printf("MATRIX A is\n");
for(i=0; i<R1; i++)
{
for(j=0; j<C1; j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}

printf("MATRIX B is\n");
for(i=0; i<R2; i++)
{
for(j=0; j<C2; j++)
{
ISBN-10: 1726820874 71 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
printf("%3d",B[i][j]);
}
printf("\n");
}

/* Comparing two matrices for equality */

if(R1 == R2 && C1 == C2)


{
printf("Matrices can be compared\n");
for(i=0; i<R1; i++)
{
for(j=0; j<C2; j++)
{
if(A[i][j] != B[i][j])
{
flag = 0;
break;
}
}
}
}
else
{ printf(" Cannot be compared\n");
exit(1);
}

if(flag == 1 )
printf("Two matrices are equal\n");
else
printf("But,two matrices are not equal\n");

/*------------------------------------------------------
Output
Enter the order of the matrix A
22
Enter the order of the matrix B
22
Enter the elements of matrix A
12
34
Enter the elements of matrix B
12
34
MATRIX A is
1 2
3 4
MATRIX B is
ISBN-10: 1726820874 72 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
1 2
3 4
Matrices can be compared
Two matrices are equal
-------------------------------------------------------*/

ISBN-10: 1726820874 73 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
23]* Write a C Program to check if a given matrix is an identity matrix */

#include <stdio.h>

void main()
{
int A[10][10];
int i, j, R, C, flag =1;

printf("Enter the order of the matrix A\n");


scanf("%d %d", &R, &C);

printf("Enter the elements of matrix A\n");


for(i=0; i<R; i++)
{
for(j=0; j<C; j++)
{
scanf("%d",&A[i][j]);
}
}
printf("MATRIX A is\n");
for(i=0; i<R; i++)
{
for(j=0; j<C; j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}

/* Check for unit (or identity) matrix */

for(i=0; i<R; i++)


{
for(j=0; j<C; j++)
{
if(A[i][j] != 1 && A[j][i] !=0)
{
flag = 0;
break;
}
}
}

if(flag == 1 )
printf("It is identity matrix\n");

else
printf("It is not a identity matrix\n");
}
ISBN-10: 1726820874 74 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
/*------------------------------------------
Output
Run 1
Enter the order of the matrix A
22
Enter the elements of matrix A
22
12
MATRIX A is
2 2
1 2
It is not a identity matrix

Run 2
Enter the order of the matrix A
22
Enter the elements of matrix A
10
01
MATRIX A is
1 0
0 1
It is identity matrix
------------------------------------------*/

ISBN-10: 1726820874 75 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
24]/* Write a C program to find the sum of all elements of
an array using pointers as arguments */

#include <stdio.h>

void main()
{
static int array[5]={ 200,400,600,800,1000 };
int sum;

int addnum(int *ptr); /* function prototype */

sum = addnum(array);

printf("Sum of all array elements = %5d\n", sum);

} /* End of main() */

int addnum(int *ptr)


{
int index, total=0;

for(index = 0; index < 5; index++)


{
total += *(ptr+index);
}
return(total);
}
/*-----------------------------------
Output
Sum of all array elements = 3000
------------------------------------*/

ISBN-10: 1726820874 76 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
25]/* This program accepts an array of N elements and a key. Then it searches for the
desired element. If the search is successful, it displays "SUCCESSFUL SEARCH".
Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed. */

#include <stdio.h>
void main()
{
int table[20];
int i, low, mid, high, key, size;

printf("Enter the size of an array\n");


scanf("%d", &size);

printf("Enter the array elements\n");


for(i = 0; i < size; i++)
{
scanf("%d", &table[i]);
}

printf("Enter the key\n");


scanf("%d", &key);

/* search begins */

low = 0;
high = (size - 1);

while(low <= high)


{
mid = (low + high)/2;
if(key == table[mid])
{
printf("SUCCESSFUL SEARCH\n");
return;
}
if(key < table[mid])
high = mid - 1;
else
low = mid + 1;
}

printf("UNSUCCESSFUL SEARCH\n");
} /* End of main() */
/*----------------------------------
Output
Output
Enter the size of an array
5
Enter the array elements
12
ISBN-10: 1726820874 77 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
36
45
78
99
Enter the key
45
SUCCESSFUL SEARCH
-------------------------------------*/

ISBN-10: 1726820874 78 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
26]/* Write a C program to find the frequency of odd numbers and even numbers
in the input of a matrix */

#include <stdio.h>
void main ()
{
static int ma[10][10];
int i,j,m,n,even=0,odd=0;

printf ("Enter the order ofthe matrix \n");


scanf ("%d %d",&m,&n);

printf ("Enter the coefficients if matrix \n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d", &ma[i][j]);
if ((ma[i][j]%2) == 0)
{
++even;
}
else
++odd;
}
}
printf ("The given matrix is\n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}

printf ("\nThe frequency of occurance of odd number = %d\n",odd);


printf ("The frequency of occurance of even number = %d\n",even);

} /* End of main() */

/*-----------------------------------------------------
Output
Enter the order ofthe matrix
33
Enter the coefficients if matrix
123
456
789
The given matrix is
ISBN-10: 1726820874 79 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
123
456
789

The frequency of occurance of odd number = 5


The frequency of occurance of even number = 4

-------------------------------------------------------*/

ISBN-10: 1726820874 80 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
27]/* Write a C program to find accept a matric of order M x N and find the sum of the
main diagonal and off diagonal elements */

#include <stdio.h>

void main ()
{
static int ma[10][10];
int i,j,m,n,a=0,sum=0;

printf ("Enetr the order of the matix \n");


scanf ("%d %d",&m,&n);

if ( m == n )
{
printf ("Enter the co-efficients of the matrix\n");

for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&ma[i][j]);
}
}

printf ("The given matrix is \n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}

for (i=0;i<m;++i)
{
sum = sum + ma[i][i];
a = a + ma[i][m-i-1];
}

printf ("\nThe sum of the main diagonal elements is = %d\n",sum);


printf ("The sum of the off diagonal elemets is = %d\n",a);
}
else
printf ("The givan order is not square matrix\n");
} /* End of main() */

/*--------------------------------------------------------
Output
ISBN-10: 1726820874 81 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
Enetr the order of the matix
33
Enter the co-efficients of the matrix
123
456
789
The given matrix is
123
456
789

The sum of the main diagonal elements is = 15


The sum of the off diagonal elemets is = 15

Run 2
Enetr the order of the matix
23
The givan order is not square matrix

--------------------------------------------------------*/

ISBN-10: 1726820874 82 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
28]/* Write a C program to accept a matrix and determine whether it is a sparse matrix. A
sparse martix is matrix which as more zero elements than nonzero elements */

#include <stdio.h>

void main ()
{
static int m1[10][10];
int i,j,m,n;
int counter=0;

printf ("Enter the order of the matix\n");


scanf ("%d %d",&m,&n);

printf ("Enter the co-efficients of the matix\n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&m1[i][j]);
if (m1[i][j]==0)
{
++counter;
}
}
}
if (counter>((m*n)/2))
{
printf ("The given matrix is sparse matrix \n");
}
else
printf ("The given matrix is not a sparse matrix \n");

printf ("There are %d number of zeros",counter);

} /* END of main() */
/*----------------------------------------------
Output
Enter the order of the matix
22
Enter the co-efficients of the matix
12
34
The given matrix is not a sparse matrix
There are 0 number of zeros

Run 2
Enter the order of the matix
33
Enter the co-efficients of the matix
ISBN-10: 1726820874 83 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
100
001
010
The given matrix is sparse matrix
There are 6 number of zeros
-----------------------------------------------*/

ISBN-10: 1726820874 84 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
29]/* Write a C Program to convert the lower case letters to upper case and vice-versa
*/

#include <stdio.h>
#include <ctype.h>
#include <conio.h>

void main()
{
char sentence[100];
int count, ch, i;
clrscr();

printf("Enter a sentence\n");
for(i=0; (sentence[i] = getchar())!='\n'; i++)
{
;
}
count = i;

printf("\nInput sentence is : %s ",sentence);

printf("\nResultant sentence is : ");


for(i=0; i < count; i++)
{
ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);
putchar(ch);
}

} /*End of main()

/*-----------------------------------------
Output
Enter a sentence
gOOD mORNING

Input sentence is : gOOD mORNING

Resultant sentence is :Good Morning


------------------------------------------*/

ISBN-10: 1726820874 85 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
30]/* Write a C program to accept a matrix of order M x N and store its elements and
interchange the main diagonal elements of the matrix with that of the secondary diagonal
elements */

#include <stdio.h>

void main ()
{

static int ma[10][10];


int i,j,m,n,a;

printf ("Enetr the order of the matix \n");


scanf ("%d %d",&m,&n);

if (m ==n )
{
printf ("Enter the co-efficients of the matrix\n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%dx%d",&ma[i][j]);
}
}

printf ("The given matrix is \n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}

for (i=0;i<m;++i)
{
a = ma[i][i];
ma[i][i] = ma[i][m-i-1];
ma[i][m-i-1] = a;
}

printf ("THe matrix after changing the \n");


printf ("main diagonal & secondary diagonal\n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
ISBN-10: 1726820874 86 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
}
printf ("\n");
}
}
else
printf ("The givan order is not square matrix\n");

} /* end of main() */

/*----------------------------------------------------
Output
Enetr the order of the matix
33
Enter the co-efficients of the matrix
123
456
789
The given matrix is
123
456
789
THe matrix after changing the
main diagonal & secondary diagonal
321
456
987
----------------------------------------------------------*/

ISBN-10: 1726820874 87 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
31]/* Write a C program to accept a matrix of order MxN and sort all rows of the matrix in
ascending order and all columns in descendng order */

#include <stdio.h>

void main ()
{
static int ma[10][10],mb[10][10];
int i,j,k,a,m,n;

printf ("Enter the order of the matrix \n");


scanf ("%d %d", &m,&n);

printf ("Enter co-efficients of the matrix \n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&ma[i][j]);
mb[i][j] = ma[i][j];
}
}

printf ("The given matrix is \n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}

printf ("After arranging rows in ascending order\n");


for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
for (k=(j+1);k<n;++k)
{
if (ma[i][j] > ma[i][k])
{
a = ma[i][j];
ma[i][j] = ma[i][k];
ma[i][k] = a;
}
}
}
} /* End of outer for loop*/

ISBN-10: 1726820874 88 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("\n");
}

printf ("After arranging the columns in descending order \n");


for (j=0;j<n;++j)
{
for (i=0;i<m;++i)
{
for (k=i+1;k<m;++k)
{
if (mb[i][j] < mb[k][j])
{
a = mb[i][j];
mb[i][j] = mb[k][j];
mb[k][j] = a;
}
}
}
} /* End of outer for loop*/

for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",mb[i][j]);
}
printf ("\n");
}
} /*End of main() */
/*------------------------------------
Output: Enter the order of the matrix
22
Enter co-efficients of the matrix
31
52
The given matrix is
31
52
After arranging rows in ascending order
13
25
After arranging the columns in descending order
52
31
ISBN-10: 1726820874 89 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
32]/* Write a C program to accept two matrices and
find the sum and difference of the matrices */

#include <stdio.h>
#include <stdlib.h>

void main()
{
int A[10][10], B[10][10], sum[10][10], diff[10][10];
int i, j, R1, C1, R2, C2;

printf("Enter the order of the matrix A\n");


scanf("%d %d", &R1, &C1);

printf("Enter the order of the matrix B\n");


scanf("%d %d", &R2,&C2);

if( R1 != R2 && C1 != C2)


{
printf("Addition and subtraction is not possible\n");
}
else
{
printf("Enter the elements of matrix A\n");

for(i=0; i<R1; i++)


{
for(j=0; j<C1; j++)
{
scanf("%d",&A[i][j]);
}
}

printf("MATRIX A is\n");

for(i=0; i<R1; i++)


{
for(j=0; j<C1; j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}

printf("Enter the elements of matrix B\n");


for(i=0; i<R2; i++)
{
for(j=0; j<C2; j++)
{
scanf("%d",&B[i][j]);
ISBN-10: 1726820874 90 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
}
}
printf("MATRIX B is\n");

for(i=0; i<R2; i++)


{
for(j=0; j<C2; j++)
{
printf("%3d",B[i][j]);
}
printf("\n");
}

/*To find the sum of elements of matrix A and Matrix B*/


for(i=0; i<R1; i++)
{
for(j=0; j<C2; j++)
{
sum[i][j] = A[i][j] + B[i][j];
}
}

printf("Sum matrix is\n");


for(i=0; i<R1; i++)
{
for(j=0; j<C2; j++)
{
printf("%d\t", sum[i][j]) ;
}
printf("\n");
}

/*Function to find the difference of matrix A and Matrix B*/


for(i=0; i<R1; i++)
{
for(j=0; j<C2; j++)
{
diff[i][j] = A[i][j] - B[i][j];
}
}

printf("Difference matrix is\n");


for(i=0; i<R1; i++)
{
for(j=0; j<C2; j++)
{
printf("%d\t", diff[i][j]);
}
printf("\n");
}
ISBN-10: 1726820874 91 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra

} /*End of else statement */

} /*End of main function */

/*---------------------------------------------------
Output
Enter the order of the matrix A
22
Enter the order of the matrix B
22
Enter the elements of matrix A
1
2
3
4
MATRIX A is
1 2
3 4
Enter the elements of matrix B
2
4
6
8
MATRIX B is
2 4
6 8
Sum matrix is
3 6
9 12
Difference matrix is
-1 -2
-3 -4
-----------------------------------------------------*/

ISBN-10: 1726820874 92 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra

Module-III

ISBN-10: 1726820874 93 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
1)/* Write a C program to find the size of a union*/

#include <stdio.h>

void main()
{
union sample
{
int m;
float n;
char ch;
};

union sample u;

printf("The size of union =%d\n", sizeof(u));

/*initialization */

u.m = 25;
printf("%d %f %c\n", u.m, u.n,u.ch);

u.n = 0.2;
printf("%d %f %c\n", u.m, u.n,u.ch);

u.ch = 'p';
printf("%d %f %c\n", u.m, u.n,u.ch);
} /*End of main() */
/*-----------------------------------------
Output
The size of union =4
25 12163373596672.000000
-13107 0.200000 Í
-13200 0.199999 p
------------------------------------------*/

ISBN-10: 1726820874 94 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
2)/* Write a C program to create a file called emp.rec and store information about
a person, in terms of his name, age and salary.*/

#include <stdio.h>

void main()
{
FILE *fptr;
char name[20];
int age;
float salary;

fptr = fopen ("emp.rec", "w"); /*open for writing*/

if (fptr == NULL)
{
printf("File does not exists\n");
return;
}

printf("Enter the name\n");


scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);

printf("Enter the age\n");


scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);

printf("Enter the salary\n");


scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);

fclose(fptr);
}
/*---------------------------------------------------------------------------
Output
Enter the name
Prabhu
Enter the age
25
Enter the salary
25000
-------------------------------------
Please note that you have to open the file called emp.rec in the directory

Name = Prabhu
ISBN-10: 1726820874 95 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
Age = 25
Salary = 25000.00
-----------------------------------------------------------------------------*/

ISBN-10: 1726820874 96 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
3)/* Write a C program to find the sum of two one-dimensional arrays using
Dynamic Memory Allocation */

#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>

void main()
{
int i,n;
int *a,*b,*c;

printf("How many Elements in each array...\n");


scanf("%d", &n);

a = (int *) malloc(n*sizeof(int));
b = (int *) malloc(n*sizeof(int));
c =( int *) malloc(n*sizeof(int));

printf("Enter Elements of First List\n");


for(i=0;i<n;i++)
{
scanf("%d",a+i);
}

printf("Enter Elements of Second List\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 List is\n");


for(i=0;i<n;i++)
{
printf("%d\n",*(c+i));
}

} /* End of main() */

/*---------------------------------------
Output
ISBN-10: 1726820874 97 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
How many Elements in each array...
4
Enter Elements of First List
1
2
3
4
Enter Elements of Second List
6
7
8
9
Resultant List is
7
9
11
13
----------------------------------------*/

ISBN-10: 1726820874 98 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
4]/* Write a C program to accept an array of 10 elements and swap 3rd element
with 4th element using pointers. And display the results */

#include <stdio.h>

void main()
{

float x[10];
int i,n;

void swap34(float *ptr1, float *ptr2 ); /* Function Declaration */

printf("How many Elements...\n");

scanf("%d", &n);

printf("Enter Elements one by one\n");

for(i=0;i<n;i++)
{
scanf("%f",x+i);
}

swap34(x+2, x+3); /* Function call:Interchanging 3rd element by 4th */

printf("\nResultant Array...\n");
for(i=0;i<n;i++)
{
printf("X[%d] = %f\n",i,x[i]);
}
} /* End of main() */

/* Function to swap the 3rd element with the 4th element in the array */

void swap34(float *ptr1, float *ptr2 ) /* Function Definition */


{
float temp;

temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
} /* End of Function */

/*-------------------------------------------
Output
ISBN-10: 1726820874 99 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra
How many Elements...
10
Enter Elements one by one
10
20
30
40
50
60
70
80
90
100

Resultant Array...
X[0] = 10.000000
X[1] = 20.000000
X[2] = 40.000000
X[3] = 30.000000
X[4] = 50.000000
X[5] = 60.000000
X[6] = 70.000000
X[7] = 80.000000
X[8] = 90.000000
X[9] = 100.000000
----------------------------------------------------*/

ISBN-10: 1726820874 100 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
5]/* Write a C program to accept a set of names and sort them in an alphabetical
order, Use structures to store the names */

#include<stdio.h>
#include<conio.h>
#include<string.h>

struct person
{
char name[10];
int rno;
};
typedef struct person NAME;
NAME stud[10], temp[10];

void main()
{
int no,i;

void sort(int N); /* Function declaration */

clrscr();
fflush(stdin);

printf("Enter the number of students in the list\n");


scanf("%d",&no);

for(i = 0; i < no; i++)


{
printf("\nEnter the name of person %d : ", i+1);
fflush(stdin);
gets(stud[i].name);

printf("Enter the roll number of %d : ", i+1);


scanf("%d",&stud[i].rno);
temp[i] = stud[i];
}

printf("\n*****************************\n");
printf (" Names before sorting \n");
/* Print the list of names before sorting */
for(i=0;i<no;i++)
{
printf("%-10s\t%3d\n",temp[i].name,temp[i].rno);
}

ISBN-10: 1726820874 101 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra
sort(no); /* Function call */

printf("\n*****************************\n");
printf (" Names after sorting \n");
printf("\n*****************************\n");
/* Display the sorted names */
for(i=0;i<no;i++)
{
printf("%-10s\t%3d\n",stud[i].name,stud[i].rno);

}
printf("\n*****************************\n");
} /* End of main() */

/* Function to sort the given names */


void sort(int N)
{
int i,j;
NAME temp;

for(i = 0; i < N-1;i++)


{
for(j = i+1; j < N; j++)
{
if(strcmp(stud[i].name,stud[j].name) > 0 )
{
temp = stud[i];
stud[i] = stud[j];
stud[j] = temp;
}
}
}
} /* end of sort() */

Output:
/*--------------------------------------------------------
Enter the number of students in the list
5
Enter the name of person 1 : Rajashree
Enter the roll number of 1 : 123

Enter the name of person 2 : John


Enter the roll number of 2 : 222

Enter the name of person 3 : Priya


Enter the roll number of 3 : 331
ISBN-10: 1726820874 102 ISBN-13: 978-1726820875
Author: Hitesh Mohapatra

Enter the name of person 4 : Anand


Enter the roll number of 4 : 411

Enter the name of person 5 : Nirmala


Enter the roll number of 5 : 311

*****************************
Names before sorting
Rajashree 123
John 222
Priya 331
Anand 411
Nirmala 311

*****************************
Names after sorting

*****************************
Anand 411
John 222
Nirmala 311
Priya 331
Rajashree 123

*****************************
----------------------------------------------------*/

ISBN-10: 1726820874 103 ISBN-13: 978-1726820875


Author: Hitesh Mohapatra

ISBN-10: 1726820874 104 ISBN-13: 978-1726820875

View publication stats

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy