0% found this document useful (0 votes)
12 views5 pages

C Lab Manual Full Code

The document contains a lab manual with ten programming exercises in C, each demonstrating different programming concepts such as matrix addition, stack operations, finding the largest element in an array, bubble sort, palindrome checking, Fibonacci series, prime number checking, factorial calculation, string reversal, and simple file I/O. Each program includes the objective, code, and expected output. The exercises are designed to help students practice and understand fundamental programming techniques.

Uploaded by

inbox.ayesha1989
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)
12 views5 pages

C Lab Manual Full Code

The document contains a lab manual with ten programming exercises in C, each demonstrating different programming concepts such as matrix addition, stack operations, finding the largest element in an array, bubble sort, palindrome checking, Fibonacci series, prime number checking, factorial calculation, string reversal, and simple file I/O. Each program includes the objective, code, and expected output. The exercises are designed to help students practice and understand fundamental programming techniques.

Uploaded by

inbox.ayesha1989
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/ 5

C Lab Manual (With Full Code and

Output)
Program 1: Matrix Addition
Objective: Add two 2x2 matrices.

Code:

#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];
printf("Result: %d %d %d %d", c[0][0], c[0][1], c[1][0], c[1][1]);
return 0;
}

Expected Output:
Result: 6 8 10 12

Program 2: Stack Using Arrays


Objective: Implement stack operations using array.

Code:

#include<stdio.h>
#define SIZE 3
int stack[SIZE], top=-1;
void push(int val) {
if(top < SIZE-1) stack[++top]=val;
}
int main() {
push(1); push(2); push(3);
for(int i=0;i<=top;i++) printf("%d ", stack[i]);
return 0;
}

Expected Output:
123
Program 3: Find Largest in Array
Objective: Find largest element in an array.

Code:

#include<stdio.h>
int main() {
int arr[3]={4,7,1}, max=arr[0];
for(int i=1;i<3;i++) if(arr[i]>max) max=arr[i];
printf("Largest: %d", max);
return 0;
}

Expected Output:
Largest: 7

Program 4: Bubble Sort


Objective: Sort array using bubble sort.

Code:

#include<stdio.h>
int main() {
int a[]={3,2,1}, n=3;
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]);
return 0;
}

Expected Output:
123

Program 5: Palindrome Number


Objective: Check if a number is a palindrome.

Code:

#include<stdio.h>
int main() {
int n=121, r=0, t=n;
while(n){ r=r*10+n%10; n/=10; }
printf("%s", t==r?"Palindrome":"Not");
return 0;
}

Expected Output:
Palindrome

Program 6: Fibonacci Series


Objective: Print first 5 Fibonacci numbers.

Code:

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

Expected Output:
01123

Program 7: Prime Number Check


Objective: Check if a number is prime.

Code:

#include<stdio.h>
int main() {
int n=7,i,flag=1;
for(i=2;i<n;i++) if(n%i==0) flag=0;
printf("%s", flag?"Prime":"Not");
return 0;
}

Expected Output:
Prime
Program 8: Factorial Using Loop
Objective: Calculate factorial using loop.

Code:

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

Expected Output:
120

Program 9: Reverse a String


Objective: Reverse a given string.

Code:

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

Expected Output:
olleh

Program 10: Simple File I/O


Objective: Write and read from a file.

Code:
#include<stdio.h>
int main() {
FILE *f=fopen("a.txt","w");
fprintf(f,"Hello"); fclose(f);
f=fopen("a.txt","r"); char c; while((c=fgetc(f))!=EOF) putchar(c);
fclose(f);
return 0;
}

Expected Output:
Hello

You might also like