narula institute of technology
C ASSIGNMENT
NAME: Soumik Chowdhury
UNIVERSITY ROLL NO: 127256100014
STREAM: masters of computer applications
YEAR: 1st
SEMESTER: 1st
SUBJECT: PRGRAMMING FOR PROBLEM SOLVING
using c
SUBJECT CODE: Mca 101
1. Largest among 3 numbers
Program
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("Largest number is: %d", a);
else if (b >= a && b >= c)
printf("Largest number is: %d", b);
else
printf("Largest number is: %d", c);
return 0;
}
OUTPUT:
Enter three numbers: 5 9 3
Largest number is: 9
2. Factorial of a number
PROGRAM
(a) Without Recursion
#include <stdio.h>
int main() {
int n, i;
long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fact = fact * i;
printf("Factorial = %ld", fact);
return 0;
}
Output:
Enter a number: 5
Factorial = 120
(b) With Recursion
PROGRAM
#include <stdio.h>
long factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial = %ld", factorial(n));
return 0;
}
OUTPUT:
Enter a number: 4
Factorial = 24
3. Check Palindrome using Array (String)
PROGRAM
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, len, flag = 1;
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
flag = 0;
break;
}
}
if (flag)
printf("String is Palindrome");
else
printf("String is Not Palindrome");
return 0;
}
OUTPUT:
Enter a string: level
String is Palindrome
4. Matrix Multiplication (3×3)
PROGRAM
#include <stdio.h>
int main() {
int a[3][3], b[3][3], c[3][3];
int i, j, k;
printf("Enter elements of first matrix:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
printf("Enter elements of second matrix:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
scanf("%d", &b[i][j]);
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
c[i][j] = 0;
for (k = 0; k < 3; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
printf("Resultant Matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
OUTPUT:
Resultant Matrix:
30 24 18
84 69 54
138 114 90
5. File Handling – Write "Hello World" to File
PROGRAM
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("[Link]", "w");
if (fp == NULL) {
printf("File cannot be opened");
return 1;
}
fprintf(fp, "Hello World");
fclose(fp);
printf("Data written to file successfully");
return 0;
}
OUTPUT:
Data written to file successfully
(File [Link] will contain: Hello World)
6. Dynamic Memory Allocation using malloc
PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
int *arr;
printf("Enter number of elements: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed");
return 1;
}
printf("Enter elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Entered elements are:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
free(arr);
return 0;
}
OUTPUT:
Enter number of elements: 3
Enter elements:
10 20 30
Entered elements are:
10 20 30