Krish
Krish
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
➔
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
➔
#include <stdio.h>
int main() {
int num, reversed = 0, original, remainder;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
if (original == reversed) {
printf("%d is a palindrome.\n", original);
} else {
printf("%d is not a palindrome.\n", original);
}
return 0;
}
➔
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Q.5 Write a program for printing multiplication table of 5 using for loop.
➔
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("5 x %d = %d\n", i, 5 * i);
}
return 0;
}
Q.6 Write a program for printing prime numbers between range of 1 to 100
➔
#include <stdio.h>
#include <stdbool.h>
int main() {
for (int num = 2; num <= 100; num++) {
bool isPrime = true;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) printf("%d ", num);
}
return 0;
}
➔
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
➔
#include <stdio.h>
int main() {
int n, i, sum = 0;
return 0;
}
int main() {
int n;
float sum = 0.0, average;
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
average = sum / n;
printf("The average of the array elements is: %.2f\n",
average);
return 0;
}
int main() {
int n;
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
➔
#include <stdio.h>
int main() {
int n;
if (n < 2) {
printf("Array size must be at least 2.\n");
return 1;
}
int arr[n];
if (second == -1) {
printf("There is no distinct second largest
element.\n");
} else {
printf("The second largest element in the array is:
%d\n", second);
}
return 0;
}
Q.12 Write a program to concatenate two arrays.
➔
#include <stdio.h>
int main() {
int n1, n2;
printf("Concatenated array:\n");
for (int i = 0; i < n1 + n2; i++) {
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
Q.13 Write a program to perform matrix addition and store the result
in third array and display it.
➔
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows and columns of the
matrices: ");
scanf("%d %d", &rows, &cols);
}
}
return 0;
}
➔
#include <stdio.h>
#define MAX 10 // Maximum size for matrices
int main() {
int matrix1[MAX][MAX], matrix2[MAX][MAX],
result[MAX][MAX];
int rows1, cols1, rows2, cols2;
if (cols1 != rows2) {
printf("Matrix multiplication is not possible with the
given dimensions.\n");
return 1;
}
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &matrix1[i][j]);
}
return 0;
}
Q.15 Write a program to find the length of the string using library
functions.
➔
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
if (str[length - 1] == '\n') {
length--; // Decrease length to ignore the newline
character
}
printf("The length of the string is: %d\n", length);
return 0;
}
Q.16 Write a program to find the length of the sting without using
library functions.
➔
#include <stdio.h>
int stringLength(char str[]) {
int length = 0;
return 0;
}
int main() {
char str[100];
int alphabets, digits, specialChars;
return 0;
}
➔
#include <stdio.h>
str1[i] = '\0';
}
int main() {
char str1[100], str2[50];
int len1 = 0;
while (str1[len1] != '\0') len1++;
if (str1[len1 - 1] == '\n') {
str1[len1 - 1] = '\0'; }
int len2 = 0;
while (str2[len2] != '\0') len2++;
if (str2[len2 - 1] == '\n') {
str2[len2 - 1] = '\0';
}
concatenateStrings(str1, str2);
return 0;
}
➔
#include <stdio.h>
#include <string.h>
int main() {
int n;
printf("Enter number of strings: ");
scanf("%d", &n);
char arr[n][100];
printf("Enter %d strings:\n", n);
for (int i = 0; i < n; i++) {
scanf("%s", arr[i]);
}
sortStrings(arr, n);
return 0;
}
Q.20 Write a program to explain the concept of function in C with the
help of an example.
➔
#include <stdio.h>
int add(int a, int b);
int main() {
int num1, num2, sum;
return 0;
}