C programming Practicals
C programming Practicals
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);
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;
}
return 0;
}
Ex No: 3a- For Loop Sum of first n natural numbers
Program:
#include <stdio.h>
int main() {
int num, count, sum = 0;
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;
do {
sum = sum + i;
i = i + 1;
} while (i <= n);
avg = (float)sum / n;
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;
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);
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;
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;
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);
}
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>
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
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;
if (n <= 0)
{
printf("Please enter a positive integer.\n");
return 1;
}
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>
int main() {
int salary = 0, bonus = 0;
salaryhike(&salary, bonus);
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];
return 0;
}
Ex No: 8c- Pointer to String
Program:
#include<stdio.h>
int main() {
char str[100], *ps;
int nword = 0;
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;
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;
};
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];
};
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;
}