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

c lab manual(r23)-1

Uploaded by

shaiknouman555
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

c lab manual(r23)-1

Uploaded by

shaiknouman555
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

WEEK1:

1. Writing simple programs using printf(), scanf()


Aim: Writing simple programs using printf(), scanf()
Program:
#include <stdio.h>
int main()
{
int a;
printf("Hello World");
printf("enter a number");
scanf("%d",&a);
printf("the entered number is:%d",a);
return 0;
}
Output:
Hello Worldenter a number23
the entered number is:23

WEEK2:

2. Sum and average of 3 numbers


Aim: Write a program to calculate sum and average of 3 numbers.
Program:
#include <stdio.h>
int main()
{
int a,b,c,sum;
float avg;
printf("enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
avg=sum/3.0;
printf("sum of 3 numbers=%d",sum);
printf("average of 3 numbers=%f",avg);
return 0;
}
Output:
enter 3 numbers4 6 7
sum of 3 numbers=17average of 3 numbers=5.666667

3. Conversion of Fahrenheit to Celsius and vice versa


Aim: Write a C program for Conversion of Fahrenheit to Celsius and
vice versa.
Program:
#include<stdio.h>
int main()
{
float f,c;
printf("Enter Fahrenheit:");
scanf("%f",&f);
c=(f-32)/1.8;
printf("Celsius: %f",c);
printf("Enter Celsius:");
scanf("%f",&c);
f=(c*1.8)+32;
printf("fahrenheit: %f",f);
return 0;
}
Output:
Enter Fahrenheit:102.3
Celsius: 39.055557
Enter Celsius:38.3
fahrenheit: 100.940002

4. Write a C program to perform simple interest calculation.

# include <stdio.h>
int main()
{
//Simple interset program
int P, R, T;
float SI;
printf("Enter the principal: ");
scanf("%d", &P);
printf("Enter the rate: ");
scanf("%d", &R);
printf("Enter the time: ");
scanf("%d", &T);
SI = (P * R * T) / 100;
printf("The Simple interest is %f", SI);
return 0;
}
Output:
Enter the principal: 12000
Enter the rate: 10
Enter the time: 3
The Simple interest is 3600.000000
WEEK3:
5. Finding the square root of a given
number
Aim: Write a C program to find the square root of a given number
Program:
#include <stdio.h>
#include <math.h>
int main(){
float num, root;

// Asking for Input


printf("Enter an integer: ");
scanf("%f", &num);

root = sqrt(num);
printf("The Square Root of %f is %f.", num, root);
return 0;
}
Output:
Enter an integer: 36
The Square Root of 36.000000 is 6.000000.
6. Finding compound interest
Aim: Write a C program to find compound interest
Program:
#include <stdio.h>
#include <math.h>

int main()
{
float p, r, t, ci;

printf("Enter the principle :");


scanf("%f", &p);
printf("Enter the rate :");
scanf("%f", &r);
printf("Enter the time :");
scanf("%f", &t);

ci = p * pow((1 + r / 100), t) - p;

printf("\nThe compound interest is %0.2f", ci);


return 0;
}
Output:
Enter the principle :12000
Enter the rate :12
Enter the time :3
The compound interest is 4859.14
7. Area of a triangle using heron’s
formulae
Aim: Write a C program to find Area of
a triangle using heron’s formulae
Program:
#include<stdio.h>
#include<math.h>

int main()
{
float a, b, c, s, area;
printf("\nEnter three sides of
triangle\n");
scanf("%f%f%f",&a,&b,&c);
s = (a+b+c)/2;

//Calculate area of triangle


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

printf("\n Area of triangle:


%f\n",area);

return 0;
}
Output:
Enter three sides of triangle
563
Area of triangle: 7.483315
8. Distance travelled by an object
Aim: Write a C program to find Distance travelled by an object.
Program:
#include<stdio.h>

int main() {
float u, a, d;
int t;

printf("\nEnter the value of a : ");


scanf("%f", & a);

printf("\nEnter the value of u : ");


scanf("%f", & u);

printf("\nEnter the value of t : ");


scanf("%d", & t);

d = (u * t) + (a * t * t) / 2;

printf("\n The Distance : %.2f", d);

return 0;
}

Output:
Enter the value of a : 3
Enter the value of u : 5
Enter the value of t : 2
The Distance : 16.00

WEEK4:
9. Evaluate the following expressions.
a. A+B*C+(D*E) + F*G
b. A/B*C-B+A*D/3
c. A+++B---A
d. J= (i++) + (++i)

a. Write a C program to evaluate A+B*C+(D*E) + F*G


Program:
#include<stdio.h>
int main()
{
int A,B,C,D,E,F,G,RES;
printf("enter values of A,B,C,D,E,F,G");
scanf("%d%d%d%d%d%d%d",&A,&B,&C,&D,&E,&F,&G);
RES=A+B*C+(D*E) + F*G;
printf("result=%d",RES);
return 0;
}
Output:
enter values of A,B,C,D,E,F,G2 3 4 2 5 6 6
result=60
b. Write a C program to evaluate A/B*C-B+A*D/3.
Program:
#include<stdio.h>
int main()
{
int A,B,C,D,RES;
printf("enter values of A,B,C,D");
scanf("%d%d%d",&A,&B,&C,&D);
RES=A/B*C-B+A*D/3;
printf("result=%d",RES);
return 0;
}
Output:
enter values of A,B,C,D2 4 3 5
result=-1
c. Write a C program to evaluate A+++B---A
Program:
#include<stdio.h>
int main()
{
int A,B,RES;
printf("enter values of A,B");
scanf("%d%d",&A,&B);
RES=A+++B---A;
printf("result=%d",RES);
return 0;
}
Output:
enter values of A,B3 4
result=3

d. Write a C program to evaluate J= (i++) + (++i)


Program:
#include<stdio.h>
int main()
{
int i,j;
printf("enter value i");
scanf("%d",&i);
j=(i++) + (++i);
printf("result=%d",j);
return 0;
}
Output:
enter value i 4
result=10
10. Find the maximum of three
numbers using conditional
operator
Aim: Write a C program to Find the
maximum of three numbers using
conditional operator.
Program:
# include <stdio.h>

void main()
{
int a, b, c, big ;

printf("Enter three numbers : ") ;

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

big = a > b ? (a > c ? a : c) : (b > c ? b :


c) ;

printf("\nThe biggest number is :


%d", big) ;
}
Output:
Enter three numbers : 3 5 8
The biggest number is : 8

11) Take marks of 5 subjects in integers, and find the total, average in float.

AIM: Take marks of 5 subjects in integers, and find the total, average in float
Program:
#include <stdio.h>
#include<conio.h>
int main ()
{
int eng, phy, chem, math, comp;
float total, average;
printf ("Enter marks of five subjects: \n");
scanf ("%d%d%d%d%d", &eng, &phy, &chem, &math, &comp);
total = eng + phy + chem + math + comp;
average = total / 5.0;
printf("Total marks = %.2f\n", total);
printf("Average marks = %.2f\n", average);
return 0;
}
Output:
Enter marks of five subjects:
89
92
86
84
95
Total marks = 446.00
Average marks = 89.20

WEEK5:

12) Write a C program to find the max and min of four numbers using if-else.
AIM: Write a C program to find the max and min of four numbers using if-else.
Program:
#include <stdio.h>
#include<conio.h>
main()
{
int a, b, c, d, y;
printf("Enter four integers (separate them with spaces): ");
scanf("%d %d %d %d", &a, &b, &c, &d);
if (a>b && a>c && a>d){
if (b<c && b<d){
y = b;
}
else if (c<b && c<d){
y = c;
}
else if (d<b && d<c){
y = d;
}
printf("Largest: %d\n", a);
printf("Smallest: %d", y);
}
else if (b>a && b>c && b>d) {
if (a<c && a<d){
y = a;
}
else if(c<a && c<d){
y = c;
}
else if(d<a && d<c){
y = d;
}
printf("Largest: %d\n", b);
printf("Smallest: %d", y);
}
else if (c>a && c>b && c>d)
{
if (a<b && a<d){
y = a;
}
else if(b<a && b<d){
y = b;
}
else if(d<a && d<b){
y = d;
}
printf("Largest: %d\n", c);
printf("Smallest: %d", y);
}
else if (d>a && d>b && d>c) {
if (a<b && a<c){
y = a;
}
else if(b<a && b<c){
y = b;
}
else if(c<a && c<b){
y = c;
}
printf("Largest: %d\n", d);
printf("Smallest: %d", y);
}
return 0;
}
Output:
Enter four integers (separate them with spaces):
21
8
4
52
Largest: 52
Smallest: 4

13) Write a C program to generate electricity bill.


AIM: Write a C program to generate electricity bill

Program:

#include <stdio.h>
#include<conio.h>
int main()
{
int units;
float amount, charge, total_amount;
printf ("Enter total units consumed: ");
scanf ("%d", &units);

if (units <= 50)


{
amount = units * 2.50;
charge = 30;
}
else if (units <= 100)
{
amount = units * 3.00;
charge = 50;
}
else if (units <= 200)
{
amount = units * 3.50;
charge = 75;
}
else if (units <= 300)
{
amount = units * 4.00;
charge = 100;
}
else
{
amount = units * 5.00;
charge = 125;
}
total amount = amount + charge;
printf ("Electricity Bill: %.2f", total amount);
return 0;
}

Output
Enter total units consumed: 280
Electricity Bill: 1220.00

14) Find the roots of the quadratic equation.

