Factorial Function in C

Hi Friends

I hope you missed me. Here’s a C Program showing Factorial functionality.

/* Program demonstrating Factorial Function */

 

#include<stdio.h>

#include<conio.h>

 

//Factorial Function 1st version

int factorial1(int num)

{

      int i;

      int result = 1;

     

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

      {

               result = result * i;

      }

     

      return result; 

}

 

//Factorial Function 2nd version

int factorial2(int num)

{

      int i;

      int result = 1;

     

      for(i=num; i>=1; i–)

      {

               result = result * i;

      }

     

      return result; 

}

int main()

{

     int num = 3;

    

     printf(“\n ##Factorial Function in C## \n”);

    

     printf(“\n Factorial of %d is %d”, num, factorial1(num));

    

     printf(“\n Factorial of 5 is %d”, factorial2(5));

    

     getch();

     return 0;  

}

Snapshot of the o/p

FactorialC

A Simple C Program

Hello Friends,

This is a very simple C Program demonstrating input, output using printf(), scanf() functions in C. It also shows how to write some simple functions in C without any argument or return type.

Level- Beginner

/*

  Program demonstrating Simple Functions, input, output in C

Hello.c

*/

 

#include<stdio.h>

#include<conio.h>

 

void hello_message()

{

     printf(“\n Hello World!! :) \n”); 

}

 

void bye_message()

{

     printf(“\n Good Bye World!!!\n”); 

}

 

void main()

{

     int num, a, b, c;

     a=5;

     b=7;

     printf(“\n ~~~Welcome to the world of C~~~ \n”);

     /*

     This is not allowed gives error

     printf(“\n Hello

    

     “);*/

    

     hello_message();

    

     printf(“\n Enter a Number : “);

    

     scanf(“%d”, &num);

    

     printf(“\n You entered %d”, num);

    

     printf(“\n c = %d (Garbage Value)”, c); //displays garbage value

     c=9;

     printf(“\n c = %d”, c);//o/p – 9

     printf(“\n a = %d”, a); //o/p – 5

    

     printf(“\n Experiments with printf \n”);

    

     printf(“\n number = %d”, 3); // o/p- 3

     printf(“\n a+b = %d”, a+b); //o/p -12

     printf(“\n a*b+c = %d”, a*b+c); //o/p-44

    

     printf(“\n Experiments with scanf \n”);

    

     printf(“\n Enter three Numbers : “);

     scanf(“%d %d %d”, &a, &b, &c);

     printf(“\n a = %d, b = %d, c = %d “, a,b,c);

     

     printf(“\n Enter three Numbers (separated by commas) : “);

     scanf(“%d, %d, %d”, &a, &b, &c);

     printf(“\n a = %d, b = %d, c = %d “, a,b,c);

    

     bye_message();

     getch();   

}

 Snapshot of the o/p

Simple Program O/P