The document contains code for three different C programs:
1. A program to calculate the factorial of a number using a function. It prompts the user to enter a number and prints the factorial.
2. Another factorial program using a function that returns the factorial to the main function.
3. A bubble sort algorithm implemented as a function to sort an array of integers in ascending order. It prompts the user to enter the array size and elements before calling the sorting function.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
425 views
Factorial Program in C Using Function
The document contains code for three different C programs:
1. A program to calculate the factorial of a number using a function. It prompts the user to enter a number and prints the factorial.
2. Another factorial program using a function that returns the factorial to the main function.
3. A bubble sort algorithm implemented as a function to sort an array of integers in ascending order. It prompts the user to enter the array size and elements before calling the sorting function.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
.
Factorial program in c using function
2. C program to find factorial of a number
#include<stdio.h>
int findFactorial(int); int main(){ int i,factorial,num;
printf("Enter a number: "); scanf("%d",&num);
factorial = findFactorial(num); printf("Factorial of %d is: %d",num,factorial);
return 0; }
int findFactorial(int num){ int i,f=1;
for(i=1;i<=num;i++) f=f*i;
return f; } Sample output: Enter a number: 8 Factorial of 8 is: 40320 C Program to find Factorial of given Number using Function
/* C Program to find Factorial of given Number using Function*/ #include <stdio.h> //#include<conio.h> int main() { int num; //clrscr(); printf("\n >> PROGRAM TO FIND FACTORIAL OF GIVEN NUMBER using Function <<\n"); printf("\n Enter the Number whose Factorial you want: "); scanf("%d",&num); printf("\n The factorial of %d is %d.\n\n", num,factorial(num)); //factorial(num) prints the value returned by the function //getch(); return 0;
}
factorial(num1) { int i,fact=1; for(i=num1; i>=2 ; i--) { fact = fact * i; } return fact; //returning fact to main function } /* Bubble sort code */
#include <stdio.h>
int main() { int array[100], n, c, d, swap;
printf("Enter number of elements\n"); scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) scanf("%d", &array[c]);
for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } }
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]);
return 0; }
#include <stdio.h>
void bubble_sort(long [], long);
int main() { long array[100], n, c, d, swap;
printf("Enter number of elements\n"); scanf("%ld", &n);
printf("Enter %ld integers\n", n);
for (c = 0; c < n; c++) scanf("%ld", &array[c]);
bubble_sort(array, n);
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ ) printf("%ld\n", array[c]);
return 0; }
void bubble_sort(long list[], long n) { long c, d, t;
for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (list[d] > list[d+1]) { /* Swapping */