0% found this document useful (0 votes)
21 views20 pages

CSE Lab 05

Uploaded by

akumakirito2
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)
21 views20 pages

CSE Lab 05

Uploaded by

akumakirito2
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/ 20

Problem 01: Factorial Calculation

Problem: Write a function to calculate the factorial of a given number.

Function Signature: int factorial(int n)

Sample Input: 5

Sample Output: 120

#include <stdio.h>

int factorial(int n) {

int result = 1;

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

result *= i;

return result;

int main() {

int number;

printf("Enter a number: ");

scanf("%d", &number);

int result = factorial(number);

printf("The factorial of %d is %d\n", number, result);

return 0;

}
Input & Output:
Problem 02: Prime Number Check

Problem: Write a function to check if a given number is a prime number.

Function Signature: int is_prime(int n)

Sample Input: 7

Sample Output: 1 (1 for prime, 0 for not prime)

#include <stdio.h>

int is_prime(int n) {

if (n <= 1) return 0;

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

if (n % i == 0) return 0;

return 1;

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("%d\n", is_prime(num));

return 0;

}
Input & Output:
Problem 03: Greatest Common Divisor (GCD)

Problem: Write a function to find the GCD of two numbers using Euclidean algorithm.

Function Signature: int gcd(int a, int b)

Sample Input: 48, 18

Sample Output: 6

#include <stdio.h>

int gcd(int a, int b) {

while (b != 0) {

int t = b;

b = a % b;

a = t;

return a;

int main() {

int num1, num2;

printf("Enter two integers: ");

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

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

return 0;

}
Input & Output:
Problem 04: String Reversal

Problem: Write a function to reverse a given string.

Function Signature: void reverse_string(char str[])

Sample Input: “hello”

Sample Output: “olleh”

#include <stdio.h>

#include <string.h>

void reverse_String(char str[]) {

int length = strlen(str);

for (int i = length - 1; i >= 0; i--) {

printf("%c",str[i]);

int main() {

char in[100];

gets(in);

reverse_String(in);

return 0;

}
Input & Output:
Problem 05: Fibonacci Series

Problem: Write a function to generate the first n Fibonacci numbers.

Function Signature: void fibonacci(int n)

Sample Input: 5

Sample Output: 0, 1, 1, 2, 3

#include<stdio.h>

void fibonacci(int n) {

if (n >= 1) {

printf("0");

if (n >= 2) {

printf(", 1");

int a = 0, b = 1, c;

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

c = a + b;

printf(", %d", c);

a = b;

b = c;

printf("\n");

}
int main() {

int n;

scanf("%d", &n);

fibonacci(n);

return 0;

Input & Output:


Problem 06: Armstrong Number Check

Problem: Write a function to check if a given number is an Armstrong number.

Function Signature: int is_armstrong(int n)

Sample Input: 153e

Sample Output: 1 (1 for Armstrong number, 0 for not Armstrong number)

#include <stdio.h>

#include <math.h>

int count_digits(int n) {

int count = 0;

while (n != 0) {

count++;

n /= 10;

return count;

int is_armstrong(int n) {

int num_digits = count_digits(n);

int sum = 0;

int t= n;

while (t != 0) {

int digit = t % 10;

sum += pow(digit, num_digits);


t/= 10;

return (sum == n);

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

int result = is_armstrong(num);

printf("%d\n", result);

return 0;

Input & Output:


Problem 07: Sum of Digits

Problem: Write a function to calculate the sum of digits of a given number.

Function Signature: int sum_of_digits(int n)

Sample Input: 123

Sample Output: 6

#include <stdio.h>

int sum_of_digits(int n) {

int sum = 0;

while (n != 0) {

sum += n % 10;

n /= 10;

return sum;

int main() {

int number;

scanf("%d", &number);

int result = sum_of_digits(number);

printf(" %d\n", result);

return 0;

}
Input & Output:
Problem 08: Palindrome Check

Problem: Write a function to check if a given string is a palindrome.

Function Signature: int is_palindrome(char str[])

Sample Input: “radar”

Sample Output: 1 (1 for palindrome, 0 for not palindrome)

Source Code:

#include <stdio.h>

#include <string.h>

int is_Palindrome(char str[]) {

int length = strlen(str);

for (int i = 0; i < length / 2; i++){

if (str[i] != str[length - 1 - i]) {

return 0; }

return 1; }

int main()

{ char in[100];

gets(in);

int result = is_Palindrome(in);

printf("%d\n", result);

return 0;

}
Input & Output:
Problem 09: Array Maximum

Problem: Write a function to find the maximum element in an array.

Function Signature: int max_in_array(int arr[], int size)

Sample Input: {1, 5, 3, 9, 2}

Sample Output: 9

#include <stdio.h>

int max_in_array(int arr[], int size){

int max = arr[0];

for (int i = 1; i < size; i++) {

if (arr[i] > max) {

max = arr[i];

return max;

int main() {

int size;

printf("Enter the size of the array: ");

scanf("%d", &size);

int arr[size];

for (int i = 0; i < size;i++) {

scanf("%d", &arr[i]);

}
int maximum= max_in_array(arr, size);

printf("%d",maximum);

return 0;

Input & Output:


Problem 10: Power Calculation

Problem: Write a function to calculate the power of a number raised to another


number.

Function Signature: int power(int base, int exp)

Sample Input: 2, 3

Sample Output: 8

Source Code:

#include <stdio.h>

int power(int base, int exp) {

int result = 1;

while (exp > 0) {

result *= base;

exp--;

return result;

int main() {

int base, exp;

scanf("%d", &base);

scanf("%d", &exp);

int result = power(base, exp);

printf("%d\n", result);

return 0;}
Input & Output:

You might also like