AIM: Find the roots of the quadratic equation.

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

int main ()
{
float a, b, c, r1, r2, d;
printf ("Enter the values of a b c: ");
scanf (" %f %f %f", &a, &b, &c);
d= b*b - 4*a*c;
if (d>0)
{
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf ("The real roots = %f %f", r1, r2);
}
else if (d==0)
{
r1 = -b/(2*a);
r2 = -b/(2*a);
printf ("Roots are equal =%f %f", r1, r2);
}
else
printf ("Roots are imaginary");

return 0;
}
Output:
Case 1: Enter the values of a b c:
1
4
3
The real roots = -3.000000 -5.000000
Case 2:
Enter the values of a b c: 1
2
1
Roots are equal =-1.000000 -1.000000
Case 3:
Enter the values of a b c: 1
1
2
Roots are imaginary

15)Write a C program to simulate a calculator using switch case.

AIM: Write a C program to simulate a calculator using switch case.


Program:
#include<stdio.h>
int main ()
{
int num1, num2;
float result=0;
char ch;
printf ("Enter first number = ");
scanf ("%d", &num1);
printf ("Enter second number = ");
scanf ("%d", &num2);
printf ("Choose operator to perform operations = ");
scanf (" %c", &ch);

switch (ch)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf ("Invalid operation\n");
}
printf("The value = %f", result);
return 0;
}

