0% found this document useful (0 votes)
2 views26 pages

c programming for omputer science students

The document contains a series of C programming exercises and their corresponding code implementations, covering various topics such as matrix operations, string manipulation, number theory, sorting algorithms, and data structures. Each exercise includes the code, a brief description, and sample output. The exercises are aimed at enhancing programming skills in C for students in the School of Computing at the VET Institute of Arts and Science.

Uploaded by

gowrikcom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views26 pages

c programming for omputer science students

The document contains a series of C programming exercises and their corresponding code implementations, covering various topics such as matrix operations, string manipulation, number theory, sorting algorithms, and data structures. Each exercise includes the code, a brief description, and sample output. The exercises are aimed at enhancing programming skills in C for students in the School of Computing at the VET Institute of Arts and Science.

Uploaded by

gowrikcom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

VET INSTITUTE OF ARTS AND SCIENCE

SCHOOL OF COMPUTING
ODD SEM 2025-26
C LAB PROGRAMS
1. Matrix Addition

#include <stdio.h>
int main() {
int a[2][2] = {{1,2},{3,4}}, b[2][2] = {{5,6},{7,8}}, c[2][2];
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
c[i][j] = a[i][j] + b[i][j];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}

Output:

68
10 12

2. Reverse a String

#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello";
int len = strlen(str);
for(int i=len-1;i>=0;i--)
printf("%c", str[i]);
printf("\n");
return 0;
}

Output:

olleh

3. Palindrome Check

#include <stdio.h>
#include <string.h>
int main() {
char str[] = "madam";
int i, flag = 1, len = strlen(str);
for(i = 0; i < len / 2; i++) {
if(str[i] != str[len - i - 1]) {
flag = 0;
break;
}
}
if(flag) printf("Palindrome\n");
else printf("Not Palindrome\n");
return 0;
}

Output:

Palindrome

4. Armstrong Number
#include <stdio.h>
int main() {
int n = 153, sum = 0, temp = n;
while(n > 0) {
int d = n % 10;
sum += d*d*d;
n /= 10;
}
if(temp == sum) printf("Armstrong\n");
else printf("Not Armstrong\n");
return 0;
}

Output:

Armstrong

5. Binary to Decimal

#include <stdio.h>
int main() {
int b = 1101, d = 0, base = 1;
while(b > 0) {
int last = b % 10;
d += last * base;
base *= 2;
b /= 10;
}
printf("%d\n", d);
return 0;
}

6.Bubble Sort

