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

C programming Practicals

The document provides a series of C programming exercises covering various topics such as I/O statements, decision-making constructs, loops, arrays, strings, functions, recursion, and pointers. Each exercise includes a program example demonstrating the specific concept, such as calculating the area of a circle, checking for leap years, and manipulating arrays. The document serves as a practical guide for learning fundamental C programming skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C programming Practicals

The document provides a series of C programming exercises covering various topics such as I/O statements, decision-making constructs, loops, arrays, strings, functions, recursion, and pointers. Each exercise includes a program example demonstrating the specific concept, such as calculating the area of a circle, checking for leap years, and manipulating arrays. The document serves as a practical guide for learning fundamental C programming skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

C programming Practical

Ex no: 1- I/O Statements, Operators and Expressions

Program:

#include <stdio.h>
int main() {
float rad, area, circum;
printf("\nEnter the radius of the circle: ");
scanf("%f", &rad);
area = 3.14 * rad * rad;
circum = 2 * 3.14 * rad;
printf("\nCircumference = %f", circum);
printf("\nArea = %f", area);
return 0;
}
Ex No: 2a- Decision making Constructs if-else (Leap Year or not)

Program:

#include <stdio.h>

int main() {
int y;
printf("Enter the year to check: ");
scanf("%d", &y);
if (y % 4 == 0)
printf("\nIt is a leap year");
else
printf("\nIt is not a leap year");
return 0;
}
Ex No: 2b- goto Statement (Tables)

Program:

#include <stdio.h>
int main() {
int count, t, r;

// Read value of t
printf("Enter number: ");
scanf("%d", &t);

count = 1;

start:
if (count <= 10) {
// Formula of table
r = t * count;
printf("%d * %d = %d\n", t, count, r);
count++;
goto start;
}

return 0;
}
Ex No: 2c- Switch case (Calculator)

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 the 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:
if (b != 0) {
c = a / b;
printf("Division: %d\n", c);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
case 0:
exit(0);
break;
default:
printf("Invalid operation code.\n");
}
}
Ex No:2d- break-continue Calculating Square Root of a Number

Program:

#include <stdio.h>
#include <math.h>
int main() {
int num;
do {
printf("\nEnter the number, Enter 999 to stop: ");
scanf("%d", &num);

if (num == 999)
break;

if (num < 0) {
printf("\nSquare root of a negative number is not defined");
continue;
}

printf("\nThe square root of %d is %f", num, sqrt(num));


} while (1);

return 0;
}
Ex No: 3a- For Loop Sum of first n natural numbers

Program:

#include <stdio.h>

int main() {
int num, count, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

for (count = 1; count <= num; ++count) {


sum += count;
}

printf("Sum = %d", sum);


return 0;
}
Ex No: 3b- While Loop Sum and Reverse of the given Number

Program:

#include <stdio.h>
int main() {
unsigned long int a, num, sum = 0, rnum = 0, rem;
printf("\nEnter the number: ");
scanf("%lu", &num);
a = num;
while (num != 0) {
rem = num % 10;
sum = sum + rem;
rnum = rnum * 10 + rem;
num = num / 10;
}
printf("\nThe sum of the digits of %lu is %lu\n", a, sum);
printf("\nThe reverse of the number %lu is %lu", a, rnum);
if (a == rnum)
printf("\nThe given number is a palindrome");
else
printf("\nThe given number is not a palindrome");
return 0;
}
Ex No: 3c- do- While Average of first n numbers

Program:

#include <stdio.h>

int main() {
int n, i = 1, sum = 0;
float avg = 0.0;

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


scanf("%d", &n);

do {
sum = sum + i;
i = i + 1;
} while (i <= n);

avg = (float)sum / n;

printf("\nThe sum of the first %d numbers = %d", n, sum);


printf("\nThe average of the first %d numbers = %f", n, avg);

return 0;
}
Ex No: 4a- Arrays 1D array ( Calculation Average of n numbers)