OUTPUT:
Enter first number = 8
Enter second number = 9
Choose operator to perform operations = +
The value = 17.000000

16) Write a C program to find the given year is a leap year or not.
AIM: Write a C program to find the given year is a leap year or not
Program:
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);

if(year%400 == 0)
{
printf("%d is a leap year.\n", year);
}
else
if(year%100 == 0)
printf("%d is not a leap year.\n", year);
else
if(year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
Output:
Enter a year to check if it is a leap year
2016
2016 a leap year.

WEEK6:

17) Find the factorial of given number using any loop.


AIM: Find the factorial of given number using any loop.
Program:
#include <stdio.h>
void main ()
{
int i, fact=1,n;

printf("Input the number : ");


scanf ("%d", &n);

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


fact=fact*i;

printf ("The Factorial of %d is: %d\n",n,fact);


}
Output:
Input the number: 7
The Factorial of 7 is: 5040

18) Find the given number is a prime or not.


AIM: To Find the given number is a prime or not.
Program:
#include <stdio.h>
main ()
{
int n, i, c = 0;
printf ("Enter any number n :");
scanf ("%d", &n);

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


{
if (n % i == 0)
{
c++;
}
}

if (c == 2)
{
printf ("n is a Prime number");
}
else
{
printf ("n is not a Prime number");
}
return 0;
}
Output:
Enter any number n: 7
n is a Prime number

19) Compute sine and cos series.


AIM: To Compute sine and cos series.
Program:
/* program to find the value of sin x */
#include<stdio.h>
#include<math.h>
void main ()
{
float x, sum, term;
int i, n;
printf ("enter the no of terms: \n");
scanf ("%d", &n);
printf ("enter the angle in degrees x: \n");
scanf("%f", &x);
x=(x*3.14)/180;
sum=x;
term=x;
for (i=1; i<=n; i++)
{
term=(term*(-1)*x*x)/((2*i)*(2*i+1));
sum+=term;
}
printf ("sin valve of given angle is %f", sum);
}
Output:
enter the no of terms: 3
enter the angle in degrees x:30
sin valve of given angle is 0.499770

/* program to find the value of cos x */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x, sum, term;
int i,n;
clrscr ();
printf ("enter the no of terms: \n");
scanf ("%d",&n);
printf ("enter the angle in degrees x: \n");
scanf("%f", &x);
x=(x*3.14)/180;
sum=1;
term=1;
for (i=1; i<=n; i++)
{
term=(term*(-1)*x*x)/((2*i)*(2*i-1));
sum+=term;
}
printf("cos valve of given angle is %f",sum);
getch();
}
Output:
enter the no of terms: 3
enter the angle in degrees x: 30
cos valve of given angle is 0.866158

20) Checking a number palindrome.


AIM: To check whether a number is palindrome or not.
Program:
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}

Output:
Enter the number=151
palindrome number

21. Construct a pyramid of numbers

#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i;
++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}

Output:
Enter the number of rows: 8
*
***
*****
*******
*********
***********
*************
***************

WEEK7:

22. Find the min and max of a 1-D integer


array

