C Programming Important Codes - Jai PDF
C Programming Important Codes - Jai PDF
#include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int matrix[ROWS][COLS] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int transpose[COLS][ROWS];
printf("Original Matrix:\n");
displayMatrix(matrix);
transposeMatrix(matrix, transpose);
printf("\nTranspose Matrix:\n");
displayMatrix(transpose);
return 0;
}
2. write a program to find the power of x raised to n that is:x^n, using recursive
function in c language
#include <stdio.h>
int main() {
double x;
int n;
return 0;
}
3. write a program to accept elements of one dimensional array from user and sort and
display them in ascending order in c language
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp; }
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}}}}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n)
printf("Sorted array in ascending order:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
4. write a c program to find the gcd of two numbers using recursion
#include <stdio.h>
int main() {
int num1, num2;
return 0;
}
return 0;
}
6. write a program in c to find the reverse of a given string without using inbuilt string
function
#include <stdio.h>
int main() {
char str[100];
reverse(str);
return 0;
}
7. write a program in c to find the reverse of a given string without using inbuilt string
function
#include <stdio.h>
int length(char str[]) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
int main() {
char str[100];
reverse(str);
return 0;
}
8. write a program to check if the entered number is prime number or not in c
#include <stdio.h>
int isPrime(int num) {
if (num < 2) {
return 0;
}
// Check divisibility from 2 to sqrt(num)
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return 0; // Not prime
}
}
return 1; // Prime
}
int main() {
int num;
if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
9. write a program to accept set of 10 numbers and print the numbers using arrays in c
#include <stdio.h>
#define SIZE 10
int main() {
int numbers[SIZE];
return 0;
}
10. write a program to print fibonacci series in c
#include <stdio.h>
int main() {
int n;
if (n <= 0) {
printf("Invalid input. Number of terms should be greater than zero.\n");
return 1;
}
fibonacci(n);
return 0;
}
11. write a program using recurion to find factorial of a number in c
#include <stdio.h>
int main() {
int n;
if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
return 1;
}
return 0;
}
12.write a c program to find lcm of two numbers using recurion in c
#include <stdio.h>
int main() {
int num1, num2;
return 0;
}
13.write a program to find largest of three numbers using nested if-else in c
#include <stdio.h>
int main() {
int num1, num2, num3;
return 0;
}