Programs
Programs
Course Objectives:
The course aims to give students hands – on experience and train them on the concepts of the C- programming
language.
Course Outcomes:
CO1: Read, understand, and trace the execution of programs written in C language.
CO2: Select the right control structure for solving the problem.
CO3: Develop C programs which utilize memory efficiently using programming constructs like pointers.
CO4: Develop, Debug and Execute programs to demonstrate the applications of arrays, functions, basic concepts of
pointers in C.
WEEK 1
i) Basic Linux environment and its editors like Vi, Vim & Emacs etc.
• Linux text editors can be used for editing text files, writing codes, updating user instruction files, and more.
• A Linux system supports multiple text editors.
• There are two types of text editors in Linux
1) Command-line text editors such as Vi/Vim
Vim editor is one of the most used and powerful command-line based editor of the Linux
system.
Source Code:
#include <stdio.h>
int main()
{
float num1, num2, num3;
float sum, average;
printf("Enter three numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);
sum = num1 + num2 + num3;
average = sum / 3;
printf("Sum = %f\n", sum);
printf("Average = %f\n", average);
return 0;
}
Expected Output :
Enter three numbers: 3 5 4
Sum = 12.000000
Average = 4.000000
Actual Output:
ii) Conversion of Fahrenheit to Celsius and vice versa
Algorithm : Flowchart:
Input: Fahrenheit value
Output: Celsius heat value
Step 1: Start.
Step 2: Read F.
Step 3: C=(5(F-32))/9.
Step 4: Print C.
Step 5: Stop
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius, fahrenheit;
clrscr();
printf("\n Enter Temp in Fahrenheit : ");
scanf("%f", &fahrenheit);
celsius = ((fahrenheit-32) *5)/9 ;
printf("\n Temperature in Celsius : %.2f ", celsius);
}
Expected Output :
Enter Temp in Fahrenheit : 41
Temperature in Celsius : 5
Actual Output:
iii) Simple interest calculation
Algorithm : Flowchart:
Input: Principal, Time(years) and Rate
Output: Simple Interest
Step 1:Start
Step 2:Read Principal Amount, Rate and Time
Step 3:Calculate Interest using formula SI= ((amount*rate*time)/100)
Step 4:Print Simple Interest
Step 5:Stop
Source Code:
# include <stdio.h>
# include <stdlib.h>
int main()
int P, R, T;
double SI;
scanf("%d", &P);
scanf("%d", &R);
scanf("%d", &T);
SI = (P * R * T) / 100;
return 0;
Expected Output :
Enter the principal: 1000
Actual Output:
Week 3
Lab 3: Simple computational problems using arithmetic expressions.
i) Finding the square root of a given number
Source Code:
/* C program to find square root of a number */
#include <stdio.h>
#include <math.h>
int main()
{
double num, root;
// Input a number from user
printf("Enter any number to find square root: ");
scanf("%lf", &num);
// Calculate square root of num
root = sqrt(num);
// Print the resultant value
printf("Square root of %.2lf = %.2lf", num, root);
return 0;
}
Expected Output :
Enter any number to find square root: 25
Square root of 25 = 5.00000
Actual Output:
ii) Finding compound interest
Source Code:
#include<stdio.h>
#include<math.h>
int main()
{
double principal,rate,time, Amount,CI;
printf(" Enter the Principal Amount: ");
scanf("%lf",&principal);
printf(" Enter the rate of interest: ");
scanf("%lf",&rate);
printf(" Enter the Time in years: ");
scanf("%lf",&time);
Amount = principal * ((pow((1 + rate / 100), time)));
CI = Amount - principal;
printf("Compound Interest is : %lf",CI);
return 0;
}
Expected Output :
Enter the Principal Amount: 1000
Enter the rate of interest: 5
Enter the Time in years: 2
Compound Interest is : 102.500000.
Actual Output:
iii) Area of a triangle using heron’s formulae
Source Code:
# include<stdio.h>
# include <math.h>
int main()
{
// area of triangle by using heron's formula
float a,b,c,s,area;
printf("Enter the length of three sides of a triangle:\n");
scanf("%f%f%f", &a,&b,&c);
s = (a+b+c)/2;
printf("The value of S is %.2f\n",s);
area = sqrt(s*(s-a) * (s-b) * (s-c));
printf("The area of the triangle is:%f\n", area);
return 0;
}
Expected Output :
Enter the length of three sides of a triangle:
10 15 20
The value of S is 22.50
The area of the triangle is:72.618439
Actual Output:
iv) Distance travelled by an object
Source Code:
#include<stdio.h>
int main()
{
float u, a, s;
int t;
printf("Enter initial velocity : ");
scanf("%f",&u);
printf("Enter acceleration : ");
scanf("%f",&a);
printf("Enter time : ");
scanf("%d",&t);
s=(u*t)+(a*t*t)/2;
printf("\nDistance travelled by an object= %.2f",s);
return 0;
}
Expected Output :
Enter initial velocity : 4
Enter acceleration : 5
Enter time : 9
Distance travelled by an object= 238.50
Actual Output:
Week 4
Lab 4: Simple computational problems using the operator’ precedence and associativity
i) 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)
Source Code:
#include<stdio.h>
int main()
{
float A,B,C,D,E,F,G,I,result1,result2,result3,result4;
printf("Enter values of A B C D E F G I : ");
scanf("%f%f %f %f %f %f %f ",&A,&B,&C,&D,&E,&F,&G,&I);
result1= A+B*C+(D*E) + F*G;
result2=A/B*C-B+A*D/3;
result3= A+++B---A;
result4=(I++) + (++I)
printf("\The Result of A+B*C+(D*E) + F*G is = %.2f",result1);
printf("\The Result of A/B*C-B+A*D/3 is = %.2f",result2);
printf("\The Result of A+++B---A is = %.2f",result3);
printf("\The Result of (I++) + (++I) is = %.2f",result4);
return 0;
}
Expected Output :
Enter values of A B C D E F G I : 8 7 6 5 4 3 2 1
Expected Output :
Enter a number: 5
Factorial of 5 is: 120
Actual Output:
ii) Find the given number is a prime or not.
Source Code:
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
Expected Output :
Enter a positive integer: 29
29 is a prime number.
Actual Output:
iii) Compute sine and cos series
Source Code:
#include<stdio.h>
#include<math.h>
void main()
{
int n, x1, i, j;
float x, sign, cosx, fact, sum, t;
printf("Enter the number of the terms in a series\n");
scanf("%d", & n);
printf("Enter the value of x(in degrees)\n");
scanf("%f", & x);
x1 = x;
x = x * (3.142 / 180.0); /* Degrees to radians*/
cosx = 1;
sign = -1;
for (i = 2; i <= n; i = i + 2)
{
fact = 1;
for (j = 1; j <= i; j++)
{
fact = fact * j;
}
cosx = cosx + (pow(x, i) / fact) * sign; /*calculating the cosx x sum*/
sign = sign * (-1);
}
printf("Sum of the cosine series= %7.2f\n", cosx);
printf("The value of cos(%d) using library method = %f\n", x1, cos(x));
// Program code for Sine Series
printf("Enter the value for x : ");
scanf("%f",&x);
printf("Enter the value for n : ");
scanf("%d",&n);
x=x*3.14159/180;
t=x;
sum=x;
/* Loop to calculate the value of Sine */
for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
printf("The value of Sin(%f) = %.4f",x,sum);
}
/*End of main() */
Expected Output :
Enter the number of the terms in a series
5
Enter the value of x(in degrees)
6
Sum of the cosine series= 0.99
The value of cos(6) using library method = 0.994520
Enter the value for x : 7
Enter the value for n : 7
The value of Sin(0.122173) = 0.1219
Actual Output:
iv) Checking a number palindrome
Source Code:
#include"stdio.h"
void main()
{
int num,org,rem=0,rev;
printf("\nEnter a number : ");
scanf("%d",&num);
org=num;
for(int i=num;num>0;i++)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
if(rev==org)
printf("\n %d is palindrome",rev);
else
printf("\n %d is not a palindrome",rev);
}
Expected Output :
Enter an integer: 1001
1001 is a palindrome.
Actual Output:
v) Construct a pyramid of numbers.
Source Code:
#include <stdio.h>
int main()
{
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; ++j)
{
printf("%4d ", number);
++number;
}
printf("\n");
}
return 0;
}
Expected Output :
Enter the number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Actual Output:
WEEK 7
i) Find the min and max of a 1-D integer array.
Source Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int a[1000],i,n,min,max;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("\n minimum of array is : %d",min);
printf("\n maximum of array is : %d",max);
return 0;
}
Expected Output :
Enter size of the array: 5
Enter elements in array: 1
2
3
4
5
minimum of an array is: 1
maximum of an array is: 5
Actual Output:
ii) Perform linear search on1D array.
Source Code:
#include <stdio.h>
int main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
return 0;
}
Expected Output :
Enter the number of elements in array
5
Enter 5 numbers
12
23
22
10
45
Enter the number to search
22
22 is present at location 3.
Actual Output:
iii) The reverse of a 1D integer array.
Source Code:
#include <stdio.h>
#define N 1000
int main()
{
int arr[N];
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter an array: ");
for (int i = 0; i< n; i++)
{
scanf("%d", &arr[i]);
}
printf("Reversed array: ");
for (int i = n-1; i>=0; i--)
{
printf("%d ", arr[i]);
}
return 0;
}
Expected Output :
Enter the size of the array: 5
Enter an array: 3 8 4 9 6
Reversed array: 6 9 4 8 3
Actual Output:
iv) Find 2’s complement of the given binary number
Source Code:
#include <stdio.h>
int main()
{
int n; // variable declaration
printf("Enter the number of bits do you want to enter :");
scanf("%d",&n);
char binary[n+1]; // binary array declaration;
char onescomplement[n+1]; // onescomplement array declaration
char twoscomplement[n+1]; // twoscomplement array declaration
int carry=1; // variable initialization
printf("\nEnter the binary number : ");
scanf("%s", binary);
printf("%s", binary);
printf("\nThe ones complement of the binary number is :");
// Finding onescomplement in C
for(int i=0;i<n;i++)
{
if(binary[i]=='0')
onescomplement[i]='1';
else if(binary[i]=='1')
onescomplement[i]='0';
}
onescomplement[n]='\0';
printf("%s",onescomplement);
printf("\nThe twos complement of a binary number is : ");
// Finding twoscomplement in C
for(int i=n-1; i>=0; i--)
{
if(onescomplement[i] == '1' && carry == 1)
{
twoscomplement[i] = '0';
}
else if(onescomplement[i] == '0' && carry == 1)
{
twoscomplement[i] = '1';
carry = 0;
}
else
{
twoscomplement[i] = onescomplement[i];
}
}
twoscomplement[n]='\0';
printf("%s",twoscomplement);
return 0;
}
Expected Output :
Enter the number of bits do you want to enter :6
Enter the binary number : 101010
101010
The ones complement of the binary number is :010101
The twos complement of a binary number is : 010110
Actual Output:
v) Eliminate duplicate elements in an array
Source Code:
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare local variables
int arr[20], i, j, k, size;
printf (" Define the number of elements in an array: ");
scanf (" %d", &size);
printf (" \n Enter %d elements of an array: \n ", size);
// use for loop to enter the elements one by one in an array
for ( i = 0; i < size; i++)
{
scanf (" %d", &arr[i]);
}
// use nested for loop to find the duplicate elements in array
for ( i = 0; i < size; i ++)
{
for ( j = i + 1; j < size; j++)
{
// use if statement to check duplicate element
if ( arr[i] == arr[j])
{
// delete the current position of the duplicate element
for ( k = j; k < size - 1; k++)
{
arr[k] = arr [k + 1];
}
// decrease the size of array after removing duplicate element
size--;
// if the position of the elements is changes, don't increase the
index j
j--;
}
}
}
/* display an array after deletion or removing of the duplicate
elements */
printf (" \n Array elements after deletion of the duplicate
elements: ");
// for loop to print the array
for ( i = 0; i < size; i++)
{
printf (" %d \t", arr[i]);
}
return 0;
}
Expected Output :
Define the number of elements in an array: 10
Enter 10 elements of an array:
57
12
89
32
62
12
89
35
67
75
Array elements after deletion of the duplicate elements: 57 12 89 32 62
35 67 75
Actual Output:
WEEK 8
i) Write a C program Addition of two matrices.
Source Code:
#include <stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}
Expected Output :
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3
Enter elements of 1st matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3
Sum of two matrices:
-2 8 7
10 8 6
Actual Output
ii) Write a C program Multiplication two matrices
Source Code:
#include<stdio.h>
int main()
{
int r1,r2,c1,c2;
printf("Enter number of rows for First Matrix:\n");
scanf("%d",&r1);
printf("Enter number of columns for First Matrix:\n");
scanf("%d",&c1);
printf("Enter number of rows for Second Matrix:\n");
scanf("%d",&r2);
printf("Enter number of columns for Second Matrix:\n");
scanf("%d",&c2);
if(c1!=r2)
{
printf("Matrices Can't be multiplied together");
}
else
{
int m1[r1][c1],m2[r2][c2];
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
int mul[r1][c2];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
mul[i][j]=0;
// Multiplying i’th row with j’th column
for(int k=0;k<c1;k++)
{
mul[i][j]+=m1[i][k]*m2[k][j];
}
}
}
printf("Multiplied matrix\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
return 0;
}
Expected Output :
Enter number of rows for First Matrix:
2
Enter number of columns for First Matrix:
2
Enter number of rows for Second Matrix:
2
Enter number of columns for Second Matrix:
2
Enter first matrix elements
1 3
5 6
Enter Second matrix elements
2 1
4 4
Multiplied matrix
14 13
34 29
Actual Output
iii) Write a C program Sort array elements using bubble sort
Source Code:
#include <stdio.h>
int main()
{
int arr[50], num, x, y, temp;
printf("Please Enter the Number of Elements you want in the array: ");
scanf("%d", &num);
printf("Please Enter the Value of Elements: ");
for(x = 0; x < num; x++)
scanf("%d", &arr[x]);
for(x = 0; x < num - 1; x++)
{
for(y = 0; y < num - x - 1; y++)
{
if(arr[y] > arr[y + 1])
{
temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
printf("Array after implementing bubble sort: ");
for(x = 0; x < num; x++)
{
printf("%d ", arr[x]);
}
return 0;
}
Expected Output :
Please Enter the Number of Elements you want in the array:5
Please Enter the Value of Elements:9 5 -2 7 3
Array after implementing bubble sort:-2 3 5 7 9
Actual Output :
iv) Write a C program Concatenate two strings without built-in functions
Source Code:
#include<stdio.h>
void 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);
}
Expected Output :
Enter First String: Sai
Enter Second String: Kumar
Concatenated String is SaiKumar
Actual Output :
v) Write a C program to Reverse a string using built-in and without built-in string functions.
Source Code:
#include <stdio.h>
#include <conio.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);
}
Expected Output :
Actual Output :
WEEK 9
i) Write a C program to find the sum of a 1D array using malloc().
Source Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr; //declaration of integer pointer
int limit; //to store array limit
int i; //loop counter
int sum; //to store sum of all elements
printf("Enter limit of the array: ");
scanf("%d", &limit);
//declare memory dynamically
ptr = (int*)malloc(limit * sizeof(int));
//read array elements
for (i = 0; i < limit; i++)
{
printf("Enter element %02d: ", i + 1);
scanf("%d", (ptr + i));
}
//print array elements
printf("\nEntered array elements are:\n");
for (i = 0; i < limit; i++)
{
printf("%d\n", *(ptr + i));
}
//calculate sum of all elements
sum = 0; //assign 0 to replace garbage value
for (i = 0; i < limit; i++)
{
sum += *(ptr + i);
}
printf("Sum of array elements is: %d\n", sum);
//free memory
free(ptr); //hey, don't forget to free dynamically allocated memory.
return 0;
}
Expected Output :
Enter limit of the array: 5
Enter element 01: 2
Enter element 02: 6
Enter element 03: 8
Enter element 04: 7
Enter element 05: 9
Entered array elements are:
2
6
8
7
9
Sum of array elements is: 32
Actual Output :
ii) Write a C program to find the total, average of n students using structures
Source Code:
#include<stdio.h>
struct student
{
char usn[10];
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
{
struct student s[20];
int n,i;
float tavg,sum=0.0;
printf("Enter the number of student=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the detail of %d students\n",i+1);
printf("\n Enter USN=");
scanf("%s",s[i].usn);
printf("\n Enter Name=");
scanf("%s",s[i].name);
printf("Enter the three subject score\n");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
}
for(i=0;i<n;i++)
{
if(s[i].avg>=35)
printf("\n %s has scored above the average marks",s[i].name);
else
printf("\n %s has scored below the average marks",s[i].name);
}
}
Expected Output :
Enter the number of students 2
Enter the details of 1 students
Enter USN = 100
Enter the name = Ricky
Enter the three subject score = 10 21 15
Enter the details of 1 students
Enter USN = 222
Enter the name = Krish
Enter the three subject score = 11 9 10
Ricky has scored above the average marks
Krish has scored below the average marks
Actual Output :
iii) Write a C program to Enter n students data using calloc() and display failed students list
Source Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n=5, i;
printf("Enter number of elements: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
// Check if the memory has been successfully
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;
}
Expected Output :
Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
Actual Output :
v) Write a C program to implement realloc()
Source Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Allocate memory for 10 integers
int *ptr = (int *) malloc(10 * sizeof(int));
ptr[0] = 1;
ptr[1] = 2;
ptr[2] = 3;
ptr[3] = 4;
ptr[4] = 5;
ptr[5] = 6;
ptr[6] = 7;
ptr[7] = 8;
ptr[8] = 9;
ptr[9] = 10;
// Reallocate memory for 15 integers
ptr = (int *) realloc(ptr, 15 * sizeof(int));
for (int i = 9; i < 15; i++)
{
ptr[i] = i + 1;
}
for (int i = 0; i < 15; i++)
printf("%d ", ptr[i]);
free(ptr);
return 0;
}
Expected Output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Actual Output :
Week 12
i) Write a recursive function to generate Fibonacci series.
Source Code:
#include<stdio.h>
void printFibonacci(int n)
{
static int n1=0,n2=1,n3;
if(n>0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
return 0;
}
Expected Output :
Enter the number of elements: 7
Fibonacci Series: 0 1 1 2 3 5 8
Actual Output :
ii) Write a recursive function to find the lcm of two numbers.
Source Code:
#include<stdio.h>
int find_lcm(int, int); // function prototype declaration
int main()
{
int a, b, lcm;
printf("\n\nEnter 2 integers to find LCM of:\n");
scanf("%d%d", &a, &b);
lcm = find_lcm(a,b); // function call
printf("\n\n LCM of %d and %d is: %d\n\n", a, b, lcm);
return 0;
}
int find_lcm(int a, int b) // function definition
{
static int temp = 1;
if(temp%a == 0 && temp%b == 0)
{
return temp;
}
else
{
temp++;
find_lcm(a,b);
return temp;
}
}
Expected Output :
Enter 2 integers to find LCM of:
12 25
LCM of 12 and 25 is: 300
Actual Output :
iv) Write a C Program to implement Ackermann function using recursion.
Source Code:
#include <stdio.h>
int ack(int m, int n)
{
if (m == 0)
{
return n+1;
}
else if((m > 0) && (n == 0))
{
return ack(m-1, 1);
}
else if((m > 0) && (n > 0))
{
return ack(m-1, ack(m, n-1));
}
}
int main()
{
int A;
A = ack(1, 2);
printf("%d", A);
return 0;
}
Expected Output :
4
Actual Output :
4
Week 14
Source Code:
#include<stdio.h>
#include<stdlib.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("\nContents of file is :\n");
printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));
fclose(fp);
return 0;
}
Expected Output :
Enter file name to create :a.txt
File created successfully.
Data written successfully.
Contents of file is :
ABC
Actual Output :
iii) 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 \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
Expected Output :
Enter the filename to open for reading
a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt
Actual Output :
Enter the filename to open for reading
a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt