You are on page 1of 13

1.

implement a program to find tranpose of a matrix in c language

#include <stdio.h>

#define ROWS 3
#define COLS 3

// Function to transpose a matrix


void transposeMatrix(int matrix[ROWS][COLS], int transpose[COLS][ROWS]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
transpose[j][i] = matrix[i][j];
}
}
}

// Function to display a matrix


void displayMatrix(int matrix[ROWS][COLS]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

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>

// Recursive function to calculate power


double power(double x, int n) {
if (n == 0)
return 1;
else if (n > 0)
return x * power(x, n - 1);
else // for negative exponent
return (1 / x) * power(x, n + 1);
}

int main() {
double x;
int n;

printf("Enter the value of x: ");


scanf("%lf", &x);
printf("Enter the value of n: ");
scanf("%d", &n);

double result = power(x, n);

printf("%.2lf raised to the power %d is: %.2lf\n", x, n, result);

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>

// Function to find GCD of two numbers using recursion


int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}

int main() {
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));

return 0;
}

5. write a program to find summation of n number using recursion in c language


#include <stdio.h>
int sum(int n) {
if (n == 0) {
return 0; // Base case: If n is 0, return 0
} else {
return n + sum(n - 1); // Recursive call to sum function
}}
int main() {
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);
if (n < 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}
printf("Sum of first %d numbers is: %d\n", n, sum(n));

return 0;
}
6. write a program in c to find the reverse of a given string without using inbuilt string
function
#include <stdio.h>

// Function to find the length of a string


int length(char str[]) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}

// Function to reverse a string


void reverse(char str[]) {
int len = length(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}

int main() {
char str[100];

printf("Enter a string: ");


scanf("%s", str);

reverse(str);

printf("Reverse of the string: %s\n", 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;
}

// Function to reverse a string


void reverse(char str[]) {
int len = length(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}

int main() {
char str[100];

printf("Enter a string: ");


scanf("%s", str);

reverse(str);

printf("Reverse of the string: %s\n", 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;

printf("Enter a number: ");


scanf("%d", &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];

// Input numbers from the user


printf("Enter %d numbers:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
printf("Number %d: ", i + 1);
scanf("%d", &numbers[i]);
}

// Print the entered numbers


printf("Entered numbers:\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", numbers[i]);
}
printf("\n");

return 0;
}
10. write a program to print fibonacci series in c
#include <stdio.h>

// Function to print Fibonacci series up to n terms


void fibonacci(int n) {
int first = 0, second = 1, next;

printf("Fibonacci Series up to %d terms:\n", n);


printf("%d, %d, ", first, second);

for (int i = 2; i < n; i++) {


next = first + second;
printf("%d", next);
if (i != n - 1) {
printf(", ");
}
first = second;
second = next;
}
printf("\n");
}

int main() {
int n;

printf("Enter the number of terms in the Fibonacci series: ");


scanf("%d", &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>

// Function to find factorial of a number using recursion


unsigned long long factorial(int n) {
if (n == 0 || n == 1) {
return 1; // Base case: factorial of 0 or 1 is 1
} else {
return n * factorial(n - 1); // Recursive call to factorial function
}
}

int main() {
int n;

printf("Enter a non-negative integer: ");


scanf("%d", &n);

if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
return 1;
}

printf("Factorial of %d is %llu\n", n, factorial(n));

return 0;
}
12.write a c program to find lcm of two numbers using recurion in c
#include <stdio.h>

// Function to find GCD (Greatest Common Divisor) using recursion


int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}

// Function to find LCM (Least Common Multiple) using recursion


int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}

int main() {
int num1, num2;

printf("Enter two positive integers: ");


scanf("%d %d", &num1, &num2);

if (num1 <= 0 || num2 <= 0) {


printf("Invalid input. Please enter positive integers.\n");
return 1;
}

printf("LCM of %d and %d is: %d\n", num1, num2, lcm(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;

printf("Enter three numbers: ");


scanf("%d %d %d", &num1, &num2, &num3);

if (num1 >= num2) {


if (num1 >= num3) {
printf("%d is the largest.\n", num1);
} else {
printf("%d is the largest.\n", num3);
}
} else {
if (num2 >= num3) {
printf("%d is the largest.\n", num2);
} else {
printf("%d is the largest.\n", num3);
}
}

return 0;
}

You might also like