int main()
{
int arr1[100];
int i, mx, mn, n;

printf("\n\nFind maximum and


minimum element in an array :\n");
printf("-------------------------------------------------
-\n");

printf("Input the number of elements


to be stored in the array :");
scanf("%d",&n);

printf("Input %d elements in the array


:\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}

mx = arr1[0];
mn = arr1[0];
for(i=1; i<n; i++)
{
if(arr1[i]>mx)
{
mx = arr1[i];
}

if(arr1[i]<mn)
{
mn = arr1[i];
}
}
printf("Maximum element is : %d\n",
mx);
printf("Minimum element is : %d\n\n",
mn);
}
Output:

Find maximum and minimum element in


an array :
--------------------------------------------------
Input the number of elements to be stored
in the array :5
Input 5 elements in the array :
element - 0 : 5
element - 1 : 2
element - 2 : 5
element - 3 : 7
element - 4 : 8
Maximum element is : 8
Minimum element is : 2

23. Perform linear search on1D array

#include <stdio.h>

int main()
{
int array[100], search, c, n, count = 0;

printf("Enter number of elements in


array\n");
scanf("%d", &n);

printf("Enter %d numbers\n", n);

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


scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++) {


if (array[c] == search) {
printf("%d is present at location
%d.\n", search, c+1);
count++;
}
}
if (count == 0)
printf("%d isn't present in the
array.\n", search);
else
printf("%d is present %d times in the
array.\n", search, count);

return 0;
}

Output:
Enter number of elements in array
8
Enter 8 numbers
5
5
6
5
6
5
6
5
Enter a number to search
6
6 is present at location 3.
6 is present at location 5.
6 is present at location 7.
6 is present 3 times in the array.

24. The reverse of a 1D integer array

int main()
{
int i,j,n,a[10],m,o,b[10];
printf("\nEnter the Limit:");
scanf("%d",&n);
printf("\nEnter the values:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nGiven values are:");
for(i=0;i<n;i++)
{
printf("\na[%d]=%d",i,a[i]);
}
for(i=0;i<n;i++)
{
b[i]=a[n-i-1];
}
printf("\nUpdated values are :");
for(i=0;i<n;i++)
{
printf("\na[%d]=%d",i,b[i]);
}
return 0;
}

Output:
Enter the Limit: 8

Enter the values:


2
5
4
5
6
7
8
9

Given values are:


a[0]=2
a[1]=5
a[2]=4
a[3]=5
a[4]=6
a[5]=7
a[6]=8
a[7]=9
Updated values are :
a[0]=9
a[1]=8
a[2]=7
a[3]=6
a[4]=5
a[5]=4
a[6]=5
a[7]=2

25. Find 2’s complement of the given


binary number.

#include <stdio.h>
#define SIZE 8
int main()
{
char binary[SIZE + 1], onesComp[SIZE +
1], twosComp[SIZE + 1];
int i, carry=1;
printf("Enter %d bit binary value: ", SIZE);
gets(binary);
for(i=0; i<SIZE; i++)
{
if(binary[i] == '1')
{
onesComp[i] = '0';
}
else if(binary[i] == '0')
{
onesComp[i] = '1';
}
}
onesComp[SIZE] = '\0';

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


{
if(onesComp[i] == '1' && carry == 1)
{
twosComp[i] = '0';
}
else if(onesComp[i] == '0' && carry ==
1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[SIZE] = '\0';

printf("Original binary = %s\n", binary);


printf("Ones complement = %s\n",
onesComp);
printf("Twos complement = %s\n",
twosComp);

return 0;
}

Output:

Enter 8 bit binary value: 10110011


Original binary = 10110011
Ones complement = 01001100
Twos complement = 01001101

26. Eliminate duplicate elements in an


array

#include<stdio.h>
#include<stdlib.h>
int main(){
int a[50],i,j,k, count = 0, dup[50], number;
printf("Enter size of the array");
scanf("%d",&number);
printf("Enter Elements of the array:");
for(i=0;i<number;i++){
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: ");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
for(i=0;i<number;i++){
for(j = i+1; j < number; j++){
if(a[i] == a[j]){
for(k = j; k <number; k++){
a[k] = a[k+1];
}
j--;
number--;
}
}
}
printf("After deleting the duplicate
element the Array is:");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
}

Output:
Enter size of the array5
Enter Elements of the array:
5
5
6
8
9
Entered element are: 5 5 6 8 9
After deleting the duplicate element the
Array is: 5 6 8 9

WEEK8:

27. Addition of two matrices

#include <stdio.h>
int main ()
{
int mat1[3][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8} };
int mat2[3][3] = { {9, 10, 11}, {12, 13, 14}, {15, 16, 17} };
int sum[3][3], i, j;

printf ("matrix 1 is :\n");


for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", mat1[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}

printf ("matrix 2 is :\n");


for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", mat2[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
}
printf ("\nSum of two matrices: \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", sum[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
return 0;
}

Output:

Matrix 1 is:
0 1 2

3 4 5

6 7 8

Matrix 2 is:
9 10 11

12 13 14

15 16 17

Sum of two matrices:


9 11 13

15 17 19

21 23 25

28. Multiplication of two matrices


#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;

printf("enter the number of row=");


scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

29.Sort array elements using bubble sort


#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
Output: Enter number of elements
8
Enter 8 integers
2
5
6
8
9
8
7
8
Sorted list in ascending order:
2
5
6
7
8
8
8
9

30. Concatenate two strings without built-


in functions

#include<stdio.h>
int main(void)
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}

Output:

Enter First String:bus

Enter Second String:college

Concatenated String is buscollege

31. Reverse a string using built-in and without built-in string functions
AIM:(i) To write a program in Reverse a string using built-in string functions
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
printf("Enter a string to reverse\n");
gets(s);
strrev(s);
printf("Reverse of the string: %s\n", s);
return 0;
}
Output : Hello
olleH
AIM:(ii) To write a program in Reverse a string with out using built-in string functions
Program:
#include <stdio.h>
#include <string.h>
void main()
{
char string[20],temp;
int i,length;
printf("Enter String : ");
scanf("%s",string);
length=strlen(string)-1;
for(i=0;i<strlen(string)/2;i++)
{
temp=string[i];
string[i]=string[length];
string[length--]=temp;
}
printf("Reverse string :%s",string);
}
Out put : Enter String : hai
Reverse string :iah

WEEK9:

32.Write a C program to find the sum ofa 1D array using malloc()


Aim:Write a C program to find the sum ofa 1D array using malloc()
Program:
#include <stdio.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));
}
}
Output:
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

33.Write a C program to find the total, average of n students using structures


Aim:Write a C program to find the total, average of n students using structures
Program:
#include<stdio.h>
struct student
{
int sub1;
int sub2;
int sub3;
};
void main()
{
int i,total=0,n=10,average;
struct student s[n];
for(i=0;i<n=n;i++)
{
printf("\nEnter Marks in Three Subjects = ");
scanf("%d%d%d",& s[i].sub1,&s[i].sub2,&s[i].sub3);
total=s[i].sub1+s[i].sub2+s[i].sub3;
average=total/3;
printf("\nTotal marks of s[%d] Student= %d",i,total);
printf("\n average marks of s[%d] Student= %d",i,average);
}
}
Output:Enter Marks in Three Subjects =88
89
87
Total marks of s[0] Student= 264
average marks of s[0] Student= 88
Enter Marks in Three Subjects =89
98
86
Total marks of s[1] Student= 273
average marks of s[1] Student= 91
Enter Marks in Three Subjects =69
96
98
Total marks of s[2] Student= 263
average marks of s[2] Student= 87
Enter Marks in Three Subjects =-----
34.Enter n students data using calloc() and display failed students list
Aim:Enter n students data using calloc() and display failed students list
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
printf("Memory successfully allocated using calloc.\n");
for (i = 0; i < n; ++i)
{
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
{
printf("%d, ", ptr[i]);
}
}

return 0;
}
Output : Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
35.Read student name and marks from the command line and display the student
details along with the total
Aim:Read student name and marks from the command line and display the student
details along with the total
Program:
#include <stdio.h>
struct student {
char name[50];
int roll;
float sub1;
float sub2;
float marks;
} s;
int main()
{
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter sub1 ");
scanf("%f", & s.sub1);
printf("Enter sub2 ");
scanf("%f", & s.sub2);
s.marks=s.sub1+s.sub2;
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;
}
Output:Enter information:
Enter name: darshi
Enter roll number: 1
Enter sub1 89
Enter sub2 90
Displaying Information:
Name: darshi
Roll number: 1
Marks: 179.0
36.Write a C program to implement realloc()
Aim:Write a C program to implement realloc()
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr = (int*) malloc(3 * sizeof(int));
ptr[0] = 1;
ptr[1] = 2;
ptr[2] = 3;
ptr = (int*) realloc(ptr, 5 * sizeof(int));
ptr[3] = 4;
ptr[4] = 5;
for (int i = 0; i< 5; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}
Output: 1 2 3 4 5

WEEK10:

37.Create and display a singly linked list using self-referential structure.


Aim:Create and display a singly linked list using self-referential structure
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void display(struct Node* temp)
{
while (temp) {
printf(" %d ", temp->data);
temp= temp->next;
}
}
void main()
{
struct Node* head =NULL;
struct Node* second_node = NULL;
struct Node* third_node = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second_node = (struct Node*)malloc(sizeof(struct Node));
third_node = (struct Node*)malloc(sizeof(struct Node));
head->data = 10; // assign data in first node
head->next = second_node; // Link first node with second
second_node->data = 200; // assign data to second node
second_node->next = third_node;
third_node->data = 3000;
third_node->next = NULL;
display(head);
}
Output: 10 200 3000
38.Demonstrate the differences between structures and unions using a C program
Aim:Demonstrate the differences between structures and unions using a C program
Program:
#include <stdio.h>
#include <string.h>
struct struct_example
{
int integer;
float decimal;
char name[20];
};
union union_example
{
int integer;
float decimal;
char name[20];
};

void main()
{
struct struct_example s = { 18, 38, "geeksforgeeks" };
union union_example u = { 18, 38, "geeksforgeeks" };
printf("structure data:\n integer: %d\n"
"decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
printf("\nunion data:\n integer: %d\n"
"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
printf("\nsizeof structure : %d\n", sizeof(s));
printf("sizeof union : %d\n", sizeof(u));
printf("\n Accessing all members at a time:");
s.integer = 183;
s.decimal = 90;
strcpy(s.name, "geeksforgeeks");
printf("structure data:\n integer: %d\n "
"decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
u.integer = 183;
u.decimal = 90;
strcpy(u.name, "geeksforgeeks");
printf("\nunion data:\n integer: %d\n "
"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
printf("\n Accessing one member at time:");
printf("\nstructure data:");
s.integer = 240;
printf("\ninteger: %d", s.integer);
s.decimal = 120;
printf("\ndecimal: %f", s.decimal);
strcpy(s.name, "C programming");
printf("\nname: %s\n", s.name);
printf("\n union data:");
u.integer = 240;
printf("\ninteger: %d", u.integer);
u.decimal = 120;
printf("\ndecimal: %f", u.decimal);
strcpy(u.name, "C programming");
printf("\nname: %s\n", u.name);
// difference four
printf("\nAltering a member value:\n");
s.integer = 1218;
printf("structure data:\n integer: %d\n "
" decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
u.integer = 1218;
printf("union data:\n integer: %d\n"
" decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
}
Output : structure data:
integer: 18
decimal: 38.00
name: geeksforgeeks
union data:
integer: 18
decimal: 0.00
name:
sizeof structure : 28
sizeof union : 20
Accessing all members at a time:structure data:
integer: 183
decimal: 90.00
name: geeksforgeeks
union data:
integer: 1801807207
decimal: 277322871721159507258114048.00
name: geeksforgeeks
Accessing one member at time:
structure data:
integer: 240
decimal: 120.000000
name: C programming
union data:
integer: 240
decimal: 120.000000
name: C programming
Altering a member value:
structure data:
integer: 1218
decimal: 120.00
name: C programming
union data:
integer: 1218
decimal: 0.00
name:
39.Write a C program to shift/rotate using bitfields
Aim:Write a C program to shift/rotate using bitfields
Program:
#include <stdio.h>
#define INT_BITS 32
int leftRotate(int n, unsigned int d)
{
return (n << d) | (n >> (INT_BITS - d));
}
int rightRotate(int n, unsigned int d)
{
return (n >> d) | (n << (INT_BITS - d));
}
void main()
{
int n = 16;
int d = 2;
printf("Left Rotation of %d by %d is ", n, d);
printf("%d", leftRotate(n, d));
printf(" Right Rotation of %d by %d is ", n, d);
printf("%d", rightRotate(n, d));
}
Output : Left Rotation of 16 by 2 is 64 Right Rotation of 16 by 2 is 4.
40.Write a C program to copy one structure variable to another structure of the same
type
Aim:Write a C program to copy one structure variable to another structure of the same
type
Program:
#include <stdio.h>
struct myStructure
{
int myNum;
char myLetter;
char myString[30];
};
int main()
{
struct myStructure s1 = {13, 'B', "Some text"};
struct myStructure s2={14,'c',"hello"};
struct myStructure s3;
s3 = s2;
printf("%d %c %s", s3.myNum, s3.myLetter, s3.myString);
return 0;
}
Output:14 c hello

WEEK11:

41. Write a C function to calculate NCR value


AIM: To Write a C function to calculate NCR value
Program:
#include <stdio.h>
int fact(int z);
void main()
{
int n, r, ncr;
printf("\n Enter the value for N and R \n");
scanf("%d%d", &n, &r);
ncr = fact(n) / (fact(r) * fact(n - r));
printf("\n The value of ncr is: %d", ncr);
}
int fact(int z)
{
int f = 1, i;
if (z == 0)
{
return(f);
}
else
{
for (i = 1; i <= z; i++)
{
f = f * i;
}
}
return(f);
}
Output:
Enter the value for N and R
52
The value of ncr is: 10

42.Write a C function to find the length of a string.


AIM: to write a C function to find the length of a string.
Program:
#include<stdio.h>
#include<string.h>
int main()
{
char str[50];
int l;
printf("Enter a string:");
gets(str);
l=strlen(str);
printf("String length=%d",l);

}
Output:
Enter a string:programming
String length=11

43.Write a C function to transpose of a matrix.


AIM: to write a C function to transpose of a matrix.
Program:
#include<stdio.h>
int main()
{
int a[20][20],i,j,m,n;
printf("Enter how many rows & columns:");
scanf("%d%d",&m,&n);
printf("Enter %d elements:",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

}
printf("\nMatrix is:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
}
printf("\nTranspose Matrix is:\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
printf("%d\t",a[j][i]);
}
}
Output:
Enter how many rows & columns:3 3
Enter 9 elements:1 2 3 4 5 6 7 8 9
Matrix is:

1 2 3
4 5 6
7 8 9
Transpose Matrix is:

1 4 7
2 5 8
3 6 9

44.Write a C function to demonstrate numerical integration of differential equations


using Euler’s method
AIM: to write a C function to demonstrate numerical integration of differential equations
using Euler’s method
Source code:
#include<stdio.h>
float fun(float x,float y)
{
float f;
f=x+y;
return f;
}
main()
{
float a,b,x,y,h,t,k;
printf("\nEnter x0,y0,h,xn: ");
scanf("%f%f%f%f",&a,&b,&h,&t);
x=a;
y=b;
printf("\n x\t y\n");
while(x<=t)
{
k=h*fun(x,y);
y=y+k;
x=x+h;
printf("%0.3f\t%0.3f\n",x,y);
}
}
output:
Enter x0,y0,h,xn: 0 1 0.1 1
x y
0.100 1.100
0.200 1.220
0.300 1.362
0.400 1.528
0.500 1.721
0.600 1.943
0.700 2.197
0.800 2.487
0.900 2.816
1.000 3.187

WEEK12:

45.Write a recursive function to generate Fibonacci series.


AIM: to write a recursive function to generate Fibonacci series.
Source code:
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
OUTPUT:
8
Fibonacci series
0
1
1
2
3
5
8
13
46. Write a recursive function to find LCM of two numbers.
AIM:to write a recursive function to find LCM of two numbers.
Source code:
#include <stdio.h>

int lcm(int, int);

int main()
{
int a, b, result;

printf("Enter two numbers: ");


scanf("%d%d", &a, &b);
result = lcm(a, b); //call the function lcm recursively.
printf("The LCM of %d and %d is %d\n", a, b, result);
return 0;
}

int lcm(int a, int b)


{
static int common = 1;

if (common % a == 0 && common % b == 0)


{
return common;
}
common++;
lcm(a, b); //call the function lcm recursively.
}
Output:
Enter two numbers: 2 3
The LCM of 2 and 3 is 6

Enter two numbers: 16 15


The LCM of 16 and 15 is 240

47. Write a recursive function to find factorial of a number.


AIM: to write a program to find factorial of a number using recursive function.
Source code:
#include<stdio.h>
unsigned long long int factorial(unsigned int i)
{
if(i<=1)
{
return 1;
}
return i*factorial(i-1);
}
int main()
{
int i=11;
printf("Factorial of %d is %d\n",factorial(i));
return 0;
}
OUTPUT:
Factorial of 39916800 is -267337928

48. Write a c program to implement Ackermann function by using recursive function


Ackerman function: In computability theory, the Ackermann function, named
after Wilhelm Ackermann, is one of the simplest and earliest-discovered examples of a
total computable function that is not primitive recursive. All primitive recursive
functions are total and computable, but the Ackermann function illustrates that not all
total computable functions are primitive recursive.
Ackermann function is defined as:

#include<stdio.h>
int A(int m, int n);

main()
{
int m,n;
printf("Enter two numbers :: \n");
scanf("%d%d",&m,&n);
printf("\nOUTPUT :: %d\n",A(m,n));
}

int A(int m, int n)


{
if(m==0)
return n+1;
else if(n==0)
return A(m-1,1);
else
return A(m-1,A(m,n-1));
}
OUTPUT:
Enter two numbers ::
1
0
OUTPUT :: 2

Enter two numbers ::


0
5
OUTPUT :: 6

49.Write a recursive function to find the sum of series.


AIM: to write a recursive function to find the sum of series.
Source code:
/ C program to find the sum of n
// natural numbers using recursion
#include <stdio.h>

// Returns the sum of first n


// natural numbers
int recSum(int n)
{
// Base condition
if (n <= 1)
return n;

// Recursive call
return n + recSum(n - 1);
}

// Driver code
int main()
{
int n;
printf("enter a value for series of sum\n ");
scanf("%d",&n);
printf("Sum = %d ", recSum(n));
return 0;
}
OUTPUT:
enter a value for series of sum
55
Sum = 1540

WEEK13:

50.Write a c program to swap two numbers using call by reference.


AIM: to write a c program to swap two numbers using call by reference.
Source code:
#include<stdio.h>
void swap(int*, int*);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
OUTPUT:
Enter the value of x and y
20
30
Before Swapping
x = 20
y = 30
After Swapping
x = 30
y = 20

51. Write a C program to demonstrate dangling pointer.


Source code:
#include <stdio.h>
int *fun()
{
static int y=10;
return &y;
}
int main()
{
int *p=fun();
printf("%d", *p);
return 0;
}
Output:
10
52. Write a c program to copy one string to another using pointer
AIM: to Write a c program to copy one string to another using pointer

Source code:

#include<stdio.h>
#include<conio.h>
void main()
{
char *str1, *str2;
int i;
clrscr();
printf("Enter the string : ");
scanf("%s", str2);
for(i = 0; *str2 != '\0'; i++, str1++, str2++)
*str1 = *str2;
*str1 = '\0';
str1 = str1 - i;
printf("\nThe copied string is : %s", str1);
getch();
}
OUTPUT:

Enter the string : bhuvan


The copied string is : bhuvan

53. Write a C program to count no.of characters, digits and special character in a string.
#include<stdio.h>

int main()
{
char str[100];
int i,a=0,d=0,s=0;
printf("\nEnter The String : ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
if((str[i]>=65&&str[i]<=90) || (str[i]>=97&&str[i]<=122))
a++;
else if (str[i]>=48&&str[i]<=57)
d++;
else
s++;
}
printf("\nTotal Alphabets : %d",a);
printf("\nTotal Digits : %d",d);
printf("\nTotal Special : %d",s);
return 0;
}

Output
Enter The String : Tutor joes @ 2014
Total Alphabets :9
Total Digits :4
Total Special :5

Week14:
54. Write a C program to write and read text into a file.
Source code:

#include< stdio.h >

int main()

FILE *fp; /* file pointer*/

char fName[20];

printf("\nEnter file name to create :");

scanf("%s",fName);

/*creating (open) a file*/

fp=fopen(fName,"w");

/*check file created or not*/

if(fp==NULL)

printf("File does not created!!!");

exit(0); /*exit from program*/

printf("File created successfully.");

/*writting into file*/

putc('A',fp);

putc('B',fp);

putc('C',fp);
printf("\nData written successfully.");

fclose(fp);

/*again open file to read data*/

fp=fopen(fName,"r");

if(fp==NULL)

printf("\nCan't open file!!!");

exit(0);

printf("Contents of file is :\n");

printf("%c",getc(fp));

printf("%c",getc(fp));

printf("%c",getc(fp));

fclose(fp);

return 0;

Output

Enter file name to create : ok.txt

File created successfully.

Data written successfully.

Contents of file is :

ABC

55. Write a c program to write and read text into a binary file using fread() and fwrite()

Source code:

#include<stdio.h>

/* Our structure */

struct record
{

int a,b,c;

};

int main()

int count;

FILE *ptr;

struct record myRecord;

ptr=fopen("test.bin","rb");

if (!ptr)

printf("Unable to open file!"); return 1;

for ( count=1; count <= 10; count++)

fread(&myRecord,sizeof(struct record),1,ptr); printf("%d\n",myRecord.a);

} fclose(ptr);

return 0;

#include<stdio.h>

/* Our structure */

struct record

int a,b,c;

};

int main()

int count;

FILE *ptr;

struct record myRecord;


ptr=fopen("test.bin","wb");

if (!ptr)

printf("Unable to open file!");

return 1;

for ( count=0; count < 10; count++)

myRecord.a= count;

fwrite(&myRecord, sizeof(struct record), 1, ptr);

fclose(ptr);

return 0;

56. write a c program to copy the contents of one file to another file.

Source code:

#include <stdio.h>

#include <stdlib.h> // For exit()

int main(){

FILE *fptr1, *fptr2;

char filename[100], c;

printf("Enter the filename to open for reading


");

scanf("%s",filename);

// Open one file for reading

fptr1 = fopen(filename, "r");

if (fptr1 == NULL){

printf("Cannot open file %s


", filename);
exit(0);

printf("Enter the filename to open for writing


");

scanf("%s", filename);

// Open another file for writing

fptr2 = fopen(filename, "w");

if (fptr2 == NULL){

printf("Cannot open file %s


", filename);

exit(0);

// Read contents from file

c = fgetc(fptr1);

while (c != EOF){

fputc(c, fptr2);

c = fgetc(fptr1);

printf("
Contents copied to %s", filename);

fclose(fptr1);

fclose(fptr2);

return 0;

Output:

Enter the filename to open for reading


file3.txt
Enter the filename to open for writing
file1.txt
Contents copied to file1.txt

57. write a C program to merge two files into third file

Source code:
#include <stdio.h>

#include <stdlib.h>

int main()

// Open two files to be merged

FILE *fp1 = fopen("file1.txt", "r");

FILE *fp2 = fopen("file2.txt", "r");

// Open file to store the result

FILE *fp3 = fopen("file3.txt", "w");

char c;

if (fp1 == NULL || fp2 == NULL || fp3 == NULL)

puts("Could not open files");

exit(0);

// Copy contents of first file to file3.txt

while ((c = fgetc(fp1)) != EOF)

fputc(c, fp3);

// Copy contents of second file to file3.txt

while ((c = fgetc(fp2)) != EOF)

fputc(c, fp3);

printf("Merged file1.txt and file2.txt into file3.txt");

fclose(fp1);

fclose(fp2);
fclose(fp3);

return 0;

Output:

Merged file1.txt and file2.txt into file3.txt

58. Write a c program to find no. of lines words and characters in a file.

Source code:

int main()

FILE *fp;

int count = 0,word=0,character=0;

char filename[MAX_FILE_NAME];

char c; // To store a character read from file

// Get file name from user. The file should be

// either in current folder or complete path should be provided

printf("Enter file name: ");

scanf("%s", filename);

// Open the file

fp = fopen(filename, "r");

// Check if file exists

if (fp == NULL)

printf("Could not open file %s", filename);

return 0;

}
// Extract characters from file and store in character c

for (c = getc(fp); c != EOF; c = getc(fp)){

if (c == '\n') // Increment count if this character is newline

count = count + 1;

else if(c==’ ‘)

word++;

else

character++;

// Close the file

fclose(fp);

printf("The file %s has %d lines\n %d words\n %d characters ", filename, count,word,character);

return 0;

Output:

Enter file name: countLines.c

The file countLines.c has 41 lines

102 words

210 characters

59. Write a c program to print last n characters of a given file


Source code:
#include<stdio.h>

#include<stdlib.h>

int main() {

FILE *fp;

char ch;
int num;

long length;

printf("Enter the value of n : ");

scanf("%d", &num);

fp = fopen("C:\\Users\\acer\\Documents\\file4.txt", "r");

if (fp == NULL) {

puts("\ncannot open this file");

exit(1);

fseek(fp, 0, SEEK_END);

length = ftell(fp);

fseek(fp, (length - num), SEEK_SET);

do {

ch = fgetc(fp);

putchar(ch);

} while (ch != EOF);

fclose(fp);

return(0);

OUTPUT : :

/* C program to read last n characters of text file */

Enter the value of n : 60


of. Principle september she conveying did eat may extensive.

You might also like