C Programming LAB Syllabus Programs 1 42 (1)
C Programming LAB Syllabus Programs 1 42 (1)
C PROGRAMMING LAB
(Common to all branches)
Prerequisites: [Note: The programs may be executed using any available Open Source / Freely available
IDE Some of the Tools available are:
DevCpp: http://www.bloodshed.net/devcpp.html
Eclipse: http://www.eclipse.org
1. To work with an IDE to create, edit, compile, run and debug programs
2. To develop programs to solve basic problems by understanding basic concepts in C like operators,
control statements etc.
3. To develop modular, reusable and readable C Programs using the concepts like functions, arrays etc.
Course Outcomes: After completion of this course, the students will be able to
CO1: Formulate the algorithms for simple problems and identify correct logical errors encountered during
execution.
CO2: Modularize the code with functions so that they can be reused.
CO5: Create, read and write to and from simple text and binary files.
R21 B. Tech Syllabus KGRCET Hyderabad
Practice Sessions:
1. 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.
2. Write a simple program that converts one given data type to another using auto conversion and casting.
Take the values from standard input.
1. Write a program for find the max and min from the three numbers.
3. 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.
4. Write a program that prints a multiplication table for a given number and the number of 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
Expression Evaluation:
1. 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)).
2. 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)
4. Write a C program to find the sum of individual digits of a positive integer and test given number is
palindrome.
The first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the
preceding two terms in the sequence. Write a C program to generate the first n terms of the sequence.
6. Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by
the user.
8. Write a C program to calculate the following, where x is a fractional value. 1-x/2 +x^2/4-x^3/6
9. Write a C program to read in two numbers, x and n, and then compute the sum of this geometric
progression: 1+x+x^2+x^3+…………. +x^n. For example: if n is 3 and x is 5, then the program
R21 B. Tech Syllabus KGRCET Hyderabad
computes 1+5+25+125.
iii. Transpose of a matrix with memory dynamically allocated for the new matrix as row and
column counts may not be same.
2. Write a C program to find the minimum, maximum and average in an array of integers.
3. Write a function to compute mean, variance, Standard Deviation, sorting of n elements in single
dimension array.
ii. To find the GCD (greatest common divisor) of two given integers.
5. Write a program for reading elements using pointer into array and display the values using array.
6. Write a program for display values reverse order from array using pointer.
Strings:
1. Write a C program to convert a Roman numeral ranging from I to L to its decimal equivalent.
4. Write a C program to determine if the given string is a palindrome or not (Spelled same in both
directions with or without a meaning like madam, civic, noon, abcba, etc.)
5. Write a C program that displays the position of a character ch in the string S or – 1 if S doesn ‘t contains
ch.
6. Write a C program to count the lines, words and characters in a given text.
Files:
2. Write a C program which copies one file to another, replacing all lowercase characters with their
uppercase equivalents.
3. 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 command line arguments.
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.
5. Write a C program to merge two files into a third file (i.e., the contents of the first file followed by
those of the second are put in the third file).
Structures:
Miscellaneous:
1. 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.
1 * 1 1 *
1 2 * * 2 3 2 2 * *
1 2 3 * * * 4 5 6 3 3 3 * * *
4 4 4 4 * *
*
Suggested Reference Books for solving the problems:
1. Byron Gottfried, Schaum’s, “Outline of Programming with C”, McGraw-Hill
2. B.A. Forouzan and R.F. Gilberg, “C Programming and Data Structures”, Cengage Learning, (3rd
Edition, 2018)
3. Brian W. Kernighan and Dennis M. Ritchie, “The C Programming Language”, Prentice Hall of India.
4. R.G. Dromey, “How to solve it by Computer”, Pearson, (16th Impression)
5. Programming in C, Stephen G. Kochan, Pearson Education, (4th Edition)
6. Herbert Schildt, “C: The Complete Reference”, Mc Graw Hill, 4th Edition
1. 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.
Aim: Program that prints the results of all the operators available in C. Program:
#include<stdio.h>void
main( )
{
int a,b;
printf(“enter the values of a and b”);
scanf(“%d%d”,&a,&b);
printf(“the arithmetic operators result is %d %d %d %d”, a+b,a-b,a*b,a/b);
printf(“the relational operators result is %d %d %d %d”, a>b,a<b,a>=b,a<=b);
printf(“the logical operators result is %d %d %d”, a&&b,a||b,!(a==b)); printf(“the
increment operator result is %d %d %d %d”,a++,++a,b++,++b); printf(“the
decrement operator result is %d %d %d %d”,a--,--a,b--,--b); printf(“the bitwise
AND operator result is %d”,a&b);
printf(“the bitwise OR operator result is %d”,a|b); printf(“the
bitwise NOT operator result is %d”,a^b);
}
Output:
2. Write a simple program that converts one given data type to another using auto
conversion. Take the values form standard input.
Aim: program that converts one given data type to another using auto conversion.
Program:
#include<stdio.h>
#include<conio.h> void
main()
{
float sum, count;
Aim: program for find the max and min from the three numbers. Program:
#include<stdio.h>
#include<conio.h> void
main( )
{
int a,b,c;
clrscr();
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);
printf("\n");
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);
getch();
}
Output:
4. Write the program for the simple, compound interest.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h> void
main()
{
int p,t;
float r,si, amount, ci;
clrscr( );
printf("Please enter principle,time and rate of interest");
scanf("%d%d%f",&p,&t,&r);
si=p*t*r/100;
printf("\nSimple interest = %.3f",si);
amount=p*pow((1 +r/100),t); ci=amount-
p;
printf("\nCompound interest = %.3f",ci);
getch( );
}
Output:
5. 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.
Aim: program that declares class awarded for a given percentage of marks. Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int phy, chem, bio, math, comp; float
per;
clrscr();
printf("Enter five subjects marks: ");
scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);per =
(phy + chem + bio + math + comp) / 5.0; printf("Percentage =
%.2f\n", per);
if(per >= 70)
{
printf("Distinction");
}
else if(per <70 && per>=60)
{
printf("First Class");
}
else if(per <60 && per>=40)
{
printf("Second Class");
}
else
{
printf("Failed");
}
}
Output:
6. Write a C program to find the roots of a Quadratic equation.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,root1,root2;
clrscr();
printf("\n Enter values of a,b,c for finding roots of a quadratic eq:\n");
scanf("%f%f%f",&a,&b,&c);
/*checking condition*/
if(b*b>4*a*c)
{
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("\nRoots are real\n");
printf("\n root1=%f\n root2=%f",root1,root2);
}
else if(b*b==4*a*c)
{
printf("The roots are real and equal\n"); printf("\n
root1=%f\n root2=%f",root1,root2);
}
else
printf("\n Imaginary Roots.");
}
Output:
7. Write a program that prints a multiplication table for a given number and the number of
rows in the table. For example, for a number 5 and rows = 3, the output should be: 5 x 1 =
5
5 x 2 = 10
5 x 3 = 15
Aim: program that prints a multiplication table for a given number and the number of rows
in the table.
Program:
#include <stdio.h>
#include<conio.h> void
main()
{
int n, i, range;
clrscr();
printf("Enter an integer: ");
scanf("%d",&n); printf("Enter
the range: "); scanf("%d",
&range); for(i=1; i<=range; i++)
{
printf("%d * %d = %d \n",n,i, n*i);
}
}
Output:
8. Write a program that shows the binary equivalent of a given positive number between 0 to
255.
Aim: a program that shows the binary equivalent of a given positive number between 0 to
255.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h> void
main()
{
int num, rem=0, inum, p=0; long
int bin=0;
clrscr();
printf("Enter the number between 0 and 255");
scanf("%d",&num);
clrscr(); inum=num;
while(inum>0)
{
rem=inum%2;
bin+=rem*pow(10,p);
inum/=2;
p++;
}
printf("%d binary equivalent number is %ld",num,bin);
}
Output:
9. Write a C program to construct a pyramid of numbers as follows:
Program:
#include <stdio.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");
}
}
Output:
Enter number of rows3
1
12
123
Aim: Program to construct a pyramid of numbers.
*
**
*** Program:
#include <stdio.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("* ");
}
printf("\n");
}
}
Output:
Enter number of rows3
*
**
***
Aim: Program to construct a pyramid of numbers. 1
23
456
Program:
#include <stdio.h>void
main()
{
int i, j, rows,num=1; printf("Enter
number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j,num++)
{
printf("%d ",num);
}
printf("\n");
}
}
Output:
Enter number of rows3
1
23
456
Aim: Program to construct a pyramid of numbers. 1
22
333
4444
Program:
#include <stdio.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 ",i);
}
printf("\n");
}
}
Output:
Enter number of rows4
1
22
333
4444
Aim: Program to construct a pyramid of numbers.
*
**
***
****
***
**
*
Program:
#include <stdio.h> int
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("* ");
}
printf("\n");
}
for(i=rows-1; i>=1; --i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
OUTPUT:
Enter number of rows: 4
*
**
***
****
***
**
*
10. 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)).
Aim: program to print time taken by the ball to reach each floor.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h> void
main()
{
double a=9.8, t=0.0, tt=0.0; int
i,s=3;
clrscr(); for(i=10;i>=1;i--
,s+=3)
{
t = sqrt((2*s)/a);
printf("The time taken from %d to %d is %lf\n",i,i-1,t);
}
}
Output:
11. 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)
Aim: program which takes two integer operands and one operator from the user, performs the
operation and then prints the result.
Program:
#include<stdio.h>
#include<conio.h> void
main()
{
int a,b; char
ch;clrscr();
printf(" + for addition\n"); printf(" -
for subtraction\n"); printf(" * for
multiplication\n");printf(" / for
division\n"); printf(" '\%' for
modulus\n"); printf("Enter your
choice"); scanf("%c",&ch);
printf("Enter two integers");
scanf("%d%d",&a,&b);
switch(ch)
{
case '+':
printf("The addition of %d and %d is %d",a,b,a+b); break;
case '-':
printf("The subtraction of %d and %d is %d",a,b,a-b);
break;
case '*':
printf("The multiplication of %d and %d is %d",a,b,a*b);break;
case '/':
printf("The division of %d and %d is %d",a,b,a/b);break;
case '%':
printf("The modulus of %d and %d is %d",a,b,a%b);
break;
default:
printf("Entered wrong operator");
}
}
Output:
12. Write a program that finds if a given number is a prime number Aim:
#include<stdio.h>void
main()
{
int num,i=2,count=0,rem=0;
clrscr();
printf("Enter a number");
scanf("%d",&num);
while(i<=num/2)
{
rem = num%i;
if(rem==0) count++;
i++;
}
if(count>=1)
printf("The %d is not prime",num);
else
printf("the %d is prime",num);
}
Output:
13. Write a C program to find the sum of individual digits of a positive integer and test given
number is palindrome.
Aim: Program to display the sum of individual digits of a positive integer and
palindrome number.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num,rem,tnum,pal=0,i=2,sum=0;
clrscr();
printf("enter number");
scanf("%d",&num);
tnum=num;
while(tnum>0)
{
rem = tnum%10;
sum+=rem;
pal+=rem*pow(10,i); i--;
tnum/=10;
}
printf("The sum of individual digits of %d is %d \n",num,sum);
if(pal == num)
printf("The %d is palindrome number",pal);
else
printf("The %d is not a palindrome number",pal);
}
Output:
14. Write a C program to generate the first n terms of the sequence.
Aim: Program to display the first n terms of the Fibonacci sequence. Program:
#include <stdio.h>
void main()
{
//Loop will run for 2 Time less in series as first 2 digits printed in advance
for(counter = 1; counter <= n-2; counter++)
{
fib=num1 + num2;
printf(" %d",fib);
num1=num2;
num2=fib;
}
}
Output:
15. Write a C program to generate all the prime numbers between 1 and n, where n is avalue
supplied by the user.
Aim: program to generate all the prime numbers between 1and n. Program:
#include <stdio.h>
void main()
{
int no,counter,counter1,check;
clrscr();
printf("< PRIME NO. SERIES >");
printf("\n\n\n\t\t\tINPUT THE VALUE OF N: ");
scanf("%d",&no);
printf("\n\nTHE PRIME NO. SERIES B/W 1 TO %d : \n\n",no);
if(check == 0)
printf("%d\t",counter);
}
Output:
16. Write a C program to calculate the following, where x is a fractional value. 1-x/2
+x^2/4-x^3/6
Aim: program to calculate the following, where x is a fractional value. 1-x/2 +x^2/4-
x^3/6
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h> void
main()
{
float x,sum=1.0; int n;
clrscr();
printf("Enter x value");
scanf("%f",&x);
for(n=1;n<=3;n++)
{
sum+=((pow(-1,n))*((pow(x,n)/(2*n))));
}
printf("The sum of expression is %f",sum);
}
Output:
17. Write a C program to read in two numbers, x and n, and then compute the sum of this
geometric progression: 1+x+x^2+x^3+ ................ +x^n. For example: if n is 3 and x
is 5, then the program computes 1+5+25+125.
Aim: program to read in two numbers, x and n, and then compute the sum of this
geometric progression: 1+x+x^2+x^3+ ................ +x^n.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h> void
main()
{
int i,x,n,sum=0;
clrscr();
printf("Enter x,n values");
scanf("%d%d",&x,&n);
clrscr();
for(i=0;i<=n;i++)
{
sum+=pow(x,i);
}
printf("The sum of given equation is %d",sum);
}
Output:
18. Write a C program to convert a Roman numeral ranging from I to L to its decimal
equivalent.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int *a,len,i,j,k;char
*rom; clrscr();
printf("Enter the Roman Numeral:");
scanf("%s",rom);
len=strlen(rom);
for(i=0;i<len;i++)
{
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
Output:
19. Write a C program that converts a number ranging from 1 to 50 to Roman equivalent Aim:
while(num != 0)
{
if (num == 50) // 50 - l
{
printf("l");num -=
50;
}
else if (num >= 40) // 40 – xl
{
printf("xl");num
-= 40;
}
else if (num >= 10) // 10 – x
{
printf("x");num
-= 10;
}
else if (num >= 9) // 9 - ix
{
printf("ix");num
-= 9;
}
else if (num >= 5) // 5 - v
{
printf("v");num
-= 5;
}
else if (num >= 4) // 4 – iv
{
printf("iv");num
-= 4;
}
else if (num >= 1) // 1 – i
{
printf("i");num
-= 1;
}
}
return 0;
}
Output
20. Write a C program to find the minimum, maximum and average in an array of
integers.
Aim: program to find the minimum, maximum and average in an array ofintegers.
Program:
#include<stdio.h>
#include<conio.h> int
main()
{
int i,n,num[20],min=0,max=0;
float sum=0.0,avg=0.0;
clrscr();
for(i=0;i<n;i++)
{
printf("Enter a[%d] value",i);
scanf("%d",&num[i]);
}
min = max = num[0];
for(i=0;i<n;i++)
{
sum+=num[i];
if(num[i]<min)
min =num[i];
if(num[i]>max)
max = num[i];
}
avg = sum/i;
Output:
21. Write a C program to determine if the given string is a palindrome or not (Spelled same
in both directions with or without a meaning like madam, civic, noon, abcba, etc.)
Program:
void main()
{
char str[10],strrev[10]; int
i,j,len;
clrscr();
printf("Enter a string");
scanf("%s",str);
len=strlen(str);
for(i=len-1,j=0;i>=0;i--,j++)
{
strrev[j]=str[i];
//printf("%c",str[i]);
}
strrev[j]='\0';
if(strcmp(str,strrev)==0)
printf("%s is palindrome",str);
else
printf("%s is not palindrome",str);
}
Output:
22. Write a C program that displays the position of a character ch in the string S or – 1 ifS
doesn’t ‘t contains ch.
Aim: program that displays the position of a character ch in the string S or – 1 if S doesn’t ‘t
contains ch.
Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30];// t[20];
char *found,t;
clrscr();
Output:
23. Write a C program to count the lines, words and characters in a given text.
Aim: program to count the lines, words and characters in a given text.
Program:
#include <stdio.h>void
main()
{
char line[81], ctr;
int i,c, end = 0, characters = 0, words = 0, lines = 0;
printf("KEY IN THE TEXT.\n");
printf("GIVE ONE SPACE AFTER EACH WORD.\n");
printf("WHEN COMPLETED, PRESS 'RETURN'.\n\n");
while( end == 0)
{
/* Reading a line of text */
c = 0;
while((ctr=getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';
/* counting the words in a line */
if(line[0] == '\0')
break ;
else
{
words++;
for(i=0; line[i] != '\0';i++)
if(line[i] == ' ' || line[i] == '\t')
words++;
}
/* counting lines and characters */
lines = lines +1;
characters = characters + strlen(line);
}
printf ("\n");
printf("Number of lines = %d\n", lines);
printf("Number of words = %d\n", words);
printf("Number of characters = %d\n", characters);
}
Output:
24. Write a program through pointer variable to sum of n elements from array.
Aim: program through pointer variable to sum of n elements from array. Program:
#include<stdio.h>void
main()
{
int num[20],*pn,n,i,sum=0;
clrscr();
for(i=0;i<n;i++)
{
printf("Enter %d value",i+1);
scanf("%d",&num[i]);
}
pn = &num[0];
Output:
25. Write a program for reading elements using pointer into array and display the valuesusing
array.
Aim: program for reading elements using pointer into array and display the values usingarray.
Program:
#include<stdio.h>void
main()
{
int num[20],*pn,n,i;
clrscr();
pn = num;
for(i=0;i<n;i++)
{
printf("Enter %d value",i+1);
scanf("%d",(pn+i));
}
Output:
26. Write a program for display values reverse order from array using pointer.
Aim: Write a program for display values reverse order from array using pointer.
Program:
#include<stdio.h>void
main()
{
int num[20],*pn,n,i;
clrscr();
for(i=0;i<n;i++)
{
printf("Enter %d value",i+1);
scanf("%d",&num[i]);
}
pn = &num[0];
Output:
27. Write a C program to display the contents of a file to standard output device.
Program:
#include <stdio.h>
#include <conio.h>
#include <process.h>
if(argc!=2)
{
puts("Invalid number of arguments.");
exit(0);
}
fs = fopen(argv[1],"r");
if(fs==NULL)
{
puts("Source file cannot be opened.");
exit(0);
}
while(1)
{
ch=fgetc(fs);
if (ch==EOF)
break;
else
putch(ch);
}
fclose(fs);
getch();
}
Aim: C program copies contents of one file to another by replacing all lowercase
characters with their uppercase equivalents.
Program:
#include<stdio.h>void
main()
{
char ch;
FILE *fp1,*fp2;
clrscr();
fp1 = fopen("cbi","r");
if(fp1 == NULL)
{
printf("File is not opened properly");
exit(0);
}
fp2 = fopen("lmn","w");
if(fp2 == NULL)
{
printf("File is not opened properly");
exit(0);
}
while((ch=fgetc(fp1))!=EOF)
{
if(ch>=97 && ch<=122)
{
ch=ch-32;
fputc(ch,fp2);
}
else fputc(ch,fp2);
}
fcloseall();
}
Output:
Aim: C program to count the number of times a character occurs in a text file.
Program:
#include<stdio.h>
void main(int argc,char* argv[])
{
char ch;
int count=0;FILE
*fp1; clrscr();
if(argc!=3)
{
printf("Invalid number of arguments");
exit(0);
}
fp1 = fopen(argv[1],"r");
if(fp1 == NULL)
{
printf("File is not opened properly");
exit(0);
}
while((ch=fgetc(fp1))!=EOF)
{
if(ch==*argv[2])
{
count++;
}
}
printf("The %c is occured %d times in the file %s",*argv[2],count,argv[1]);
fclose(fp1);
}
Output:
C:\turboc3\source>31_file abc l
The l is occurred 2 times in the file abc
30. 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.
Aim: C program that create a binary file and store 10 integers, where the file name and 10
values are given in the command line. 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 inthe file. The program
read all 10 values and print them back.
Program:
#include<stdio.h> #include<stdlib.h>
void main(int argc,char* argv[])
{
FILE *fp1,*fp2;
int i,n,j=0,a[100],b[20],index=0,vindex=0;
if(argc!=12)
{
printf("Invalid number of arguments");
exit(0);
}
fp1 = fopen(argv[1],"wb");
if(fp1 == NULL)
{
printf("File is not opened properly");
exit(0);
}
i=2;
fseek(fp1,index,0);
fwrite(&vindex,sizeof(int),1,fp1);
fclose(fp1);
fp1 = fopen(argv[1],"r+b");
if(fp1 == NULL)
{
printf("File is not opened properly");
exit(0);
}
n = fread(&b,sizeof(int),10,fp1); i=0;
printf("data from file:\n");
while(i<n)
{
printf("%d\t",b[i]);i++;
}
fclose(fp1);
}
Output:
C:\turboc3\source>32_file dataint 1 2 3 4 5 6 7 8 9 10 Data
from user:
1 2 3 4 5 6 7 8 9 10
Enter the index and value for index 2 34 Data
from file:
1 34 3 4 5 6 7 8 9 10
31. Write a C program to merge two files into a third file (i.e., the contents of the first file
followed by those of the second are put in the third file).
Program:
#include <stdio.h>
#include <conio.h>
void main(int argc, char *argv[])
{
FILE *f1,*f2,*f3;
char ch;
clrscr();
if(argc!=4)
{
puts("Invalid number of arguments.");
exit(0);
}
f1 = fopen(argv[1],"r");
if(f1==NULL)
{
puts("Source file cannot be opened.");
exit(0);
}
f2 = fopen(argv[2],"r");
if (f2==NULL)
{
puts("Source file cannot be opened.");
exit(0);
}
f3 = fopen(argv[3],"w");
if (f3==NULL)
{
puts("Target file cannot be opened.");
exit(0);
}
while((ch=fgetc(f1))!=EOF)
{
fputc(ch,f3);
}
while((ch=fgetc(f2))!=EOF)
{
fputc(ch,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
getch();
}
Output:
C:\turboc3\source>33_file abc pqr lmn
C:\turboc3\source>type abc
Welcome to C
C:\turboc3\source>type pqr File
merge concept
C:\turboc3\source>type lmn
Welcome to C
File merge concept
32. Write a C program that uses functions to perform addition of two matrices.
Program:
#include <stdio.h>
void main()
{
int r, c, i, j, mat1[10][10], mat2[10][10], mat3[10][10];
clrscr();
mat_add(mat1,mat2,r,c);
}
Output:
33. Write a C program that uses function to perform the multiplication of two matrices
Program:
#include<stdio.h>
void main()
{
int i,j,r1,c1,r2,c2,a[10][10],b[10][10],*c;
clrscr();
printf("Input rows and columns of A matrix:");
scanf("%d%d",&r1,&c1);
printf("Input rows and columns of B matrix:");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("matrices can be multiplised\n");
printf("resultant matrix is %d*%d\n",r1,c2);
printf("Input A matrix\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
printf("Enter a[%d][%d] value",i,j);
scanf("%d",&a[i][j]);
}
printf("Input B matrix\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
printf("Enter b[%d][%d] value",i,j);
scanf("%d",&b[i][j]);
}
mat_mul(a,b,r1,c1,c2);
}
else
{
printf("Matrices cannot be multiplied.");
}
}
void mat_mul(int a[10][10],int b[10][10],int r1,int c1,int c2)
{
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<c1;++k)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
clrscr();
printf("\n =====Matrix Multiplication=====\n");
printf("Resultant of two matrices:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%5d",c[i][j]);
printf("\n");
}
}
Output:
34. Write a C program that uses functions to perform Transpose of a matrix with
memory dynamically allocated for the new matrix as row and column counts may
not be same.
Aim: Transpose of a matrix with memory dynamically allocated for the new matrix as row
and column counts may not be same.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],*b,i,j,r,c,tr,tc;
clrscr();
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter a[%d][%d] element",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Given Matrix is \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
tra_mat(a,r,c);
}
tr =c;
tc =r;
b =(int*)malloc(tr*tc*sizeof(int));
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
*(b+i*tc+j)=a[j][i];
}
}
Output:
35. Write a function to compute mean, variance, Standard Deviation, sorting of n
elements in single dimension array.
Aim: a function to compute mean, variance, Standard Deviation, sorting of n elements insingle
dimension array.
Program:
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
int i,j,n;
void main()
{
int x[MAXSIZE];
clrscr();
printf("Enter the value of N \n");
scanf("%d", &n);
/* Sorting elements */
for (i = 1; i <=n-1; i++)
{
for(j=i;j>0&&x[j-1]>x[j];j--)
{
temp=x[j];
x[j]=x[j-1];
x[j-1]=temp;
}
}
clrscr();
printf("The sorted array elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",x[i]);
sum = sum+x[i];
}
Output:
36. Write C programs that use both recursive and non-recursive functions to find the factorial of
a given integer.
Aim: programs that use both recursive and non-recursive functions to find the factorial ofa
given integer.
#include<stdio.h>
#include<conio.h>
int rec_factorial(int);
int non_rec_factorial(int);
void main()
{
int n,rfact,nrfact;
clrscr();
printf("Enter n value");
scanf("%d",&n);
rfact=rec_factorial(n);
printf("The factorial of %d is %d using recursive\n",n,rfact);
nrfact=non_rec_factorial(n);
printf("The factorial of %d is %d using non-recursive\n",n,nrfact);
}
int rec_factorial(int n)
{
if(n==0)
return 1;else
return n*rec_factorial(n-1);
}
int non_rec_factorial(int n)
{
int fact=1,i;
for(i=1;i<=n;i++)
fact*=i;
return fact;
}
Output:
37. Write C programs that use both recursive and non-recursive functions to find the GCD
(greatest common divisor) of two given integers.
Aim: programs that use both recursive and non-recursive functions to find the GCD
(greatest common divisor) of two given integers.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(void)
{
int a,b,iGcd;
clrscr();
getch();
}
/* Recursive Function*/
unsigned int GcdRecursive(unsigned m, unsigned n)
{
if(n>m)
return GcdRecursive(n,m);
if(n==0)
return m;else
return GcdRecursive(n,m%n);
}
/* Non-Recursive Function*/
unsigned int GcdNonRecursive(unsigned p,unsigned q)
{
unsigned remainder;
remainder = p-(p/q*q);
if(remainder==0)
return q;
else
GcdRecursive(q,remainder);
}
Output:
38. Write C programs that use both recursive and non-recursive functions to find x^n
Aim: C programs that use both recursive and non-recursive functions to find x^n
Program:
#include <stdio.h>
int rec_xpowern(int,int);
int non_rec_xpowern(int,int);void
void main()
{
int base, pow, rxpn,nrxpn;clrscr();
rxpn = rec_xpowern(base,pow);
printf("\nRecursive %d^%d = %d", base, pow,rxpn);
nrxpn = non_rec_xpowern(base,pow);
printf("\nNon Recursive %d^%d = %d", base, pow,nrxpn);
}
Output:
39. Write a C program that uses functions to insert a sub-string in to a given main string from a
given position.
Aim: A C program that uses functions to insert a sub-string in to a given main string from a
given position.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
char a[20],b[20],c[20];
int p=0;
void main()
{
clrscr();
ins_sub_string(a,b,p);
}
r = strlen(a);
n = strlen(b);
printf("%s", a);
}
Output:
40. Write a C program that uses functions to delete n Characters from a given position in a
given string.
Aim: A C program that uses functions to delete n characters from a given position in a
given string.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char string[10];
int n,pos,p;
clrscr();
Output:
41. Write a C program that uses functions to perform the following operations:
i. Reading a complex number
ii. Writing a complex number
iii. Addition of two complex numbers
iv. Multiplication of two complex numbers
Aim: C program that uses function to read, write, add and multiply two complex numbers.
Program:
#include <stdio.h>
#include <conio.h>
struct complex
{
float real, imag;
}c1,c2,c3;
void main ()
{
clrscr();
printf("Enter the 1st complex number\n ");
c1 = read();
clrscr();
printf("\n The 1st complex number is ");
write(c1);
printf("\n The 2nd complex number is ");
write(c2);
printf("\n The Addition of two complex numbers is ");
c3 = add(c1,c2);
write(c3);
getch();
}
Output:
42. 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.
Aim: a menu driven C program that allows a user to enter n numbers and then choose between
finding the smallest, largest, sum, or average.
Program:
#include<stdio.h>
#include<conio.h>
void menu();
int smallest(int a[],int n);
int largest(int a[],int n);
int sum(int a[],int n);
float average(int a[],int n);
void main()
{
int *a,n,i,ch,small,large,soe;
float avg;
clrscr();
for(i=0;i<n;i++)
{
clrscr();
menu();
case 2:
large = largest(a,n);
printf("Largest number is %d",large);
break;
case 3:
soe = sum(a,n);
printf("Sum of elements is %d",soe);
break;
case 4:
avg = average(a,n);
printf("Average of elements is %d",avg);
break;
default:
printf("Invalid Choice");
break;
}
void menu()
{
printf("1. smalest number\n");
printf("2. largest number\n");
printf("3. sum of all numbers\n");
printf("4. average of all numbers\n");
}
for(i=1;i<n;i++)
{
if(a[i]<small)
small = a[i];
}
return small;
}
for(i=1;i<n;i++)
{
if(a[i]>large)
large = a[i];
}
return large;
}
for(i=0;i<n;i++)
sum+=a[i];
return sum;
}
for(i=0;i<n;i++)
sum+=a[i];
return (sum/n);
}
Output: