Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

PIC Pyq Code

Uploaded by

ryuk07u
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

PIC Pyq Code

Uploaded by

ryuk07u
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1) Write a program to reverse the number 1234 (i.e. 4321) using function.

#include <stdio.h>
void reverseNumber(int num) {
printf("Reversed number: ");
while (num > 0) {
printf("%d", num % 10);
num /= 10;
}
}
void main() {
int num = 1234;
reverseNumber(num);
}

2) Write a program to sum all the odd numbers between 1 to 20.

#include <stdio.h>
void main() {
int sum = 0;
for (int i = 1; i <= 20; i += 2) {
sum += i;
}
printf("Sum of odd numbers between 1 to 20: %d\n", sum);
}

3) Write a program to take input as a number and reverse it by while loop.

#include <stdio.h>

void main() {
int num, reversedNum = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
printf("Reversed number: %d\n", reversedNum);
}

4) Write a program to accept 10 numbers in an array and arrange them in ascending


order.

#include <stdio.h>

void main() {
int arr[10], i, j, temp;

printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++)
scanf("%d", &arr[i]);

for (i = 0; i < 9; i++) {


for (j = i + 1; j < 10; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

printf("Numbers in ascending order:\n");


for (i = 0; i < 10; i++)
printf("%d ", arr[i]);
printf("\n");
}

5) Write a program to print the reverse of an entered string using


pointer.

#include <stdio.h>

void main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);

char *ptr = str;


printf("Reversed string: ");
while (*ptr != '\0') {
ptr++;
}
ptr--;
while (ptr >= str) {
printf("%c", *ptr);
ptr--;
}
printf("\n");
}

6) Write a program to accept ten numbers and print average of it

#include <stdio.h>

void main() {
int numbers[10], sum = 0, i;

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

printf("Average: %.2f\n", (float) sum / 10);


}
7) Write a program to declare structure students having roll no, name & marks.
Accept & display data for 3 students.

#include <stdio.h>

struct Student {
int rollNo;
char name[50];
float marks;
};

void main() {
struct Student students[3];
printf("Enter data for 3 students:\n");
for (int i = 0; i < 3; i++) {
printf("Enter roll number for student %d: ", i + 1);
scanf("%d", &students[i].rollNo);
printf("Enter name for student %d: ", i + 1);
scanf("%s", students[i].name); // Assuming no spaces in the name
printf("Enter marks for student %d: ", i + 1);
scanf("%f", &students[i].marks);
}

printf("\nStudent data:\n");
for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i + 1);
printf("Roll number: %d\n", students[i].rollNo);
printf("Name: %s\n", students[i].name);
printf("Marks: %.2f\n", students[i].marks);
}
}

8) Write a program to add, subtract, multiply and divide two numbers, accepted
from the user using switch case.

#include <stdio.h>

void main() {
float num1, num2, result;
char operator;

printf("Enter operator (+, -, *, /): ");


scanf(" %c", &operator);

printf("Enter two numbers: ");


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

switch (operator) {
case '+':
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f\n", result);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Invalid operator.\n");
}
}

9) Write a program to calculate the sum of all the odd numbers between 1 to 20.

#include <stdio.h>

void main() {
int sum = 0;

for (int i = 1; i <= 20; i += 2) {


sum += i;
}

printf("Sum of odd numbers between 1 to 20: %d\n", sum);


}

10)Write a program to compute the sum of all elements stored in an array using
pointers.

#include <stdio.h>

void main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
int sum = 0;

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


sum += *ptr;
ptr++;
}

printf("Sum of elements in the array: %d\n", sum);


}
11)Write a function to print Fibonacci series starting from 0, 1.

#include <stdio.h>

void fibonacci(int n) {
int a = 0, b = 1;
int i, next;

printf("Fibonacci series: ");


for (i = 0; i < n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
}
printf("\n");
}

void main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
fibonacci(n);
}

12)Calculate factorial of a number using recursion.

#include <stdio.h>

void main() {
int num, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);

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


fact *= i;
}

printf("Factorial of %d is %d\n", num, fact);


}

13)Write a program to display table of given number (Accept number from user).

#include <stdio.h>

void main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);

printf("Multiplication table of %d:\n", number);


for (int i = 1; i <= 10; i++) {
printf("%d * %d = %d\n", number, i, number * i);
}
}
14)Write a program to sum all the even numbers between 1 to 100.

#include <stdio.h>

void main() {
int sum = 0;
for (int i = 2; i <= 100; i += 2) {
sum += i;
}
printf("Sum of even numbers between 1 to 100: %d\n", sum);
}

15)Write a program to accept ten numbers in an array. Sort array elements and
display it

#include <stdio.h>

void main() {
int numbers[10];

// Accepting ten numbers from the user


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

// Sorting the array elements in ascending order


for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9 - i; j++) {
if (numbers[j] > numbers[j + 1]) {
// Swap if the current element is greater than the next
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}

// Displaying the sorted array


printf("\nSorted array in ascending order:\n");
for (int i = 0; i < 10; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
}
16)Write a program for addition of 3×3 matrices.

#include <stdio.h>

void main() {
int matrix1[3][3], matrix2[3][3], sum[3][3];

// Input for the first matrix


printf("Enter elements of the first matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix1[i][j]);
}
}

// Input for the second matrix


printf("Enter elements of the second matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix2[i][j]);
}
}

// Addition of matrices
printf("Resultant matrix after addition:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
printf("%d ", sum[i][j]);
}
printf("\n");
}
}
17)Write a program to compute the sum of all elements stored in an array using
pointers.

#include <stdio.h>

void main() {
int arr[] = {5, 10, 15, 20, 25};
int *ptr = arr;
int sum = 0;

// Computing the sum of elements using pointers


for (int i = 0; i < 5; i++) {
sum += *ptr;
ptr++;
}

printf("Sum of elements in the array: %d\n", sum);


}
18)Write a program using structure to display information of employee which
consist of employee id, name, age and salary.

#include <stdio.h>

struct Employee {
char name[50];
int age;
char city[50];
};

void main() {
struct Employee employees[3];

printf("Enter details for 3 employees:\n");


for (int i = 0; i < 3; i++) {
printf("Employee %d:\n", i + 1);
printf("Name: ");
scanf("%s", employees[i].name);
printf("Age: ");
scanf("%d", &employees[i].age);
printf("City: ");
scanf("%s", employees[i].city);
}

printf("\nEmployee details:\n");
for (int i = 0; i < 3; i++) {
printf("Employee %d:\n", i + 1);
printf("Name: %s\n", employees[i].name);
printf("Age: %d\n", employees[i].age);
printf("City: %s\n", employees[i].city);
}
}

19)Write a program to demonstrate use of strcmp( ), strcpy( ), strlen( ), strcat(


).

#include <stdio.h>
#include <string.h>

void main() {
char str1[50] = "Hello";
char str2[50] = "World";
char str3[50];

// strcmp()
int result = strcmp(str1, str2);
printf("strcmp(\"%s\", \"%s\") = %d\n", str1, str2, result);

// strcpy()
strcpy(str3, str1);
printf("strcpy(str3, str1): %s\n", str3);

// strlen()
printf("strlen(\"%s\") = %d\n", str1, strlen(str1));
// strcat()
strcat(str1, str2);
printf("strcat(str1, str2): %s\n", str1);
}

20)Write a program to perform arithmetic operations on pointer.

#include <stdio.h>

void main() {
int num1 = 10, num2 = 5;
int *ptr1 = &num1, *ptr2 = &num2;
int sum, diff, product, quotient;

sum = *ptr1 + *ptr2;


diff = *ptr1 - *ptr2;
product = *ptr1 * *ptr2;
quotient = *ptr1 / *ptr2;

printf("Sum: %d\n", sum);


printf("Difference: %d\n", diff);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
}

21) Write ‘C’ program to add two distances given in kilometer using structure.

#include <stdio.h>

struct Distance {
float kilometers;
};

void main() {
struct Distance distance1, distance2, sum;

// Input for first distance


printf("Enter distance 1 in kilometers: ");
scanf("%f", &distance1.kilometers);

// Input for second distance


printf("Enter distance 2 in kilometers: ");
scanf("%f", &distance2.kilometers);

// Adding distances
sum.kilometers = distance1.kilometers + distance2.kilometers;

printf("Sum of distances: %.2f kilometers\n", sum.kilometers);


}
22)Write a program to calculate sum of all elements stored in given array using
pointers.

#include <stdio.h>

void main() {
int arr[] = {5, 10, 15, 20, 25};
int *ptr = arr;
int sum = 0;

// Computing the sum of elements using pointers


for (int i = 0; i < 5; i++) {
sum += *ptr;
ptr++;
}

printf("Sum of elements in the array: %d\n", sum);


}

23)Write a ‘C’ program to determine whether a given number is prime or not and
also draw flowchart for this program

#include <stdio.h>

void main() {
int num, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);

if (num <= 1) {
isPrime = 0;
} else {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
}

24)Write a ‘C’ program to find the largest and smallest number in a given array.

#include <stdio.h>

void main() {
int arr[] = {5, 10, 15, 3, 20, 25};
int size = sizeof(arr) / sizeof(arr[0]);
int smallest = arr[0], largest = arr[0];
// Finding the smallest and largest numbers in the array
for (int i = 1; i < size; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
if (arr[i] > largest) {
largest = arr[i];
}
}

printf("Smallest number: %d\n", smallest);


printf("Largest number: %d\n", largest);
}

25)Write a ‘C’ program to demonstrate access structure members using pointer

#include <stdio.h>

struct Student {
int roll_no;
char name[50];
float marks;
};

void main() {
struct Student s;
struct Student *ptr;

ptr = &s;

// Accessing structure members using pointer


printf("Enter Roll No: ");
scanf("%d", &ptr->roll_no);

printf("Enter Name: ");


scanf("%s", ptr->name);

printf("Enter Marks: ");


scanf("%f", &ptr->marks);

// Displaying structure members


printf("\nStudent Details:\n");
printf("Roll No: %d\n", ptr->roll_no);
printf("Name: %s\n", ptr->name);
printf("Marks: %.2f\n", ptr->marks);
}
26)Write a program using switch statement to check whether entered character is
VOWEL or CONSONANT.

#include <stdio.h>

void main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);

switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("%c is a VOWEL.\n", ch);
break;
default:
printf("%c is a CONSONANT.\n", ch);
}
}

27)Write a program to find area of circle using user defined function.

#include <stdio.h>

#define PI 3.14159

float calculateArea(float radius) {


return PI * radius * radius;
}

void main() {
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);

printf("Area of the circle: %.2f\n", calculateArea(radius));


}

28)Write a program to declare structure employee having data member name, age,
city. Accept data for three employees and display it.

#include <stdio.h>

struct employee {
char name[50];
int age;
char city[50];
};
void main() {
struct employee emp[3];

// Accepting data for three employees


for (int i = 0; i < 3; i++) {
printf("Enter details for employee %d:\n", i + 1);
printf("Name: ");
scanf("%s", emp[i].name);
printf("Age: ");
scanf("%d", &emp[i].age);
printf("City: ");
scanf("%s", emp[i].city);
}

// Displaying data for three employees


printf("\nEmployee Details:\n");
for (int i = 0; i < 3; i++) {
printf("Employee %d\n", i + 1);
printf("Name: %s\n", emp[i].name);
printf("Age: %d\n", emp[i].age);
printf("City: %s\n", emp[i].city);
printf("\n");
}
}

29) Suresh’s basic salary is input through the keyboard. His dearness allowance is
40% of basic salary, and house rent allowance is 20% of basic salary. Write a
program to calculate his gross salary.

#include <stdio.h>

void main() {
float basic_salary, da, hra, gross_salary;

// Input basic salary


printf("Enter Suresh's basic salary: ");
scanf("%f", &basic_salary);

// Calculate dearness allowance and house rent allowance


da = 0.4 * basic_salary;
hra = 0.2 * basic_salary;

// Calculate gross salary


gross_salary = basic_salary + da + hra;

// Display gross salary


printf("Suresh's gross salary: %.2f\n", gross_salary);
}
30) Write a program to swap the values of variables a = 10, b = using function.

#include <stdio.h>

void swap(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

void main() {
int a = 10, b = 20;
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
}

31) Implement a program to demonstrate logical AND operator.

#include <stdio.h>

void main() {
int a = 5, b = 10;
if (a > 0 && b > 0) {
printf("Both a and b are greater than 0\n");
} else {
printf("At least one of a and b is not greater than 0\n");
}
}

32) Develop a program to swap two numbers using pointer and add swapped numbers
also print their addition.

#include <stdio.h>

void swapAndAdd(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

void main() {
int a = 10, b = 20;
printf("Before swapping: a = %d, b = %d\n", a, b);
swapAndAdd(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
printf("Sum of swapped numbers: %d\n", a + b);
}
33)Design a program in C to read the n numbers of values in an array and display
it in reverse order.

#include <stdio.h>

void main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Elements in reverse order:\n");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
}

34) Write a program to take input as a number and reverse it by using a while
loop.

#include <stdio.h>

void main() {
int num, reversedNum = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
printf("Reversed number: %d\n", reversedNum);
}

35) write a program to add numbers until user enters zero

#include <stdio.h>

void main() {
int num, sum = 0;
do {
printf("Enter a number (enter 0 to exit): ");
scanf("%d", &num);
sum += num;
} while (num != 0);
printf("Sum: %d\n", sum);
}
36) Write a program using structure to display information of employee which
consist of employee id, name, age and salary.

#include <stdio.h>

void main() {
struct Employee {
int id;
char name[50];
int age;
float salary;
} emp;
printf("Enter employee details:\n");
printf("ID: ");
scanf("%d", &emp.id);
printf("Name: ");
scanf("%s", emp.name);
printf("Age: ");
scanf("%d", &emp.age);
printf("Salary: ");
scanf("%f", &emp.salary);
printf("Employee Details:\n");
printf("ID: %d\n", emp.id);
printf("Name: %s\n", emp.name);
printf("Age: %d\n", emp.age);
printf("Salary: %.2f\n", emp.salary);
}

37) Write a c program for multiplication to two 3* 3 matrix

#include <stdio.h>

void main() {
int mat1[3][3], mat2[3][3], result[3][3];
printf("Enter elements of first matrix:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%d", &mat2[i][j]);
}
}
// Matrix multiplication
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
result[i][j] = 0;
for (int k = 0; k < 3; ++k) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
printf("Resultant matrix after multiplication:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
printf("%d ", result[i][j]);
}
printf("\n");
}
}

38) Write a program to declare an array of 5 elements and display sum of all
array elements.

#include <stdio.h>

void main() {
int arr[5], sum = 0;
printf("Enter 5 elements:\n");
for (int i = 0; i < 5; ++i) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of array elements: %d\n", sum);
}

39) Write a C program to declare structure employee having data member name,
age, designation and salary. Accept and display information of 1 employee

#include <stdio.h>

void main() {
struct Employee {
char name[50];
int age;
char designation[50];
float salary;
} emp;
printf("Enter employee details:\n");
printf("Name: ");
scanf("%s", emp.name);
printf("Age: ");
scanf("%d", &emp.age);
printf("Designation: ");
scanf("%s", emp.designation);
printf("Salary: ");
scanf("%f", &emp.salary);
printf("Employee Details:\n");
printf("Name: %s\n", emp.name);
printf("Age: %d\n", emp.age);
printf("Designation: %s\n", emp.designation);
printf("Salary: %.2f\n", emp.salary);
}

40) Write a program to declare structure student having roll no, name & marks.
Accept & display data for 3 students

#include <stdio.h>

void main() {
struct Student {
int rollNo;
char name[50];
float marks;
} students[3];
printf("Enter details of 3 students:\n");
for (int i = 0; i < 3; ++i) {
printf("Student %d\n", i + 1);
printf("Roll No: ");
scanf("%d", &students[i].rollNo);
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
}
printf("Details of students:\n");
for (int i = 0; i < 3; ++i) {
printf("Student %d\n", i + 1);
printf("Roll No: %d\n", students[i].rollNo);
printf("Name: %s\n", students[i].name);
printf("Marks: %.2f\n", students[i].marks);
}
}

41) Write a program to read two strings and find whether they are equal or not

#include <stdio.h>
#include <string.h>

void main() {
char str1[100], str2[100];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
if (strcmp(str1, str2) == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
}

42) Write a program to compute the sum of all elements stored in an array
using pointers.

#include <stdio.h>

void main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
int sum = 0;
for (int i = 0; i < 5; ++i) {
sum += *ptr;
ptr++;
}
printf("Sum of array elements: %d\n", sum);
}

43) Write a program to accept a string as input from user and determine its
length. [Don’t use built in library function strlen()]

#include <stdio.h>

void main() {
char str[100];
int length = 0;
printf("Enter a string: ");
scanf("%s", str);
while (str[length] != '\0') {
length++;
}
printf("Length of the string: %d\n", length);
}

44) Develop a program to accept an integer number and print whether it is


palindrome or not.
#include <stdio.h>

void main() {
int num, reversedNum = 0, originalNum;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
while (num != 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
}

45) Write a program to perform addition, subtraction, multiplication and


division of two integer number using function.

#include <stdio.h>

int add(int a, int b) {


return a + b;
}

int subtract(int a, int b) {


return a - b;
}

int multiply(int a, int b) {


return a * b;
}

float divide(int a, int b) {


if (b != 0) {
return (float)a / b;
} else {
printf("Error: Division by zero\n");
return 0;
}
}

void main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Addition: %d\n", add(num1, num2));
printf("Subtraction: %d\n", subtract(num1, num2));
printf("Multiplication: %d\n", multiply(num1, num2));
printf("Division: %.2f\n", divide(num1, num2));
}

46) Write a program to accept marks of four subjects as input from user.
Calculate and display total and percentage marks of student.
#include <stdio.h>

void main() {
int marks[4], totalMarks = 0;
float percentage;
printf("Enter marks of four subjects:\n");
for (int i = 0; i < 4; ++i) {
printf("Subject %d: ", i + 1);
scanf("%d", &marks[i]);
totalMarks += marks[i];
}
percentage = (float)totalMarks / 4;
printf("Total marks: %d\n", totalMarks);
printf("Percentage: %.2f%%\n", percentage);
}
47) Write a program to swap two numbers using call by value

#include <stdio.h>

void swap(int a, int b) {


int temp = a;
a = b;
b = temp;
printf("Inside swap function: a = %d, b = %d\n", a, b);
}

void main() {
int num1 = 5, num2 = 10;
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(num1, num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
}

48) Write a program to Print values of variables and their addresses

#include <stdio.h>

void main() {
int num = 10;
float pi = 3.14;
char letter = 'A';

// Printing values of variables


printf("Value of num: %d\n", num);
printf("Value of pi: %.2f\n", pi);
printf("Value of letter: %c\n", letter);

// Printing addresses of variables


printf("Address of num: %p\n", (void*)&num);
printf("Address of pi: %p\n", (void*)&pi);
printf("Address of letter: %p\n", (void*)&letter);
}

49)If the value of a number (N) is entered through the keyboard. Write a program
using recursion to calculate and display factorial of number (N).

#include <stdio.h>

// Function to calculate factorial recursively


int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

void main() {
int num;

// Input number from user


printf("Enter a number: ");
scanf("%d", &num);

// Check if the number is non-negative


if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Call the factorial function and display the result
printf("Factorial of %d is: %d\n", num, factorial(num));
}
}

You might also like