Program:

#include <stdio.h>
int main() {
int marks[10], i, n, sum = 0;
float average;
printf("Enter number of elements (1 to 10): ");
scanf("%d", &n);
if (n < 1 || n > 10) {
printf("Invalid number of elements. Please enter a number
between 1 and 10.\n");
return 1;
}
for (i = 0; i < n; ++i) {
printf("Enter number %d: ", i + 1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = (float)sum / n;
printf("Average = %.2f\n", average);

return 0;
}
Ex No: 4b- Arrays 2D Arrays ( Pascal Triangle)

Program:

#include <stdio.h>

int main() {
int arr[7][7] = {0};
int row = 2, col, i, j;

arr[0][0] = arr[1][0] = arr[1][1] = 1;

while (row < 7) {


arr[row][0] = 1;
for (col = 1; col <= row; col++)
arr[row][col] = arr[row - 1][col - 1] + arr[row - 1][col];
row++;
}
for (i = 0; i < 7; i++) {
printf("\n");
for (j = 0; j <= i; j++)
printf("\t%d", arr[i][j]);
}
return 0;
}
Ex No: 4c- Multi Dimensional Array

Program:

#include <stdio.h>
int main() {
int array[2][2][2], i, j, k;
printf("\nEnter the elements of the matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
for (k = 0; k < 2; k++) {
scanf("%d", &array[i][j][k]);
}
}
}
printf("\nThe matrix is:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
for (k = 0; k < 2; k++) {
printf("\tarray[%d][%d][%d] = %d\n", i, j, k, array[i][j][k]);
}
}
}
return 0;
}
Ex No: 4d- Traversal

Program:

int main() {
int i, n, arr[20], temp;
int small, small_pos;
int large, large_pos;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
if (n > 20 || n <= 0) {
printf("Invalid number of elements. Please enter a value between
1 and 20.\n");
return 1;
}
for (i = 0; i < n; i++) {
printf("\nEnter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
small = arr[0];
small_pos = 0;
large = arr[0];
large_pos = 0;
for (i = 1; i < n; i++) {
if (arr[i] < small) {
small = arr[i];
small_pos = i;
}
if (arr[i] > large) {
large = arr[i];
large_pos = i;
}
}
printf("\nThe smallest number is: %d", small);
printf("\nThe position of the smallest number in the array is: %d",
small_pos);
printf("\nThe largest number is: %d", large);
printf("\nThe position of the largest number in the array is: %d",
large_pos);

// Swap the smallest and largest numbers


temp = arr[large_pos];
arr[large_pos] = arr[small_pos];
arr[small_pos] = temp;
printf("\nThe new array is:");
for (i = 0; i < n; i++) {
printf("\n%d", arr[i]);
}
return 0;
}
Ex No: 5- Strings: Operations

Program:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str1[20], str2[20];
int ch, i, j;
do {
printf("\tMENU\n\n");
printf("1: Find Length of String\n");
printf("2: Find Reverse of String\n");
printf("3: Concatenate Strings\n");
printf("4: Copy String\n");
printf("5: Compare Strings\n");
printf("6: Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter String: ");
scanf("%s", str1);
i = strlen(str1);
printf("Length of String: %d\n\n", i);
break;
case 2:
printf("\nEnter String: ");
scanf("%s", str1);
strrev(str1); // Replace with manual reverse logic if strrev is
unavailable
printf("Reversed String: %s\n\n", str1);
break;
case 3:
printf("\nEnter First String: ");
scanf("%s", str1);
printf("Enter Second String: ");
scanf("%s", str2);
strcat(str1, str2);
printf("String After Concatenation: %s\n\n", str1);
break;
case 4:
printf("\nEnter a String1: ");
scanf("%s", str1);
printf("Enter a String2: ");
scanf("%s", str2);
printf("\nString Before Copy:\nString1=\"%s\",
String2=\"%s\"\n", str1, str2);
strcpy(str2, str1);
printf("\nString After Copy:\nString1=\"%s\",
String2=\"%s\"\n\n", str1, str2);
break;
case 5:
printf("\nEnter First String: ");
scanf("%s", str1);
printf("Enter Second String: ");
scanf("%s", str2);
j = strcmp(str1, str2);
if (j == 0) {
printf("Strings are Same\n\n");
} else {
printf("Strings are Not Same\n\n");
}
break;
case 6:
exit(0);

default:
printf("Invalid Input. Please Enter a valid Input.\n\n");
}
} while (ch != 6);

return 0;
}
Ex No: 6a- Functions- Call and Return

Program:

#include <stdio.h>

// Function prototype
int calsum(int x, int y, int z);

int main() {
int a, b, c, sum;

printf("\nEnter any three numbers: ");


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

sum = calsum(a, b, c);


printf("\nSum = %d\n", sum);
return 0;
}
// Function definition
int calsum(int x, int y, int z) {
int d;
d = x + y + z;
return d;
}
Ex No: 6b- Functions Call by Value and Call by Reference

Program:

#include <stdio.h>

// Function prototypes
void swap_call_by_val(int, int);
void swap_call_by_ref(int *, int *);

int main() {
int a = 1, b = 2, c = 3, d = 4;

printf("\nBefore call by value: a = %d, b = %d", a, b);


swap_call_by_val(a, b);

printf("\nAfter call by value: a = %d, b = %d", a, b);

printf("\n\nBefore call by reference: c = %d, d = %d", c, d);


swap_call_by_ref(&c, &d);

printf("\nAfter call by reference: c = %d, d = %d\n", c, d);

return 0;
}
void swap_call_by_val(int a, int b) {

int temp;
temp = a;
a = b;
b = temp;
printf("\nIn function (call by value method): a = %d, b = %d", a, b);
}

void swap_call_by_ref(int *c, int *d)


{

int temp;
temp = *c;
*c = *d;
*d = temp;
printf("\nIn function (call by reference method): c = %d, d = %d",
*c, *d);
}
Ex No: 6c Functions Passing arrays to function

Program:

#include <stdio.h>

void disp(int *num) {


printf("%d ", *num);
}

int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

for (int i = 0; i < 10; i++) {


disp(&arr[i]);
}

printf("\n");

return 0;
}
Ex No: 7 Recursion Fibonacci Series Generation By Recursion

Program:

#include <stdio.h>

// Function prototype
void fib(int n);

int main()
{
int n;

printf("Number of terms to generate? ");


scanf("%d", &n);

if (n <= 0)
{
printf("Please enter a positive integer.\n");
return 1;
}

printf("\n\nFibonacci Sequence up to %d terms:\n\n", n);


fib(n);
printf("\n");
return 0;
}

void fib(int n) {
static int f1 = 0, f2 = 1;
int temp;

if (n < 2)
{
f1 = 0;
f2 = 1;
}
else
{
fib(n - 1);
temp = f2;
f2 = f1 + f2;
f1 = temp;
}

printf("%5d", f1);
return;
}
Ex No: 8a- Pointers Pointers to Functions

Program:

#include <stdio.h>

void salaryhike(int *var, int b) {


*var = *var + b;
}

int main() {
int salary = 0, bonus = 0;

printf("Enter the employee's current salary: ");


scanf("%d", &salary);

printf("Enter bonus: ");


scanf("%d", &bonus);

salaryhike(&salary, bonus);

printf("Final salary: %d\n", salary);

return 0;
}
Ex No: 8c- Pointers to Arrays

Program:

#include <stdio.h>

int main()
{
int *p;
int val[7] = {11, 23, 33, 44, 55, 66, 77};
p = &val[0];

for (int i = 0; i < 7; i++)


{
printf("val[%d]: value is %d and address is %p\n", i, *p, (void *)p);
p++;
}

return 0;
}
Ex No: 8c- Pointer to String

Program:

#include<stdio.h>

int main() {
char str[100], *ps;
int nword = 0;

printf("\nEnter the String: ");


gets(str);
ps = str;
while (*ps == 32 && *ps != '\0')
ps++;
if (*ps != '\0')
nword++;
for (ps = str; *ps != '\0'; ps++) {
if (*ps != 32 && *(ps - 1) == 32)
nword++;
}
printf("%d word(s) in the given string", nword);

return 0;
}
Ex NO: 8d- Pointers to Pointers

Program:

#include <stdio.h>

int main() {
int a = 10;
int *p1;
int **p2;

p1 = &a;
p2 = &p1;

printf("Address of a = %u\n", &a);


printf("Address of p1 = %u\n", &p1);
printf("Address of p2 = %u\n\n", &p2);
printf("Value at the address stored by p2 = %u\n", *p2);
printf("Value at the address stored by p1 = %d\n\n", *p1);
printf("Value of **p2 = %d\n", **p2);

return 0;
}
Ex NO:9a- Structures Nested Structures

Program

#include<stdio.h>

int main()
{

struct DOB
{

int day;
int month;
int year;
};

struct student
{

int roll_no;
char name[100];
float fees;
struct DOB date;
};

struct student stud1;


printf("\nEnter the roll no: ");
scanf("%d", &stud1.roll_no);

printf("\nEnter the name: ");


scanf("%s", stud1.name);

printf("\nEnter the fees: ");


scanf("%f", &stud1.fees);

printf("\nEnter the DOB (day month year): ");


scanf("%d%d%d", &stud1.date.day, &stud1.date.month,
&stud1.date.year);

printf("\n** Student's Details **");


printf("\nRoll no = %d", stud1.roll_no);
printf("\nName = %s", stud1.name);
printf("\nFees = %.2f", stud1.fees);
printf("\nDOB = %d-%d-%d", stud1.date.day, stud1.date.month,
stud1.date.year);

return 0;
}
Ex NO: 9b Arrays of Structures

Program:

#include<stdio.h>

int main() {
struct student {
int rollno;
char name[80];
int fees;
char dob[80];
};

struct student stud[50];


int n, i;

printf("\nEnter the number of students: ");


scanf("%d", &n);

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


printf("\nEnter the roll number: ");
scanf("%d", &stud[i].rollno);

printf("\nEnter the name: ");


scanf("%s", stud[i].name);

printf("\nEnter the fees: ");


scanf("%d", &stud[i].fees);

printf("\nEnter the dob (dd-mm-yyyy): ");


scanf("%s", stud[i].dob);
}

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


printf("\n**Details of student %d**", i + 1);
printf("\nRoll no = %d", stud[i].rollno);
printf("\nName = %s", stud[i].name);
printf("\nFees = %d", stud[i].fees);
printf("\nDOB = %s", stud[i].dob);
}

return 0;
}
Ex No: 9c Unions

Program:

#include <stdio.h>
#include <string.h>

union Data {
int i;
float f;
char str[20];
};

int main() {
union Data data;

data.i = 10;
printf("data.i : %d\n", data.i);
data.f = 220.5;
printf("data.f : %f\n", data.f);
strcpy(data.str, "C Programming");
printf("data.str : %s\n", data.str);

return 0;
}
Ex No: 9d- Pointers to Structures

Program:

#include<stdio.h>

struct student {
int sno;
char sname[30];
float marks;
};

int main() {
struct student s;
struct student *st;
printf("Enter sno, sname, marks: ");
scanf("%d %s %f", &s.sno, s.sname, &s.marks);
st = &s;
printf("Details of the student are:\n");
printf("Number = %d\n", st->sno);
printf("Name = %s\n", st->sname);
printf("Marks = %f\n", st->marks);

return 0;
}

You might also like