LIST OF EXPERIMENTS
1. I/O statements, operators, expressions
2. decision-making constructs: if-else, goto, switch-case, break-continue
3. Loops: for, while, do-while
4. Arrays: 1D and 2D, Multi-dimensional arrays, traversal
5. Strings: operations
6. Functions: call, return, passing parameters by (value, reference), passing
arrays to function.
7. Recursion
8. Pointers: Pointers to functions, Arrays,Strings, Pointers to Pointers, Array of
Pointers
9. Structures: Nested Structures, Pointers to Structures, Arrays of Structures and
Unions.
10. Files: reading and writing, File pointers, file operations, random access,
processor directives.
PROGRAMMING IN C LABORATORY [CS3271]
Exp1.a)Write a C program to compute the area and perimeter
Program:
#include<stdio.h>
#define PI 3.14
int main()
{
float radius,area,perimeter;
printf("Enter radius of the circle: ");
scanf("%f",&radius);
area=PI*radius*radius;
perimeter=2*PI*radius;
printf("Area of the circle: %f \n,Perimeter of the circle: %f\n",area,perimeter);
return 0;
}
Output:
Exp1.b) Write a C program to convert the Celsius value into Fahrenheit.
Program:
#include<stdio.h>
main()
{
float c,f;
printf("Enter the Celsius value: ");
scanf("%f",&c);
f=(1.8*c)+32;
printf("The Fahrenheit value of the given %f Celsius
value is %f",c,f);
}
Output:
Exp1.C)Write a C program to calculate the bill amount for an item given its quantity sold,
value, discount, and tax.
Program:
#include<stdio.h>
int main()
{
float total_amt,amount,sub_total,discount_amt,
tax_amt,quantity,value,discount,tax;
printf("\n Enter the quantity of item sold: ");
scanf("%f",&quantity);
printf("\n Enter the value of item: ");
scanf("%f",&value);
printf("\n Enter the discount percentage: ");
scanf("%f",&discount);
printf("\n Enter the tax: ");
scanf("%f",&tax);
amount = quantity*value;
discount_amt = (amount*discount)/100.0;
sub_total = amount - discount_amt;
tax_amt = (sub_total*tax)/100.0;
total_amt = sub_total + tax_amt;
printf("\n\n\n******BILL******");
printf("\n Quantity sold: %f",quantity);
printf("\n Price per item: %f",value);
printf("\n Amount: %f",amount);
printf("\n Discount: %f",discount_amt);
printf("\n Discounted total: %f",sub_total);
printf("\n Tax: +%f",tax_amt);
printf("\n-----------------------------");
printf("\n Total Amount: %f",total_amt);
printf("\n-----------------------------");
return 0;
}
Output:
Exp 1. d.Write a C program to find the roots of a quadratic equation.
Program:
#include <math.h>
#include <stdio.h>
int main()
{
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// Condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Root 1 = %.2lf and Root 2 = %.2lf\n", root1, root2);
}
// Condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("Root 1 = Root 2 = %.2lf\n", root1);
}
// If roots are not real
else
{
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("Root 1 = %.2lf + %.2lfi and Root 2 = %.2lf - %.2lfi\n",
realPart, imagPart, realPart, imagPart);
}
return 0;
}
Output:
Exp 2. a)Write a C program to find the largest of three numbers.
Program:
#include<stdio.h>
int main()
{
int a,b,c;
int big;
printf("Enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
big=a;
else if(b>a && b>c)
big=b;
else
big=c;
printf("Largest numbers is = %d",big);
return 0;
}
Output:
Exp 2.b) Write a C program to check whether a person is Eligible to vote or not.
Program:
#include<stdio.h>
void main()
{
int age;
char name[50];
printf("\n Enter the name of the candidate: ");
scanf(“%s”,&name);
printf("\n Enter the age: ");
scanf("%d",&age);
if(age>=18)
printf("\n %s is Eligible for Vote",name);
else
printf("\n %s is Not Eligible for Vote",name \n);
}
Output:
Exp 2.c) Write a C program to check whether a year is leap year or not.
Program:
#include<stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if(year%100 == 0)
{
if(year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year);
}
else
printf("%d is not a leap year.", year);
}
Output:
Exp 2.d) Write a ‘c’ program to check whether a number is oddor even. (using go to
statement).
Program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int n;
printf("Enter a number\n");
scanf("%d", &n);
if (n%2 == 0)
goto even;
else
goto odd;
even:
printf("%d is even\n", n);
exit(0);
odd:
printf("%d is odd\n", n);
}
Output:
Exp 2.f) Write a c program to print first N natural numbers (using goto statement).
Program:
#include<stdio.h>
int main()
{
int n,i=1;
printf("Enter a number: ");
scanf("%d",&n);
start:
printf("%d\t",i);
i++;
if(i<n)
goto start;
return 0;
}
Output:
Exp 2.g) Write a simple menu driven calculator program in C (using switch statement)
Program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a,b,c,n;
printf("-----MENU-----\n");
printf("1 ADDITION\n");
printf("2 SUBTRACTION\n");
printf("3 MULTIPLICATION\n");
printf("4 DIVISION\n");
printf("0 EXIT\n");
printf("Enter your choice: ");
scanf("%d",&n);
if(n<=4&n>0)
{
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
}
switch(n)
{
case 1:
c=a+b;
printf("Addition: %d\n",c);
break;
case 2:
c=a-b;
printf("Subtraction: %d\n",c);
break;
case 3:
c=a*b;
printf("Multiplication: %d\n",c);
break;
case 4:
c=a/b;
printf("Division: %d\n",c);
break;
case 0:
exit(0);
break;
default:
printf("Invalid operation code");
}
}
Output:
Exp 2.h) Write a C program to check whether the entered character is vowel or not (Use
switch case).
Program:
#include <stdio.h>
int main()
{
char ch;
/* Input an alphabet from user */
printf("Enter any alphabet: ");
scanf("%c", &ch);
/* Switch value of ch */
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
return 0;
}
Output:
Exp 3.a)Write a C’ program to find the factorial of the given number. (using for loop)
Program:
#include <stdio.h>
int main()
{
int fact=1,i,n;
printf("Enter a number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The facctorial of %d is %d",n,fact);
return 0;
}
Output:
Exp 3.b)Write a C program to generate the Fibonacci series.
Program:
#include<stdio.h>
int main()
{
int n,c=0,a=0,b=1,i;
printf("Enter the number");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
if(n==0)
printf("0");
else
{
for(i=0;i<n;i++)
{
a=b;
b=c;
c=a+b;
printf("%d\t",c);
}
}}
Output:
Exp 3.a) Write a C program to check whether a number is Palindrome or not.
Program:
#include <stdio.h>
int main() {
int num, reversed = 0, remainder, temp;
printf("Enter an integer: ");
scanf("%d", &num);
temp = num;
// reversed integer is stored in reversed variable
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num/= 10;
}
// palindrome if orignal and reversed are equal
if (temp == reversed)
printf("%d is a palindrome.", temp);
else
printf("%d is not a palindrome.", temp);
return 0;
}
Output:
Exp 3.c)Write a C program to check whether a number is Armstrong or not.
Program:
#include<stdio.h>
void main()
{
int n,OriginalNumber,r,result = 0;
printf("Enter a three digit integer: ");
scanf("%d",&n);
OriginalNumber = n;
while(OriginalNumber!=0)
{
r = OriginalNumber%10;
result+=r*r*r;
OriginalNumber/=10;
}
if(result == n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
}
Output:
Exp 3.d)Write a C program to reverse a number.
Program:
#include<stdio.h>
int main()
{
int n,reverse=0,r;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
r=n%10;
reverse=reverse*10+r;
n/=10;
}
printf("The Reversed Number = %d",reverse);
return 0;
}
Output:
Exp 3.e) Write a ‘c’ program to display a half pyramid of numbers in pattern.
Program
#include<stdio.h>
int main()
{
int i,j,row;
printf("Enter the numbers of rows: ");
scanf("%d", &row);
for(i=1;i<=row;++i)
{
for(j=1;j<=i;++j)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
Output:
Exp 3.f) Write a C program to check whether a number is Positive, Negative or Zero until the
user doesn’t want to exit.(using do-while)
Program:
#include<stdio.h>
int main()
{
int n;
char choice;
do
{
printf("Enter an integer number: ");
scanf("%d", &n);
if(n == 0)
printf("Zero.");
else if (n>0)
printf("Positive Number.");
else
printf("Negative Number.");
printf("\n\nWanna check again? (press y for 'yes') :");
fflush(stdin); //to clear the input buffer
scanf(" %c", &choice);
}
while(choice == 'y');
printf("\nBye!");
return 0;}
Output:
Exp.No:3 g. Write a C program to print positive integers from 1 to 10.
Program:
#include<stdio.h>
void main()
{
int i; //Variable definition
printf("The first 10 natural numbers are:\n ");
for (i = 1; i <= 10; i++) //Iteration 10 times
{
printf("%d \t", i); //Print the number.
}
}
Output:
Ex.No: 4.a) Write a C program to insert 5 elements into an array and print the elements of
the array.
Program:
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
Output:
Ex.No: 4.b.Write a C Program to reverse the array elements.
Program:
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {1, 2, 3, 4, 5};
/Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Array in reverse order: \n");
//Loop through the array in reverse order
for (int i = length-1; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Ex.No: 4.C) Write a C program to find out the average height of persons.
Program
#include <stdio.h>
void main()
{
int i,n,sum=0,count=0,height[100];
float avg;
//Read Number of persons
printf("Enter the Number of Persons : ");
scanf("%d",&n);
//Read the height of n persons
printf("\nEnter the Height of each person in centimeter\n");
for(i=0; i<n; i++)
{
scanf("%d", &height[i]);
sum = sum + height[i];
}
avg = (float)sum/n;
//Counting
for(i=0;i<n;i++)
{
if(height[i]>avg)
count++;
}
//display
printf("\nAverage Height of %d persons is : %.2f\n",n,avg);
printf("\nThe number of persons above average : %d ",count);
}
Output
Ex.No: 5.a) Write a C program to find the length of String.
Program:
#include <stdio.h>
int main()
{
char s[1000];
int i;
printf("Enter a string: ");
scanf("%s", s);
for(i = 0; s[i] != '\0'; ++i);
printf("Length of string: %d", i);
return 0;
}
Output:
Ex.No:5.b) Write a C program to concatenate two strings.
Program:
#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;
printf("Enter first string: ");
scanf("%s", s1);
printf("Enter second string: ");
scanf("%s", s2);
// calculate the length of string s1 and store it in i
for(i = 0; s1[i] != '\0'; ++i);
for(j = 0; s2[j] != '\0'; ++j, ++i)
{
s1[i] = s2[j];
}
s1[i] = '\0';
printf("After concatenation: %s", s1);
return 0;
}
Output
Exp.No:5.c) Find the length of the string using the C program.
1. Using Library Function.
2. Without using Library Function.
a.Using Library Function
Program:
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Programming is fun";
int length = strlen(s);
printf("Length of the string: %d\n", length);
return 0;
}
Output:
b.Without using Library Function.
Program:
#include <stdio.h>
int main()
{
char s[] = "Programming is fun";
int i;
for (i = 0; s[i] != '\0'; ++i)
; // Empty loop body
printf("Length of the string: %d\n", i);
return 0;
}
Output:
Exp.No: 5.d) Write C program to count the number of lines, words and characters in a given
text.
Program:
#include <stdio.h>
int main()
{
char input[100]; // Character array to store input (up to 100 characters)
printf("Enter a string: ");
scanf("%[^\n]", input); // Read the entire line (including spaces)
int words = 0, lines = 1, characters = 0; // Initialize counters
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] == ' ') {
words++; // Increment word count for each space
} else if (input[i] == '\n') {
lines++; // Increment line count for each newline
}
characters++; // Increment character count for each character
}
// Handle corner cases
if (characters > 1) {
lines++; // Increment line count for the first line
words++; // Increment word count for the last word
}
printf("Total number of words: %d\n", words);
printf("Total number of lines: %d\n", lines);
printf("Total number of characters: %d\n", characters);
return 0;
}
Output:
Exp.No: 5.e) Write a C program to compare two strings using pointers.
Program:
#include <stdio.h>
int main()
{
//Compare Two strigs using Pointers
char string1[50],string2[50],*str1,*str2;
int i,equal = 0;
printf("Enter The First String: ");
scanf("%s",string1);
printf("Enter The Second String: ");
scanf("%s",string2);
str1 = string1;
str2 = string2;
while(*str1 == *str2)
{
if ( *str1 == '\0' || *str2 == '\0' )
break;
str1++;
str2++;
}
if( *str1 == '\0' && *str2 == '\0' )
printf("\n\n Strings Are Equal.");
else
printf("\n\n Strings Are Not Equal.");
}
Output:
Ex.No:6.a) Write a C program to perform swapping using function.
Program:
#include<stdio.h>
void swap(int,int);
void main()
{
int a,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
}
Output:
Exp.No.6.b) Display all prime numbers between two intervals using the C program(Use
Functions).
Program:
#include <stdio.h>
// Function to check if a number is prime
int checkPrime(int n) {
int i;
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
return 0; // Not a prime number
}
}
return 1; // Prime number
}
int main() {
int lowerlimit, upperlimit, i;
printf("Enter two positive integers : ");
scanf("%d %d", &lowerlimit, &upperlimit);
printf("Prime numbers between %d and %d are: ", lowerlimit, upperlimit);
for (i = lowerlimit; i <= upperlimit; ++i) {
if (checkPrime(i)) {
printf("%d ", i);
}
}
return 0;
}
Output:
Exp:No:7.a.Write a C program to reverse a sentence using recursion.
Program:
#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence() {
char c;
scanf("%c", &c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
}
Output:
Exp:No:7.b) Write a C program to get the user details and display it.
Program:
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];
int main()
{
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Mar.ks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
Output:
Exp:No:7 C.Write a C program to find power of any number using recursion.
Program:
#include <stdio.h>
long power (int, int);
int main()
{
int exp, base;
long value;
printf("Enter The Number Base Number: ");
scanf("%d", &base);
printf("Enter The Exponent: ");
scanf("%d", &exp);
value = power(base, exp);
printf("%d^%d is %ld", base, exp, value);
return 0;
}
long power (int base, int exp)
{
if (exp)
{
return (base * power(base, exp - 1));
}
return 1;
}
Output:
Ex:No:8.a).Write a C program to find the length of the string using Pointer.
Program :
#include <stdio.h>
// Function to find the length of a string using pointers
int string_length(char* given_string) {
int length = 0;
while (*given_string != '\0') {
length++;
given_string++;
}
return length;
}
int main() {
char given_string[] = "GeeksforGeeks";
printf("Length of the String: %d", string_length(given_string));
return 0;
}
Output:
Exp:No:8.b).Write a C program that displays the position or index in the main string S where
the sub string T begins, or - 1 if S doesn't contain T.
Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30], t[20];
char *found;
clrscr();
puts("Enter the first string: ");
gets(s);
puts("Enter the string to be searched: ");
gets(t);
found = strstr(s, t);
if(found)
{
printf("Second String is found in the First String at %d position.\n", found - s);
}
else
{
printf("-1");
}
getch();
}
Output:
Exp 9.a.Write a C program to create, declare and initialize structure
Program
#include <stdio.h>
/*structure declaration*/
struct employee
{
char name[30];
int empId;
float salary;
};
int main()
{
/*declare and initialization of
structure variable*/
struct employee emp = { "Mike", 1120, 76909.00f };
printf("\n Name: %s", emp.name);
printf("\n Id: %d", emp.empId);
printf("\n Salary: %f\n", emp.salary);
return 0;
}
Output:
Exp 9.b. To Write a declare, initialize an UNION.
Program
// C Program to demonstrate how to use union
#include <stdio.h>
// union template or declaration
union un
{
int member1;
char member2;
float member3;
};
// driver code
int main()
{
// defining a union variable
union un var1;
// initializing the union member
var1.member1 = 15;
printf("The value stored in member1 = %d",
var1.member1);
return 0;
}
Output
Exp No:10.a).Write a C program to copy the contents of one file to another.
Program
#include <stdio.h>
#include <stdlib.h>
// For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
// Get the source filename from the user
printf("Enter the filename to open for reading:\n");
scanf("%s", filename);
// Open the source file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL) {
printf("Cannot open file %s\n", filename);
exit(0);
}
// Get the destination filename from the user
printf("Enter the filename to open for writing:\n");
scanf("%s", filename);
// Open the destination file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s\n", filename);
exit(0);
}
// Read contents from the source file and write to the destination file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s\n", filename);
// Close both files
fclose(fptr1);
fclose(fptr2);
return 0;
}
Output
Exp No:10.b).Write a C program to merge two files into a third file.
Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
FILE *fp3 = fopen("file3.txt", "w");
char c;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
// Copy characters from file1.txt to file3.txt
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
// Copy characters from file2.txt 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:
Ex No:10.C)Write a C program to list all files and sub-directories in a directory
Program
#include <stdio.h>
#include <dirent.h>
int main(void){
struct dirent *files;
DIR *dir = opendir(".");
if (dir == NULL){
printf("Directory cannot be opened!" );
return 0;
}
while ((files = readdir(dir)) != NULL)
printf("%s
", files->d_name);
closedir(dir);
return 0;
}
Output:
..
prog1.c
prog2.c
prog3.c
...
prog41.c
This will return all files and sub-directory of the current directory.