PPS- LAB MANUAL
PPS- LAB MANUAL
(1805ES01)
LAB MANUAL
I BTECH I SEMESTER
Course Objectives:
The students will learn the following:
To work with an IDE to create, edit, compile, run and debug programs
To analyze the various steps in program development.
To develop programs to solve basic problems by understanding basic concepts in C like
operators, control statements etc.
To develop modular, reusable and readable C Programs using the concepts like
functions, arrays etc.
To write programs using the Dynamic Memory Allocation concept.
To create, read from and write to text and binary files
Course Outcomes:
The candidate is expected to be able to:
CO1 H M M H M H H
CO2 H M M M H H M
CO3 H H H H M H
CO4 H H M
CO5 H H M H
CO6 M H L
CO7 H H H H M H
CO8 H H M H
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
INDEX
List Of Contents Page
S. No
No
Practice sessions
a. Write a simple program that prints the results of all the operators
available in C (including pre/ post increment , bitwise and/or/not ,
1
etc.). Read required operand values from standard input.
1 b. Write a simple program that converts one given data type to
another using auto conversion and casting. Take the values form
standard input. 3
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India)
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India)
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Files
a. Write a C program to display the contents of a file to standard
56
output device.
b. Write a C program which copies one file to another, replacing all
57
lowercase characters with their uppercase equivalents.
c. Write a C program to count the number of times a character occurs
in a text file. The file name and the character are supplied as 59
commandline arguments.
d. Write a C program that does the following: It should first create a
7 binary file and store 10 integers, where the file name and 10 values
are given in the command line. (hint: convert the strings using atoi
function) Now the program asks for an index and a value from the 60
user and the value at that index should be changed to the new
value inthe file. (hint: use fseek function) The program should then
read all 10 values and print them back.
e. Write a C program to merge two files into a third file (i.e., the
contents of the firs t file followed by those of the second are put in 62
the third file).
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India)
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Miscellaneous
a. Write a menu driven C program that allows a user to enter n
numbers and then choose between finding the smallest, largest, sum,
or average. The menu and all the choices are to be functions. Use a 64
switch statement to determine what action to take. Display an error
message if an invalid choice is entered.
b. Write a C program to construct a pyramid of numbers as follows:
(i) 1
12 67
123
1234
(ii) *
** 68
***
(iii) 1
8 23 69
456
(iv) 1
22
70
333
4444
(v) *
**
***
****
71
****
***
**
*
C.Write a C Program implements Student Data Base System Using Files
& Structures. 73
Searching and Sorting
a. Write a C program that uses non recursive function to search for a
Key value in a given list of integers using linear search method. 80
b. Write a C program that uses non recursive function to search for a
Key value in a given sorted list of integers using binary search method. 82
c. Write a C program that implements the Bubble sort method to sort a
given list of integers in ascending order. 84
9
d. Write a C program that sorts the given array of integers using
selection sort in descending order 86
e. Write a C program that sorts the given array of integers using
88
insertion sort in ascending order
f. Write a C program that sorts a given array of names. 90
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India)
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
1. PRACTISE SESSION
1. a) Write a simple program that prints the results of all the operators
available in C (including pre/ post increment, bitwise and/or/not , etc.).
Read required operand values from standard input.
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 1
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
OUTPUT:
addition of a and b is 50
substraction of a and b is -10
multiplication of a and b is 600
remainder of a and b is 2
division of a and b is 0
Logical And=0
Logical OR NOT=1
Bitwise And 20
Bitwise Or 30
Bitwise NOT 0
Bitwise complement -21
Bitwise XOR 10
Bitwise Shift right 5
Bitwise Shift left 240
integer size= 2,floating point size= 4
conditional expression=200
preincrement 21
postincrement 30
predecrement 20
postdecrement 31
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 2
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
1.b) Write a simple program that converts one given data type to another
using auto conversion and casting. Take the values form standard input.
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 3
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
2 a) Write a program for find the max and min from the three numbers.
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("Maximum number is a = %d",a);
else if(b>a && b>c)
printf("Maximum number is b = %d",b);
else
printf("Maximum number is c = %d",c);
if(a<b && a<c)
printf("Minimum number is a = %d",a);
else if(b<a && b<c)
printf("Minimum number is b = %d",b);
else
printf("Minimum number is c = %d",c);
}
OUTPUT:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 4
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source code:
#include<stdio.h>
#include<math.h>
int main()
{
float p,q,r,SI,CI;
int n;
printf("Enter the value of Principal p =
"); scanf("%f",&p);
printf("Enter the value of Rate r =
"); scanf("%f",&r);
printf("Enter the value of Period in year n = ");
scanf("%d",&n);
SI =
((p*r*n)/100);
printf("Simple Interest SI=%f \n",SI);
q = 1+(r/100);
CI=p*pow(q,n)-p;
printf("Compound Interest CI=%f \n",CI);
return 0;
}
INPUT:
Enter the value of Principal p =1000
Enter the value of Rate r =12
Enter the value of Period in year n =2
OUTPUT:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 5
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
2. c) Write program that declares Class awarded for a given percentage of marks,
where mark <40%= Failed, 40% to <60% = Second class, 60% to <70%=First class,
>=70% = Distinction. Read percentage from standard input.
Source Code:
printf("Failed");
}
return 0;
}
INPUT:
Enter five subjects marks:
95 95 97 98 90
OUTPUT:
Percentage= 95.00
Distinction
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 6
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
2 d) Write a program that prints a multiplication table for a given number and
the numberof rows in the table. For example, for a number 5 and rows = 3, the
output should be:
5x1=5
5 x 2 = 10
5 x 3 = 15
AIM: To prints a multiplication table
Source Code:
#include <stdio.h>
int main()
{
int n, i,rows;
printf("Enter an integer: ");
scanf("%d",&n);
printf(“enter the number of rows\n”);
scanf(“%d”,&rows);
for(i=1; i<=rows; i++)
{
printf("%d * %d = %d \n", n, i, n*i);
}
return 0;
}
INPUT:
Enter an integer:5
enter the number of rows: 3
OUTPUT:
5x1=5
5 x 2 = 10
5 x 3 = 15
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 7
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
#include <stdio.h>
#include <math.h>
long decimalToBinary(int decimalnum)
{
long binarynum = 0;
int rem, temp = 1;
while (decimalnum!=0)
{
rem = decimalnum%2;
decimalnum = decimalnum / 2;
binarynum = binarynum + rem*temp;
temp = temp * 10;
}
return binarynum;
}
int main()
{
int decimalnum;
printf("Enter a Decimal Number: ");
scanf("%d", &decimalnum);
printf("Equivalent Binary Number is: %ld",
decimalToBinary(decimalnum));
return 0;
}
INPUT:
OUTPUT:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 8
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
3. EXPRESSION EVALUATION
3 a)A building has 10 floors with a floor height of 3 meters each. A ball is
dropped from the top of the building. Find the time taken by the ball to reach
each floor. (Use the formula s = ut+(1/2)at^2 where u and a are the initial
velocity in m/sec (= 0) and acceleration in m/sec^2 (= 9.8 m/s^2)).
}
printf("\n\n\n\tTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d
INTERVALS OF TIME : %f",tim_intrval,distance);
getch();
}
Output:
NO OF TIME INTERVALS :2
AT T1 TIME(sec) :5
VELOCITY AT 5 sec (m/sec) :20
ACCLERATION AT 5 sec (m/sec^2):35
AT T1 TIME(sec) :10
VELOCITY AT 10 sec (m/sec) :30
ACCLERATION AT 10 sec (m/sec^2):35
TOTAL DISTANCE TRAVELLED BY VEHICLE IN 2 INTERVALS OF
TIME :2587.5
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 9
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
3 b)Write a C program, which takes two integer operands and one operator
from the user, performs the operation and then prints the result. (Consider
the operators +,-,*, /, % and use Switch Statement)
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 10
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
break;
case '/':
printf("Division\n");
c=a/b;
printf("Quotient=%d\n",c);
break;
case '%': printf("Remainder\
n"); c=a%b;
printf("Remainder=%d\n",c);
break;
default:
printf("Invalid Option\n");
break;
} /*end of switch statement*/
}
while(1); /*End of while*/
INPUT:
Enter a and b:20 10
MENU
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
E Exit
Enter your choice : +
OUTPUT:
Sum= 30
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 11
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
if (count==2)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
INPUT:
Enter a positive integer:
11
OUTPUT:
11 s a prime number
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 12
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
OUTPUT:
Given number =121
Sum of the digits=4
Given number is palindrome =121
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 13
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
OUTPUT:
FIBONACCISEQUENCEFOR THE FIRST 10TERMS:
0 1 1 2 3 5 8 13 21 34
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 14
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
INPUT:
OUTPUT:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 15
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 16
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
printf(“Root2=%f-i%f\n”,real,img);
}
}
getch();
}
INPUT:
ENTER VALUESFOR a b c
1 4 4
OUTPUT:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 17
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source Code
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i,x,k;
float sum=1;
clrscr();
printf(“Enter x value:”);
scanf(“%d”,&x);
printf(“Sum of series is:\t”);
for(i=1;i<=3;i++)
{
sum+=pow(-1,i)*pow(x,i)/2*i;
}
printf(“%f”,sum);
getch();
}
INPUT:
Enter x value: 1
OUTPUT:
Sum of series is: 0.583333
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 18
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
INPUT:
OUTPUT:
Sum of series is 15
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 19
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum =0,min,max,list[100];
float avg;
clrscr();
printf("Enter no of elements :");
scanf("%d",&n);
printf("\nEnter %d elements \n",n);
for(i=0;i<n;i++)
scanf("%d",&list[i]);
min=max=sum= list[0];
for(i=1;i<n;i++)
{
if(list[i]>max)
max=list[i];
if(list[i]<min)
min=list[i];
sum+=list[i];
}
avg=sum/(float)n;
printf(“The list of elements are:\n");
for(i=0;i<n;i++)
printf("%5d",list[i]);
printf("\n\n");
printf("Maximum Value = %d\n",max);
printf("Minimum Value = %d\n",min);
printf("Average = %f\n",avg);
getch();
}
INPUT:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 20
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
OUTPUT:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 21
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
#include <stdio.h>
#include<conio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, 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]);
}
for (i = 0; i < n; i++)
{
sum = sum + x[i];
}
average = sum / (float)n;
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
}
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 22
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
INPUT:
Enter the value of N
5
Enter 5 real numbers
34
88
32
12
10
OUTPUT:
Average of all elements = 35.20
variance of all elements = 794.56
Standard deviation = 28.19
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 23
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 24
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
{ C[i][j]=A[i][j]+B[i]
[j];
}
}
printf("The addition of two matrices A and B is\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",C[i][j]);
}
printf(“\n”);
}
getch();
}
INPUT:
Enter the no. of rows:3
Enter the no. of columns:3
Enter the elements of matrix A
1 2 3
4 5 6
7 8 9
Enter the elements of matrix B
1 1 1
1 1 1
1 1 1
OUTPUT:
The addition of two matrices A and B is
2 3 4
5 6 7
8 9 10
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 25
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
#include<stdio.h>
#include<conio.h>
void multiply(int a[10][10],int b[10][10],int,int,int,int);
void main()
{
int a[10][10],b[10][10],r1,c1,r2,c2,i,j;
clrscr();
printf("Enter no of rows and columns of matrix A\n");
scanf("%d%d",&r1,&c1);
printf("Enter no of rows and columns of matrix B \n");
scanf("%d%d",&r2,&c2);
if(c1==r2)
printf("Matrix multiplication is possible");
else
exit();
printf("Enter the elements of matrix A\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;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]);
}
}
multiply(a,b,r1,c1,r2,c2);
}
void multiply(int a[10][10], int b[10][10],int r1,int c1,int r2,int c2)
{
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 26
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
int i,j,k,c[10][10];
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{ c[i]
[j]=0;
for(k=0;k<r2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("The resultant matrix multiplication is \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%3d",c[i][j]);
}
printf("\n");
}
getch();
}
INPUT:
Enter number of rows & columns of Matrix A:
2 2
Enter number of rows & columns of MatrixB:
2 2
Enter the elements of matrix A: 2 2 2 2
Enter the elements of matrix B: 2 2 2 2
OUTPUT:
The resultant matrix multiplication is:
88
88
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 27
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
int main()
{
int i, j, rows, cols, **a;
printf("Transpose of a Matrix");
printf("\nEnter number of rows :");
scanf("%d", &rows);
printf("\nEnter number of columns :");
scanf("%d", &cols);
// allocate rows, each row is a pointer to int
a = malloc(rows * sizeof (int *));
// for each row allocate cols int
for (i = 0; i < rows; i++)
{
a[i] = malloc(cols * sizeof (int));
}
printf("\nEnter Matrix\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("Enter Element %d %d : ", i, j);
scanf("%d", &a[i][j]);
}
}
matrixTranspose(a, rows, cols);
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 28
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Input:
Transpose of a Matrix
Enter number of rows: 2
Enter number of columns: 2
Enter Matrix
Enter Element 0 0 : 1
Enter Element 0 1 : 2
Enter Element 1 0 : 3
Enter Element 1 1 : 4
Transpose of given Matrix
1 3
1 4
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 29
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
INPUT:
Enter a number
5
OUTPUT:
The factorial value of 5 is 120
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 30
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
OUTPUT:
Factorial of 6 = 720
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 31
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
ii. To find the GCD (greatest common divisor) of two given integers using
non- recursive function.
AIM: To find GCD of two integers using non recursive function.
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,GCD;
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
GCD = gcd(n1,n2);
printf("The GCD of two numbers %d and %d is %d.", n1, n2, GCD);
getch();
}
int gcd(int a, int b)
{
int i, gcd;
for(i=1; i <= a && i <= b; ++i)
{
if(a%i==0 && b%i==0)
gcd = i;
}
return gcd;
}
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 32
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
AIM:To find the GCD (greatest common divisor) of two given integers using
recursive function
#include <stdio.h>
#include<conio.h>
int gcd(int n1, int n2);
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, gcd(n1,n2));
return 0;
}
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 33
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 34
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
INPUT:
Enter base number: 3
Enter power number(positive integer): 4
OUTPUT:
3^4=81
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 35
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
4.e)Write a program for reading elements using pointer into array and display
the values using array.
AIM: To read elements using pointer into array and display the values using
array.
Source Code:
#include <stdio.h>
#include<conio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int N, i;
int * ptr = arr;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Enter elements in array:\n");
for (i = 0; i < N; i++)
{
scanf("%d", &ptr[i]);
}
printf("Array elements: ");
for (i = 0; i < N; i++)
{
printf("%d, ", i[ptr]);
}
return 0;
}
Input:
Output:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 36
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
4.f)Write a program for display values reverse order from array using
pointer. AIM: To display values reverse order from array using pointer.
Source Code:
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main()
{
int size, i, arr[MAX];
int *ptr;
clrscr();
ptr = &arr[0];
printf("\nEnter the size of array : ");
scanf("%d", &size);
printf("\nEnter %d integers into array: ", size);
for (i = 0; i < size; i++)
{
scanf("%d", ptr);
ptr++;
}
ptr = &arr[size - 1];
printf("\nElements of array in reverse order are :");
for (i = size - 1; i >= 0; i--) {
printf("\nElement%d is %d : ", i, *ptr);
ptr--;
}
getch();
}
Input:
Enter the size of array : 5
Enter 5 integers into array: 11 22 33 44 55
Output:
Elements of array in reverse order are
: Element 4 is: 55
Element 3 is: 44
Element 2 is: 33
Element 1 is: 22
Element 0 is: 11
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 37
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int numArray[10];
int i, sum = 0;
int *ptr;
printf("\nEnter 10 elements : ");
for (i = 0; i < 10; i++)
scanf("%d", &numArray[i]);
ptr = numArray;
for (i = 0; i < 10; i++)
{
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements : %d", sum);
}
Input:
Enter 10 elements: 11 12 13 14 15 16 17 18 19 20
Output:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 38
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
5. STRINGS
5.a)Write a C program to convert a Roman numeral ranging from I to L to its
decimal equivalent.
Source Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char rom[30];
int a[30], l, i, k, dec;
clrscr();
printf("Enter the roman number\n");
scanf("%s", &rom);
l =strlen(rom);
for(i = 0; i < l; i++)
{
switch (rom[i])
{
case 'I': a[i] = 1;
break;
case 'V': a[i] = 5;
break;
case 'X': a[i] = 10;
break;
case 'L': a[i] = 50;
break;
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 39
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Input:
Enter the roman number:
V Output:
decimal equivalent is: 5
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 40
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source Code:
#include<stdio.h>
#include<conio.h>
void predigits(char c1,char c2);
void postdigits(char c,int n);
char roman_Number[1000];
int i=0;
int main(){
int j;
long int number;
while(number != 0){
if(number >=50){
if(number < (50 + 4 * 10))
{ postdigits('L',number/50);
number = number - (number/50) * 50;
}
else
{ predigits('X','C');
number = number - (100-10);
}
}
else if(number >=10){
if(number < (10 + 3 * 10))
{ postdigits('X',number/10);
number = number - (number/10) * 10;
}
else{
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 41
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
predigits('X','L');
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 42
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
}
}
else if(number >=1){
if(number < 4){
postdigits('I',number/1);
number = number - (number/1) * 1;
}
else
{ predigits('I','V');
number = number - (5-1);
}
}
}
return 0;
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 43
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 44
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Input:
Output:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 45
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 46
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Input:
Enter the Original String: computer
Enter the index where you want to insert the sub String: 3
Output:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 47
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void delchar(char *x,int a, int b);
void main()
{
char string[10];
int n,pos,p;
clrscr();
puts("Enter the string");
gets(string);
printf("Enter the position from where to delete");
scanf("%d",&pos);
printf("Enter the number of characters to be deleted");
scanf("%d",&n);
delchar(string, n,pos);
getch();
}
Input:
Enter the string: jayapal
Enter the position from where to delete:4
Enter the number of characters to be deleted 2
Output:
Jayal
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 48
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20],str3[20],k;
clrscr();
printf("enter the string\n");
scanf("%s",str1);
strcpy(str3,str1);
strcpy(str2,strrev(str1));
k=strcmp(str3,str2);
if(k==0)
{
printf("the given string is palindrome");
}
else
{
printf("the given string is not a palindrome");
}
getch();
}
Input:
Enter the string: Madam
Output:
the given string is palindrome
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 49
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source Code:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30], t[20];
char *found;
clrscr();
puts("Enter the first string: ");
gets(s);
puts("Enter the string to be searched: ");
gets(t);
found = strstr(s, t);
if(found)
{
printf("Second String is found in the First String at %d position.\n",
found - s);
}
else
{
printf("-1");
}
getch();
}
Input:
1. Enter the first
string: kali
Enter the string to be searched:
L
Output:
second string is found in the first string at 2 position
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 50
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
5.f)Write a C program to count the lines, words and characters in a given text.
Source code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char ch;
unsigned int long linecount, wordcount, charcount;
int u;
linecount=0;
wordcount=0;
charcount=0;
while((ch=getc(stdin))!=EOF){
if (ch !='\n') {++charcount;}
if (ch==' ' || ch=='\n') {++wordcount;}
if (ch=='\n') {++linecount;}
}
if(charcount>0){
++wordcount;
++linecount;
}
printf( "%lu %lu %lu\n", charcount, wordcount, linecount );
return 0;
Input :
Enter any string:abc def ghi jkl mno pqr stu vwx yz
Output:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 51
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
#include <stdio.h>
#include <conio.h>
struct complex
{
float real, imag;
}a, b, c;
struct complex read(void);
void write(struct complex);
struct complex add(struct complex, struct complex);
struct complex mul(struct complex, struct complex);
void main ()
{
clrscr();
printf("Enter the 1st complex number\n ");
a = read();
write(a);
printf("Enter the 2nd complex number\n");
b = read();
write(b);
printf("Addition\n ");
c = add(a, b);
write(c);
printf("Multiplication\n");
c = mul(a, b);
write(c);
getch();
}
struct complex read(void)
{
struct complex t;
printf("Enter the real part\n");
scanf("%f", &t.real);
printf("Enter the imaginary part\n");
scanf("%f", &t.imag);
return t;
}
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 52
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Input:
Enter the real part 2
Enter the imaginary part 4
Output:
Complex number is
4.0 + i2.0
Addition
Complex number is
6.0 + i6.0
Multiplication
Complex number is
0.0 + i20.0
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 53
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source Code:
#include <stdio.h>
#include <conio.h>
struct student
{
char name[50];
int roll;
float marks;
} s[5];
int main()
{
int i;
printf("Enter information of students:\n");
for(i=0; i<5; ++i)
{
s[i].roll = i+1;
printf("\nFor roll number%d,\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks); printf("\
n");
}
printf("Displaying Information:\n\n");
for(i=0; i<5; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
return 0;
}
Input:
Enter information of students:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 54
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Output:
Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
Roll number: 2
Name: Jerry
Marks: 89
Roll number: 3
Name: Robin
Marks: 99
Roll number: 4
Name: Jack
Marks: 87
Roll number: 5
Name: Adam
Marks: 88
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 55
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter integer: ");
scanf("%d",&personPtr->age);
printf("Enter number: ");
scanf("%d",&personPtr->weight);
printf("Displaying: ");
printf("%d\t%d",personPtr->age,personPtr->weight);
getch();
return 0;
}
Input:
Enter integer: 20
Enter number: 64
Displaying: 20 64
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 56
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Output:
ID : 10
Name : Pranay
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 57
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
7. FILES
7.a)Write a C program to display the contents of a file to standard output
device.
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("mrecw.txt","r");
if(fp==NULL)
{
printf("file does not exist");
exit(0);
}
else
printf("file exist");
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
fclose(fp);
getch();
}
Input:
Mrecw.txt :welcome to mrecw
Output:
welcome to mrecw
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 58
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
AIM: To copy one file to another, replacing all lowercase characters with their
uppercase equivalents.
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2;
char ch;
clrscr();
fp1=fopen("check.txt","r");
fp2=fopen("check1.txt","w");
if(fp1==NULL)
{
printf("files not exist");
exit(0);
}
else
printf("file exist");
while((ch=fgetc(fp1))!=EOF)
{
ch=toupper(ch);
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
printf("the resulted contents are\n");
fp2=fopen("check1.txt","r");
while((ch=fgetc(fp2))!=EOF)
{
printf("%c",ch);
}
fclose(fp2);
getch();
}
Input:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 59
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Output:
HELLO WORLD
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 60
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source Code:
#include<stdio.h>
void main(int argc,char *argv[])
{
FILE *f1;
char ch;
int count=0;
if(argc!=3)
{
printf("error occured");
exit();
}
f1=fopen(argv[1],"r");
while((ch=getc(f1))!=EOF)
{
if(ch==*argv[2])
count++;
}
fclose(f1);
printf("character %c occurred %d times",*argv[2],count);
}
Input:
prg.exe a.txt O
Output:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 61
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
7.d)Write a C program that does the following: It should first create a binary
file and store 10 integers, where the file name and 10 values are given in the
command line. (hint: convert the strings using atoi function) Now the program
asks for an index and a value from the user and the value at that index should
be changed to the new value in the file. (hint: use fseek function) The program
should then read all 10 values and print them back.
Source code:
#include<stdio.h>
/* Our structure */
struct rec
int x,y,z;
};
int counter;
FILE *ptr_myfile;
struct rec my_record;
ptr_myfile=fopen("test.bin","rb");
if (!ptr_myfile)
{
printf("Unable to open file!");
return 1;
}
return 0;
}
Input:
File.exe 10 20 34 45 67 44 33 78 90 56
Output:
My record 10 20 34 45 67 44 33 78 90 56
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 63
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
7.e)Write a C program to merge two files into a third file (i.e., the contents
of the firs t file followed by those of the second are put in the third file).
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2,*fp3;
char ch;
clrscr();
fp1=fopen("DATA1.txt","r");
fp2=fopen("DATA2.txt","r");
if(fp1==NULL||fp2==NULL)
{
printf("files not exist");
exit(0);
}
else
printf("file exist");
fp3=fopen("result.txt","w");
while((ch=fgetc(fp1))!=EOF)
{
fputc(ch,fp3);
}
while((ch=fgetc(fp2))!=EOF)
{
fputc(ch,fp3);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
printf("two files merged into third file");
printf("the resultant contents are\n");
fp3=fopen("result.txt","r");
while((ch=fgetc(fp3))!=EOF)
{
printf("%c",ch);
}
fclose(fp3);
getch();
}
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 64
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Input:
Data1.txt: welcome
Data2.txt: to MRECW
Output:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 65
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
8. MISCELLANEOUS
8.a)Write a menu driven C program that allows a user to enter n numbers and
then choose between finding the smallest, largest, sum, or average. The menu
and all the choices are to be functions. Use a switch statement to determine
what action to take. Display an error message if an invalid choice is entered.
#include<stdio.h>
#include<conio.h>
int smallest(int a[10],int n);
int largest(int a[10],int n);
int sum(int a[10],int n);
int average(int a[10],int n);
void main()
{
int a[10],n,i,ch,small,large,total,avg;
clrscr();
printf(“enter the value of n”);
scanf(“%d”,&n);
printf(“enter n elements in an array”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
do
{
printf("\t *********************");
printf("\n\tMENU\n"); printf("\
t********************"); printf("\n\
t(1)TO FIND SMALLEST"); printf("\
n\t(2)TO FIND LARGEST "); printf("\
n\t(3)TO FIND SUM"); printf("\n\
t(4)TO FIND AVERAGE"); printf("\n\
t(0)EXIT"); printf("\n\
t********************"); printf("\n\n\
tEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
small=smallest(a,n);
printf(“the smallest element in a list is %d”,small);
break;
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 66
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
case 2:
large= largest(a,n);
printf(“the largest element in list is %d”,large);
case 3: break;
total=sum(a,n);
printf(“the sum of the elements in a list is %d”,total);
break;
case 4:
avg=average(a,n);
printf(“the average of element in a list is %d”,avg);
break;
case 0:
printf("\n Choice Terminated");
exit();
break;
default:
printf("\n Invalid Choice");
}}while(1);
getch();
}
int smallest( int a[10],int n)
{
int min,i;
min=a[0];
for(i=1;i<n;i++)
{
if(a[i]<min)
min=a[i];
}
return min;
}
int largest( int a[10],int n)
{
int max,i;
max=a[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
return max;
}
int sum( int a[10],int n)
{
int s=0,i;
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 67
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
for(i=0;i<n;i++)
{
s=s+a[i];
}
return s;
}
int average( int a[10],int n)
{
int s=0,i; for(i=0;i<n;i+
+)
{
s=s+a[i];
}
return (s/n);
}
INPUT:
ENTER UR CHOICE 1
The smallest element is 1
ENTER UR CHOICE 2
The largest element is 56
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 68
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
#include <stdio.h>
#include<conio.h>
void main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
getch();
}
Input:
Enter no of rows:4
Output:
1
12
123
1234
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 69
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
(ii) *
**
***
Source Code:
#include <stdio.h>
#include<conio.h>
void main()
{
int n, c, k;
printf("Enter number of rows:\n");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k+
+ ) printf("*");
printf("\n");
}
getch();
}
Input:
Output:
*
**
***
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 70
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
(iii) 1
23
456
Source Code:
#include <stdio.h>
#include<conio.h>
void main()
{
int rows, i, j, number= 1;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i <= rows; i++)
{
for(j=1; j <= i; ++j)
{
printf("%d ", number);
++number;
}
printf("\n");
}
getch();
}
Input: Enter number of rows:3
Output:
1
23
456
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 71
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
(iv) 1
22
333
4444
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
int num;
printf("Enter the number of Digits :");
scanf("%d", &num);
for (i = 0; i <= num; i++)
{
for (j = 0; j < i; j++)
{
printf("%d ", i);
}
printf("\n");
}
getch();
}
Output:
1
22
333
4444
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 72
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
(v) *
**
***
****
****
***
**
*
Source code:
#include <stdio.h>
#include<conio.h>
void main()
{
int i, j, rows;
clrscr();
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
for(i=rows; i>=1; --i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
getch();
}
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 73
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Input:
Enter no of rows: 4
Output:
*
**
***
****
****
***
**
*
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 74
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
8.C) Writea C Program implement Student Data Base System Using Files &
Structures.
#include<conio.h>
#include<stdio.h>
void menu();
void header();
void newfile();
void add();
void search();
void display();
void deleted();
void update();
int ch,f=0;
char tid[10];
struct student
{
char id[10],name[10],rollno[10],branch[10],sec[10],addr[10],dob[10];
}rec;
struct student rec1={0};
void main()
{
menu();
}
void menu()
{
header();
gotoxy(23,9);
cprintf("\n1.CREATE A NEW FILE");
gotoxy(23,10);
cprintf("\n2.ADD A NEW RECORD");
gotoxy(23,11);
cprintf("\n3.SEARCH A RECORD");
gotoxy(23,12);
cprintf("\n4.DISPLAY A RECORD");
gotoxy(23,13);
cprintf("\n5.DELETE A RECORD");
gotoxy(23,14);
cprintf("\n6.UPDATE A RECORD");
gotoxy(23,15);
cprintf("\n7.EXIT");
gotoxy(23,16);
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 75
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
cprintf("\nENTER UR CHOICE");
scanf("%d",&ch);
switch(ch)
{
case 1:newfile();
break;
case 2:add();
break;
case 3:search();
break;
case 4:display();
break;
case 5:deleted();
break;
case 6:update();
break;
case 7:exit(0);
}
getch();
}
void newfile()
{
FILE *fp;
fp=fopen("student.txt","w");
printf("\n ONE NEW FILE CREATED SUCCESFULLY");
getch();
fclose(fp);
getch();
menu();
}
void add()
{
FILE *fp;
fp=fopen("student.txt","a+");
if(fp==NULL)
{
printf("FILE IS NOT OPENED");
getch();
exit(0);
}
printf("\n ENTER STUDENT ID");
scanf("%s",&rec.id);
printf("\ENTER STUDENT NAME");
scanf("%s",&rec.name);
printf("\n ENTER STUDENT ROLL NO");
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 76
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
scanf("%s",&rec.rollno);
printf("\n ENTER BRANCH NAME");
scanf("%s",&rec.branch);
printf("\n ENTER SECTION");
scanf("%s",&rec.sec); printf("\
n ENTER ADDRESS");
scanf("%s",&rec.addr);
fwrite(&rec,sizeof(rec),1,fp);
printf("\nONE NEW RECORD CREATED SUCCESFULLY");
getch();
fclose(fp);
menu();
}
void search()
{
FILE *fp;
fp=fopen("student.txt","r+");
if(fp==NULL)
{
printf("FILE IS NOT OPENED");
getch();
exit(0);
}
printf("ENTER UR ID TO BE SEARCHD:");
scanf("%s",&tid);
while(fread(&rec,sizeof(rec),1,fp))
{
if(!strcmp(rec.id,tid))
{ f=1
;
printf("\n\t\t\tRECORD IS FOUND");
printf("\nID :%s",rec.id); printf("\
nNAME :%s",rec.name); printf("\
nROLL NO:%s",rec.rollno); printf("\
nBRANCH :%s",rec.branch); printf("\
nSECTION:%s",rec.sec); printf("\
nADDRESS:%s",rec.addr); printf("\
nDOB:%s",rec.dob); printf("\n\n");
getch();
}
}
if(f==0)
printf("\n\n\t\t\tRECORD is not FOUND");
getch();
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 77
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
fclose(fp);
menu();
}
void display()
{
FILE *fp;
fp=fopen("student.txt","r+");
if(fp==NULL)
{
printf("FILE IS NOT OPENED");
getch();
exit(0);
}
while(fread(&rec,sizeof(rec),1,fp))
{
if(strcmp(rec.id,'\0'))
{ f=1
;
printf("\nID :%s",rec.id); printf("\
nNAME :%s",rec.name); printf("\
nROLL NO :%s",rec.rollno); printf("\
nBRANCH :%s",rec.branch); printf("\
nSECTION :%s",rec.sec); printf("\
nADDRESS :%s",rec.addr); printf("\
nDOB :%s",rec.dob); printf("\n\n");
getch();
}
}
if(f==0)
printf("\n\n\t\t\tNO RECORDS");
getch();
fclose(fp);
menu();
}
void deleted()
{
FILE *fp;
fp=fopen("student.txt","r+");
if(fp==NULL)
{
printf("FILE IS NOT OPENED");
getch();
exit(0);
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 78
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
}
printf("ENTER UR ID TO BE DELETED:");
scanf("%s",&tid);
while(fread(&rec,sizeof(rec),1,fp))
{
if(!strcmp(rec.id,tid))
{ f=1
;
printf("\n\t\t\tONE RECORD IS DELETED SUCCESSFULLY");
printf("\nID :%s",rec.id); printf("\
nNAME :%s",rec.name); printf("\
nROLL NO:%s",rec.rollno); printf("\
nBRANCH :%s",rec.branch);
printf("\nSECTION:%s",rec.sec);
printf("\nADDRESS:%s",rec.addr);
printf("\nDOB:%s",rec.dob); printf("\
n\n");
getch();
fseek(fp,ftell(fp)-sizeof(rec),0);
fwrite(&rec1,sizeof(rec),1,fp);
}
}
if(f==0)
printf("\n\n\t\t\tRECORD is not FOUND");
getch();
fclose(fp);
menu();
}
void update()
{
FILE *fp;
fp=fopen("student.txt","r+");
if(fp==NULL)
{
printf("FILE IS NOT OPENED");
getch();
exit(0);
}
printf("ENTER UR ID TO BE UPDATED:");
scanf("%s",&tid);
while(fread(&rec,sizeof(rec),1,fp))
{
if(!strcmp(rec.id,tid))
{ f=1
;
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 79
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
switch(ch)
{
case 1:printf("enter new name");
scanf("%s",rec.name);
break;
case 2:printf("enter new roll no");
scanf("%d",&rec.rollno);
break;
case 3:printf("enter new branch");
scanf("%s",rec.branch);
break;
case 4:printf("enter new section");
scanf("%s",rec.sec);
break;
case 5:printf("enter new
address");
scanf("%s",rec.addr);
break;
}
fseek(fp,ftell(fp)-sizeof(rec),0);
fwrite(&rec,sizeof(rec),1,fp);
printf("\n\t\t\tONE RECORD IS updated SUCCESSFULLY");
}
}
if(f==0)
printf("\n\n\t\t\tRECORD is not FOUND");
getch();
fclose(fp);
menu();
}
void header()
{
clrscr();
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 80
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
gotoxy(20,5);
textcolor(BLUE);
cprintf("*******************************");
gotoxy(10,6);
textcolor(YELLOW);
cprintf("@@@@@@@@@@@@@@@....STUDENT
MANAGEMENT....@@@@@@@@@@@@@@@");
gotoxy(20,7);
textcolor(BLUE);
cprintf("********************************");
}
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 81
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
AIM: To search for a key value in a given list of integers using linear search
method
Source Code:
#include<stdio.h>
#include<conio.h>
int linear(int a[],int,int);
void main()
{
int a[20],n,i,key,pos;
clrscr();
printf("enter the no. of elements u want in array\n");
scanf("%d",&n);
printf("enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the key which u wnt to search\n");
scanf("%d",&key);
pos=linear(a,n,key);
if(pos==-1)
printf("element is not found");
else
printf("element is found at %d position",pos+1);
getch();
}
int linear(int a[],int n,int key)
{
int i,k=0; for(i=0;i<n;i+
+)
{
if(key==a[i])
{
k=i;
}
}
if(k==0)
return(-1);
else
return(k);
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 82
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
}
INPUT:
enter 5 elements
56 87 4 34 21
OUTPUT:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 83
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
9.b)Write a C program that uses non recursive function to search for a Key
value in a given sorted list of integers using binary search method.
AIM: To search for a Key value in a given sorted list of integers using binary
search method.
Source Code:
#include<stdio.h>
#include<conio.h>
int binary(int a[],int,int);
void main()
{
int a[20],i,n,key,pos;
clrscr();
printf("enter the no. of elements u want in the list\n");
scanf("%d",&n);
printf("enter the %d elements of array\n",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("enter the key u want to search\n");
scanf("%d",&key);
pos=binary(a,n,key);
if(pos==-1)
printf("key is not found");
else
printf("key is found at %d position",pos+1);
getch();
}
int binary(int a[],int n,int key)
{
int low=0,high=n-1,mid;
while(high>=low)
{
mid=(low+high)/2;
if(key>a[mid])
low=mid+1;
else if(key<a[mid])
high=mid-1;
else
return(mid);
}
return(-1);
}
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 84
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
INPUT:
enter the no. of elements u want in the list 5
14 45 57 85 96
OUTPUT:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 85
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
9.c)Write a C program that implements the Bubble sort method to sort a given
list of integers in ascending order.
AIM:To implement the Bubble sort method to sort a given list of integers in
ascending order.
Source Code:
#include<stdio.h>
#include<conio.h>
void bubble(int a[20],int);
void main()
{
int a[20],n,i;
clrscr();
printf("enter the no. of elements to sort\n");
scanf("%d",&n);
printf("enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("before sorting,the elements are\n");
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
bubble(a,n);
getch();
}
void bubble(int a[20],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 86
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
}
}
printf("after sorting,the elements are\n");
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
}
Input:
Output:
before sorting,the elements are 1 5 3 2 4
after sorting,the elements are 1 2 3 4 5
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 87
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
9.d)Write a C program that sorts the given array of integers using selection
sort in descending order.
AIM: To sort the given array of integers using selection sort in descending
order.
Source Code:
#include <stdio.h>
void selection_sort();
int a[30], n;
void main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
selection_sort(); printf("\n\
nAfter sorting:\n"); for(i=0;
i<n; i++)
printf("\n%d", a[i]);
getch();
}
void selection_sort()
{
int i, j, min, temp;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 88
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Input:
Enter size of an array: 6
Output:
After sorting:
1
2
3
4
5
6
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 89
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
9.e)Write a C program that sorts the given array of integers using insertion
sort in ascending order
AIM: To sort the given array of integers using insertion sort in ascending
order.
Source Code:
#include<stdio.h>
#include<conio.h>
void insertion(int [], int );
int main()
{
int arr[30];
int i,size;
printf("Enter total no. of elements : ");
scanf("%d",&size);
printf(“\n Enter the elements to sort:”);
for(i=0; i<size; i++)
scanf("%d",&arr[i]);
insertion(arr,size);
printf("\nAfter sorting\n");
for(i=0; i<size; i++)
printf(" %d",arr[i]);
getch();
return 0;
}
void insertion(int arr[], int size)
{
int i,j,tmp;
for(i=0; i<size; i++)
{
for(j=i-1; j>=0; j--)
{
if(arr[j]>arr[j+1])
{
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
else
break;
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 90
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
}
}
}
Input:
Enter total no. of elements: 6
Enter the elements to sort:6 3 1 4 2 5
Output:
After sorting 1 2 3 4 5 6
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 91
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
Source Code:
#include <stdio.h>
#include <string.h>
int main()
{
int i, j, num;
char name[20][10], t_name[15][10], temp[20];
printf("Enter how many number of names to be sorted in alphabetical
order\n");
scanf("%d", &num);
printf("Please enter %d names one by one\n", num);
for(i=0; i< num ; i++)
{
scanf("%s",name[i]);
strcpy (t_name[i], name[i]);
}
for(i=0; i < num-1 ; i++)
{
for(j=i+1; j< num; j++)
{
if(strcmp(name[i],name[j]) > 0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("Names before sorting in alphabetical order\n");
for(i=0; i< num ; i++)
{
printf("%s\n",t_name[i]);
}
printf("Names after sorting in alphabetical order\n");
for(i=0; i< num ; i++)
{
printf("%s\n",name[i]);
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 92
PROGRAMMING FOR PROBLEM SOLVING Department of H &S
}
}
Input:
Output:
Malla Reddy Engineering College for Women (Autonomous Institution-UGC, Govt. of India) Page 93