int main() {
int a[] = {5, 2, 9, 1, 5, 6}, n = 6;
for(int i=0; i<n-1; i++) {
for(int j=0; j<n-i-1; j++) {
if(a[j] > a[j+1]) {
int t = a[j]; a[j] = a[j+1]; a[j+1] = t;
}
}
}
for(int i=0; i<n; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}

7.Factorial Using Recursion

int factorial(int n) {
if(n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int n = 5;
printf("%d\n", factorial(n));
return 0;
}

8.Prime Number Check

int main() {
int n = 29, flag = 1;
for(int i = 2; i <= sqrt(n); i++) {
if(n % i == 0) {
flag = 0;
break;
}
}
if(flag) printf("Prime\n");
else printf("Not Prime\n");
return 0;
}

9.Fibonacci Series

int main() {
int n = 10, a = 0, b = 1, c;
for(int i = 0; i < n; i++) {
printf("%d ", a);
c = a + b;
a = b;
b = c;
}
printf("\n");
return 0;
}

10.Find GCD

int gcd(int a, int b) {


if(b == 0) return a;
return gcd(b, a % b);
}
int main() {
int a = 56, b = 98;
printf("%d\n", gcd(a, b));
return 0;
}

11.Print 1 to 10 using for loop

int main() {
for(int i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}

12.Sum of first N natural numbers using while loop

int main() {
int n = 10, sum = 0, i = 1;
while(i <= n) {
sum += i;
i++;
}
printf("%d\n", sum);
return 0;
}

13.Print multiplication table of a number using do-while loop

int main() {
int n = 5, i = 1;
do {
printf("%d x %d = %d\n", n, i, n*i);
i++;
} while(i <= 10);
return 0;
}

14.Print even numbers from 1 to 20 using for loop

int main() {
for(int i = 1; i <= 20; i++) {
if(i % 2 == 0) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
15. Calculate factorial using for loop
int main5() {
int n = 5, fact = 1;
for(int i = 1; i <= n; i++) {
fact *= i;
}
printf("%d\n", fact);
return 0;
}

16.Right-Angled Triangle (Stars)


#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}

Output:

*
**
***
****
*****

17.Inverted Triangle
#include <stdio.h>
int main() {
int i, j;
for(i = 5; i >= 1; i--) {
for(j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}

Output:

*****
****
***
**
*

18.Right-Angled Number Triangle


#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++)
printf("%d ", j);
printf("\n");
}
return 0;
}

Output:

1
12
123
1234
12345
19.Inverted Number Triangle

#include <stdio.h>
int main() {
int i, j;
for(i = 5; i >= 1; i--) {
for(j = 1; j <= i; j++)
printf("%d ", j);
printf("\n");
}
return 0;
}

Output:

12345
1234
123
12
1

20.Pyramid Pattern
#include <stdio.h>
int main() {
int i, j, space;
for(i = 1; i <= 5; i++) {
for(space = 1; space <= 5 - i; space++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}

Output:

*
***
*****
*******
*********

21.Inverted Pyramid Pattern


#include <stdio.h>
int main() {
int i, j, space;
for(i = 5; i >= 1; i--) {
for(space = 1; space <= 5 - i; space++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}

Output:

*********
*******
*****
***
*
22.Diamond Pattern

#include <stdio.h>
int main() {
int i, j, space;
for(i = 1; i <= 5; i++) {
for(space = 1; space <= 5 - i; space++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
for(i = 4; i >= 1; i--) {
for(space = 1; space <= 5 - i; space++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}

Output:

*
***
*****
*******
*********
*******
*****
***
*
23.Binary Number Triangle

#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++)
printf("%d ", (i + j) % 2);
printf("\n");
}
return 0;
}

Output:

0
10
010
1010
01010

24.Hollow Square

#include <stdio.h>
int main() {
int i, j, n = 5;
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
if(i == 1 || i == n || j == 1 || j == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}

Output:

*****
* *
* *
* *
*****

25.Floyd’s Triangle

#include <stdio.h>
int main() {
int i, j, k = 1;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++)
printf("%d ", k++);
printf("\n");
}
return 0;
}

Output:

1
23
456
7 8 9 10
11 12 13 14 15
26.C program that takes a user's name as input and displays each character in a square
pattern using that character.

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

int main() {
char name[100];
printf("Enter your name: ");
scanf("%s", name); // Reads string without spaces

int len = strlen(name);

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


for (int j = 0; j < len; j++) {
printf("%c", name[i]);
}
printf("\n");
}

return 0;
}
27.Student Record Using Struct
#include <stdio.h>

struct Student {
char name[50];
int age;
char grade;
};

int main() {
struct Student student = {"Alice", 21, 'A'};
printf("Name: %s, Age: %d, Grade: %c\n", student.name, student.age, student.grade);
return 0;
}
OUTPUT
Name: Alice, Age: 21, Grade: A

28.Linked List Implementation


#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node *next;
};

int main() {
struct Node *head = NULL;
struct Node *second = NULL;
struct Node *third = NULL;

head = (struct Node *)malloc(sizeof(struct Node));


second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));

head->data = 5;
head->next = second;

second->data = 10;
second->next = third;

third->data = 15;
third->next = NULL;

struct Node *temp = head;


while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
free(head);
free(second);
free(third);
return 0;
}

OUTPUT
5 -> 10 -> 15 -> NULL

28.Matrix Multiplication
#include <stdio.h>

#define SIZE 2

int main() {
int A[SIZE][SIZE] = {{1, 2}, {3, 4}};
int B[SIZE][SIZE] = {{5, 6}, {7, 8}};
int result[SIZE][SIZE] = {0};

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


for (int j = 0; j < SIZE; j++) {
for (int k = 0; k < SIZE; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}

printf("Result Matrix:\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;
}
OUTPUT
Result Matrix:
19 22
43 50

29.Stack Implementation Using Array


#include <stdio.h>

#define MAX 10

struct Stack {
int arr[MAX];
int top;
};

int main() {
struct Stack stack;
stack.top = -1;

stack.arr[++stack.top] = 10;
stack.arr[++stack.top] = 20;
printf("Popped Element = %d\n", stack.arr[stack.top--]);

return 0;
}

OUTPUT
Popped Element = 20

30.Quick Sort Algorithm


#include <stdio.h>

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

int temp = arr[i + 1];


arr[i + 1] = arr[high];
arr[high] = temp;

quickSort(arr, low, i);


quickSort(arr, i + 2, high);
}
}

int main() {
int arr[] = {5, 3, 8, 1, 4};
int size = sizeof(arr) / sizeof(arr[0]);

quickSort(arr, 0, size - 1);

printf("Sorted Array: ");


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

OUTPUT
Sorted Array: 1 3 4 5 8
31.Merge Two Sorted Arrays
#include <stdio.h>

#define SIZE_A 3
#define SIZE_B 3

int main() {
int A[SIZE_A] = {1, 3, 5};
int B[SIZE_B] = {2, 4, 6};
int merged[SIZE_A + SIZE_B];
int i = 0, j = 0, k = 0;

while (i < SIZE_A && j < SIZE_B) {


if (A[i] < B[j]) {
merged[k++] = A[i++];
} else {
merged[k++] = B[j++];
}
}

while (i < SIZE_A) {


merged[k++] = A[i++];
}

while (j < SIZE_B) {


merged[k++] = B[j++];
}

printf("Merged Array: ");


for (int i = 0; i < SIZE_A + SIZE_B; i++) {
printf("%d ", merged[i]);
}
printf("\n");

return 0;
}
OUTPUT
Merged Array: 1 2 3 4 5 6

32.Count Number of Digits


#include <stdio.h>
int main() {
int num, count = 0;
scanf("%d", &num);
while (num != 0) {
num /= 10;
count++;
}
printf("Digits: %d\n", count);
return 0;
}

OUTPUT
12345
Digits: 5

33.Print ASCII Value


#include <stdio.h>
int main() {
char ch;
scanf("%c", &ch);
printf("ASCII: %d\n", ch);
return 0;
}

OUTPUT
A
ASCII: 65

34.Power of a Number
#include <stdio.h>
int main() {
int base, exp, result = 1;
scanf("%d %d", &base, &exp);
for (int i = 1; i <= exp; i++) result *= base;
printf("%d\n", result);
return 0;
}

OUTPUT
23
8

35.Reverse a Number
#include <stdio.h>
int main() {
int num, rev = 0;
scanf("%d", &num);
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
printf("Reversed: %d\n", rev);
return 0;
}

OUTPUT
1234
Reversed: 4321

36.Sum of Even Numbers up to N


#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
for (int i = 2; i <= n; i += 2) sum += i;
printf("Sum: %d\n", sum);
return 0;
}
OUTPUT
10
Sum: 30

37.Check if Number is Perfect


#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
for (int i = 1; i < n; i++)
if (n % i == 0) sum += i;
if (sum == n)
printf("Perfect number\n");
else
printf("Not a perfect number\n");
return 0;
}

OUTPUT
6
Perfect number

38.Sum of Squares of First N Numbers


#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
sum += i * i;
printf("Sum: %d\n", sum);
return 0;
}

OUTPUT
3
Sum: 14
39.Convert Binary to Decimal
#include <stdio.h>
int main() {
int binary, decimal = 0, base = 1, rem;
scanf("%d", &binary);
while (binary) {
rem = binary % 10;
decimal += rem * base;
binary /= 10;
base *= 2;
}
printf("Decimal: %d\n", decimal);
return 0;
}

OUTPUT
1010
Decimal: 10

40.Check Whether a Character is Alphabet, Digit or Special Character


#include <stdio.h>
int main() {
char ch;
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("Alphabet\n");
else if (ch >= '0' && ch <= '9')
printf("Digit\n");
else
printf("Special Character\n");
return 0;
}

OUTPUT
@
Special Character
41.Find First and Last Digit of a Number
#include <stdio.h>
int main() {
int num, first, last;
scanf("%d", &num);
last = num % 10;
while (num >= 10)
num /= 10;
first = num;
printf("First: %d, Last: %d\n", first, last);
return 0;
}

OUTPUT
1234
First: 1, Last: 4

42.Find Power Using Recursion


#include <stdio.h>

int power(int base, int exp) {


if (exp == 0) return 1;
return base * power(base, exp - 1);
}

int main() {
int base, exp;
scanf("%d %d", &base, &exp);
printf("%d\n", power(base, exp));
return 0;
}
OUTPUT
24
16

43.Check Number is Palindrome (using loop)


#include <stdio.h>
int main() {
int n, rev = 0, orig, rem;
scanf("%d", &n);
orig = n;
while (n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
if (rev == orig)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
}

OUTPUT
121
Palindrome

44.Count Words in a Sentence


#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int count = 1, i = 0;
fgets(str, sizeof(str), stdin);
while (str[i]) {
if (str[i] == ' ' && str[i + 1] != ' ' && str[i + 1] != '\n')
count++;
i++;
}
printf("Words: %d\n", count);
return 0;
}

OUTPUT
Hello world from C
Words: 4

45.Copy One String to Another Without strcpy()


#include <stdio.h>
int main() {
char str1[100], str2[100];
int i = 0;
fgets(str1, sizeof(str1), stdin);
while (str1[i] != '\0') {
str2[i] = str1[i];
i++;
}
str2[i] = '\0';
printf("Copied: %s", str2);
return 0;
}

OUTPUT
OpenAI GPT
Copied: OpenAI GPT

You might also like