C Program For Factorial Of A Number Using For Loop

C Program For Factorial Of A Number Using For Loop.  If you are looking for a factorial program in C with an example, this factorial tutorial will help you to learn how to find the factorial of a number in C language. Just go through this C programming example for factorial, you will be able to write a C program for factorial using for loop.

C Program For Factorial – Table of Contents

What is Factorial?

Factorial is the product of a positive integer (n) and all the integers below of it (n-1). Example, the factorial of positive integer n is ( n! ) is equal to 1*2*3*…*n.

Example: What is the value of 4!

4! = 1*2*3*4 => 24

The value of 4! is 24.

C Program For Factorial

Learn how to write a  C program for factorial. Writing a C program to find factorial can be done using various techniques like using for loop, while loop, pointers, recursion but here in this program, we show how to write a factorial program using for loop in a proper way.

This is the eleventh C programming example in the series, it helps newbies coders, programming students and B.Tech graduates in enhancing their C programming skills and land on their dream job in software industry. All the best guys in learning c programs with coding compiler website. We wish all the success in your career. Happy Learning.

C Programming Tutorials

  1. C Program For Palindrome String
  2. C Program For Palindrome Numbers
  3. C Program To Reverse a String with Using Function
  4. C Program To Reverse a String without Using Function
  5. C Program To Reverse a String Using Recursion

C Program For Factorial Source Code

Copy the below C program to find the factorial of a number source code or write your own logic by using this program as a reference. Paste the factorial program into C compilers and run the program to see the result.

/* C PROGRAM FOR FACTORIAL - FACTORIAL.C */

#include <stdio.h>
int main()
{
 int n, i,factorial = 1; //varialbes declaration

printf("Enter a number: "); //asking your to enter a number
 scanf("%d",&n); //reading entered number

// showing error message, if the entered number is a negative number
 if (n < 0)
 printf("Error! Factorial of a negative number doesn't exist.");

else
 {
   for(i=1; i<=n; ++i) //finding factorial using for loop
   {
      factorial=factorial*i; // calculating factorial of a number
   }
   printf("Factorial of %d = %d", n, factorial);
 }

return 0;
}

C Program For Factorial Output

After you compile and run the above c program for factorial of a number using for loop, your C compiler asks you to enter a number to find factorial. After you enter your number, the program will be executed and give output like below expected output.

Output – 1

Enter a number: 4
Factorial of 4 = 24

Output – 2

Enter a number: -4
Error! Factorial of a negative number doesn’t exist.

C Programming Examples

  1. C Program To Reverse a String Using Pointers
  2. C Program To Swap Two Numbers Using Two Variables
  3. C Program To Swap Two Numbers Using Three Variables
  4. C Program For Prime Numbers – Check  a Number is Prime or Not
  5. C Program To Reverse a String with Using Function
  6. C Program to Reverse a String without Using Function
  7. C Program to Reverse a Sting Using Recursion

Switch Case Program In C With Example Output

Switch Case Program In C With Example – If you are looking for a switch case program in C with an example, this switch case statement tutorial will help you to learn how to write switch case program in C language. Just go through this C program for switch case statement. After going through this C programming tutorial, you will be able to write a switch case program in C.

Switch Case Program in C – Table of Contents

Switch Case Statement

What is switch case statement? A switch case statement is used to test a variable against a list of values. In switch case, each value is called as a case, and the entered variable is called switch. The switched variable is checked against each case value in switch case statement.

Switch Case Statement Syntax in C Programming

Here, you will get Switch Case Syntax in C Programming. This below syntax illustrates how to use switch case statement in C programming to perform arithmetic operations.

//Switch Case Statement Example Syntax in C Programming

switch (n)
​{
 case constant1:
 // logic will be executed if n is equal to constant1;
 break;

case constant2:
 // logic will be executed if n is equal to constant2;
 break;
 .
 .
 .
 default:
 // logic will be executed if n doesn't match any constant
}

C Programming Tutorials

  1. C Program For Palindrome String
  2. C Program For Palindrome Numbers
  3. C Program To Reverse a String with Using Function
  4. C Program To Reverse a String without Using Function
  5. C Program To Reverse a String Using Recursion

Switch Case Program in C

Learn how to write a  Switch case program in C language. Writing a C program for switch case statement can be done using various techniques, but here in this program, we show how to write switch case program in a proper way.

This is the tenth C programming example in the series, it helps newbies, students and B.Tech graduates in enhancing C programming and land on their dream job. All the best guys in learning c programming with coding compiler website. Happy Learning.

Switch Case Program in C Source Code

Copy paste the below switch case program source code or write your own logic into C compilers and run the program to see the result.

/* C PROGRAM FOR SWITCH CASE STATEMENT - SWITCHCASE.C */

#include<stdio.h>
#include<conio.h>
void main()
{
    //variable declaration
    int n1, n2, ch;
    clrscr();
   
    //Asking user to enter a number 
    printf("Enter the first number : ") ;

    //Taking input from user
    scanf("%d", &n1) ;

    //Asking user to enter secpmd number 
    printf("\nEnter the second number : ") ;

    //Taking input from user
    scanf("%d", &n2) ;
    printf("\n[1] -> Addition ") ;
    printf("\n[2] -> Subtraction ") ;
    printf("\n[3] -> Multiplication ") ;
    printf("\n[4] -> Division ") ;

    //Asking your to enter their choice to perform arithmetic operation
    printf("\n\nEnter your choice <1...4> : ") ;
    scanf("%d", &ch) ;

    //Checking user choice against different options in switch case
    switch(ch)
    {
        case 1 :
        printf("\n%d + %d = %d", n1, n2, n1 + n2) ;
        break ;
        case 2 :
        printf("\n%d - %d = %d", n1, n2, n1 - n2) ;
        break ;
        case 3 :
        printf("\n%d * %d = %d", n1, n2, n1 * n2);
        break ;
        case 4 :
        printf("\n%d / %d = %.2f", n1, n2, (float)n1 / n2);
        break ;
        default :
        printf("\nInvalid choice");
        break ;
    }
    getch();
}

Switch Case Program in C OutPut

After you compile and run the above switch case program in C, your C compiler asks you to enter a choice to perform some action using your choice. After you enter your choice, the program will be executed and give output like below example output.

Output – 1

Enter the first number : 11
Enter the second number : 22
[1] -> Addition
[2] -> Subtraction
[3] -> Multiplication
[4] -> Division
Enter your choice <1…4> : 1
11 + 22 = 33

Output – 2

Enter the first number : 15
Enter the second number : 5
[1] -> Addition
[2] -> Subtraction
[3] -> Multiplication
[4] -> Division
Enter your choice <1…4> : 3
15 + 5 = 75

C Programming Examples

  1. C Program To Reverse a String Using Pointers
  2. C Program To Swap Two Numbers Using Two Variables
  3. C Program To Swap Two Numbers Using Three Variables
  4. C Program For Prime Numbers – Check  a Number is Prime or Not
  5. C Program To Reverse a String with Using Function
  6. C Program to Reverse a String without Using Function
  7. C Program to Reverse a Sting Using Recursion

C Program For Palindrome – Check Whether A String Is Palindrome Or Not

C Program For Palindrome String – What is Palindrome? A palindrome is a sequence of characters which reads the same backward as forward. A palindrome could be a word, phrase, number, or other sequences of characters. such as madam or racecar.

 

Palindrome Word Examples:

Madam, Racecar, Radar, Etc.

C Program For Palindrome

If you are looking for a palindrome string in C program, this article will help you to learn how to write palindrome program in C language. Just go through this C program for palindrome to check whether a string is palindrome or not. After going through this C programming tutorial, you will be able to write a  C palindrome program.

C Programming Tutorials

C Program For Palindrome Numbers

C Program To Reverse a String with Using Function

C Program To Reverse a String without Using Function

C Program To Reverse a String Using Recursion

C Program To Reverse a String Using Pointers

C Program For Palindrome String

Learn how to write a  Palindrome program in C language. Writing a C program to check whether a string is a palindrome or not can be done using various techniques, but here in this program, we show how to write palindrome program in a proper way.

This is the eighth C programming example in the series, it helps newbies, students and B.Tech graduates in enhancing C programming and land on their dream job. All the best guys in learning c programming with coding compiler website. Happy Learning.

C Program For Palindrome Source Code

Copy paste the below source code or write your own logic into C compilers and run the program to see the result.

#include <stdio.h> 

#include <string.h> 

int main()

{ 

//Variables declaration

   char string1[20]; 

   int i, length; 

   int flag = 0; 

//Reading a string from user

   printf("Enter a string:"); 

   scanf("%s", string1); 

   length = strlen(string1); 

//checking whether a string is palindrome or not

   for(i=0;i < length ;i++)

   { 

      if(string1[i] != string1[length-i-1])

      { flag = 1; break; } 

   } 

   if (flag) 

   {

      printf("%s is not a palindrome", string1); 

   } 

   else 

  { 

     printf("%s is a palindrome", string1); 

  }

   return 0; 

}

C PROGRAM FOR PALINDROME OUTPUT

After you compile and run the above program, your C compiler asks you to enter a string to check whether entered string is palindrome or not. After you enter a string, the programming logic will check and show result like below expected output.

Example-1

Enter a string:

Radar

Radar is a palindrome

Example-2

Enter a string:

King

King is not a palindrome

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program For Prime Numbers – Check  a Number is Prime or Not

C Program To Reverse a String with Using Function

C Program to Reverse a String without Using Function

C Program to Reverse a Sting Using Recursion

C Program For Palindrome – Check Whether A Number Is Palindrome Or Not

C Program For Palindrome – What is Palindrome? A palindrome is a sequence of characters which reads the same backward as forward. A palindrome could be a word, phrase, number, or other sequences of characters. such as madam or racecar.

Palindrome Number Examples:

1001, 2552, 32423, 959, Etc.

Palindrome Word Examples:

Madam, Racecar, Etc.

C Program For Palindrome

If you are looking for a palindrome in C program, this article will help you to learn how to write palindrome program in C language. Just go through this C program for palindrome to check whether a number is palindrome or not. After going through this C programming tutorial, you will be able to write a  C palindrome program.

C Programming Tutorials

C Program For Palindrome String

C Program To Reverse a String with Using Function

C Program To Reverse a String without Using Function

C Program To Reverse a String Using Recursion

C Program To Reverse a String Using Pointers

C Program For Palindrome Numbers

Learn how to write a  Palindrome program in C language. Writing a C program to check whether a number is a palindrome or not can be done using various techniques, but here in this program, we show how to write palindrome program in a proper way.

This is the seventh C programming example in the series, it helps newbies, students and B.Tech graduates in enhancing C programming and land on their dream job. All the best guys in learning c programming with coding compiler website. Happy Learning.

C Program For Palindrome Source Code

Copy paste the below source code or write your own logic into C compilers and run the program to see the result.

/* C PROGRAM FOR PALINDROME - PALINDROME.C */

#include <stdio.h>
int main()
{

//Variables declaration

 int n, reversedInteger = 0, remainder, originalInteger;

//Taking input number from user

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

originalInteger = n;

// reversed integer is stored in "reversedInteger" variable
 
 while( n!=0 )
 {
 remainder = n%10;
 reversedInteger = reversedInteger*10 + remainder;
 n /= 10;
 }

// palindrome logic
// if orignalInteger and reversedInteger are equal
// then it is a palindrome

 if (originalInteger == reversedInteger)
 printf(" Entered Number %d is a Palindrome.", originalInteger);
 else
 printf(" Entered Number %d is Not a Palindrome.", originalInteger);
 
 return 0;
}

C PROGRAM For Palindrome OUTPUT

After you compile and run the above program, your C compiler asks you to enter a number to check whether a number is palindrome or not. After you enter a number, the programming logic will check and show result like below expected output.

Enter a number:

1001

Entered Number 1001 is a Palindrome.

 

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program For Prime Numbers – Check  a Number is Prime or Not

C Program To Reverse a String with Using Function

C Program to Reverse a String without Using Function

C Program to Reverse a Sting Using Recursion

C Program To Reverse a String Using Pointers

C Program To Reverse a String Using Pointers – If you are looking for a program for reversing a string in C program, this article will help you to learn how to reverse a string in C using pointers. Just go through this reverse string tutorial you will be able to write a  C program to reverse a string.

C Programming Tutorials

CodingCompiler.com presenting series of C programming tutorials for beginners, experienced to advanced learners. These programming tutorials help C learners, students, B.Tech graduates to enhance their C programming skills and hands-on experience.

C Program To Reverse a String with Using Function

C Program To Reverse a String without Using Function

C Program To Reverse a String Using Recursion

This is the sixth C programming example in the series, it helps newbies in enhancing C programming and land on their dream job. All the best guys in learning c programming with coding compiler website.

C Program to Reverse a String Using Pointers

Learn how to reverse a string in C programming in this tutorial. Reversing a string in C programming language can be done using various techniques, but here in this program, we show how to reverse a string using pointers.

C Program To Reverse a String Source Code

Copy paste the below source code or write your own logic into C compilers and run the program to see the result.

/* C PROGRAM TO REVERSE a STRING - REVERSESTRINGRECURSION.C */

#include<stdio.h>
 
int string_length(char*); //Pointers
void reverse(char*);
 
main() 
{
   char string[100];
 
   printf("Enter a string\n");
   gets(string);  //Input taking from user
 
   reverse(string);
 
   printf("Reverse of entered string is \"%s\".\n", string);
 
   return 0;
}
//Pointers Logic to reverse a string 
void reverse(char *string) 
{
   int length, c;
   char *begin, *end, temp;
 
   length = string_length(string);
   begin  = string;
   end    = string;
 
   for (c = 0; c < length - 1; c++)
      end++;
 
   for (c = 0; c < length/2; c++)
   {        
      temp   = *end;
      *end   = *begin;
      *begin = temp;
 
      begin++;
      end--;
   }
}
 
int string_length(char *pointer)
{
   int c = 0;
 
   while( *(pointer + c) != '\0' )
      c++;
 
   return c;
}

C Program To Reverse a String Output

After you compile and run the above program, your C compiler asks you to enter a string to reverse. After you enter a string, the recursive function will reverse the string and show output like below expected output.

Enter a string

programming

Reverse of entered string is

gnimmargorp

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program For Prime Numbers – Check  a Number is Prime or Not

C Program To Reverse a String with Using Function

C Program to Reverse a String without Using Function

C Program to Reverse a Sting Using Recursion

C Program to Reverse a String Using Recursion

C Program to Reverse a Sting Using Recursion –  If you are looking for reversing a string in C program, this article will help you to guide how to reverse a string in C using recursion. Just go through this reverse string tutorial you will be able to write a  C program to reverse a string.

C Programming Tutorials

CodingCompiler.com presenting series of C programming tutorials for beginners, experienced to advanced learners. These programming tutorials help C learners, students, B.Tech graduates to enhance their C programming skills and hands-on experience.

C Program To Reverse a String with Using Function

C Program To Reverse a String without Using Function

C Program To Reverse a String Using Pointers

This is the sixth C programming example in the series, it helps newbies in enhancing C programming and land on their dream job. All the best guys in learning c programming with coding compiler website.

C Program to Reverse a String Using Recursion

Learn how to reverse a string in C programming in this tutorial. Reversing a string in C programming language can be done using various techniques, but here in this program, we show how to reverse a string using recursion.

C Program To Reverse a String Source Code

Copy paste the below source code or write your own logic into C compilers and run the program to see the result.

/* C PROGRAM TO REVERSE a STRING - REVERSESTRINGRECURSION.C */

#include <stdio.h>
#include <string.h>
 
void reverse(char*, int, int); //Method
 
int main()
{
   char a[100];  //Variables
 
   gets(a);
 
   reverse(a, 0, strlen(a)-1);
 
   printf("%s\n",a);
 
   return 0;
}
 // Logic for reversing a string
void reverse(char *x, int begin, int end) //Recursion
{
   char c;
 
   if (begin >= end)
      return;
 
   c          = *(x+begin);
   *(x+begin) = *(x+end);
   *(x+end)   = c;
 
   reverse(x, ++begin, --end);
}

C Program To Reverse a String Output

After you compile and run the above program, your C compiler asks you to enter a string to reverse. After you enter a string, recursive function will reverse the string and show output like below expected output.

Input a string

programming

Reverse of entered string is

gnimmargorp

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program For Prime Numbers – Check  a Number is Prime or Not

C Program To Reverse a String with Using Function

C Program to Reverse a String without Using Function

C Program to Reverse a String without Using Function

C Program to Reverse a String without Using Function – If you are searching for reversing a string in C program, this article will help you to learn how to reverse a string in C without using strrev() function. Just go through this reverse string tutorial you will be able to write a  C program to reverse a string.

C Programming Tutorials

CodingCompiler.com publishing series of C programming tutorials for beginners to advanced learners. These programming tutorials help C learners, students, B.Tech graduates to enhance their C programming skills and hands-on experience on coding.

C Program To Reverse a String with Using Function

C Program To Reverse a String Using Pointers

C Program To Reverse a Sting Using Recursion

This is the fifth C programming example in the series, it helps newbies in learning C programming and land on their dream job. All the best guys in learning c programming with coding compiler website.

C Program To Reverse a String Without Using Function

Learn how to reverse a string in C programming in this tutorial. Reversing a string in C programming language can be done using various techniques, but here in this program, we show without using strrev() function to reverse a string.

C Program To Reverse a String Source Code

Copy paste the below source code or write your own logic into C compilers and run the program to see the result.

/* C PROGRAM TO REVERSE a STRING - REVERSESTRING.C */

#include <stdio.h>
#include <string.h>
 
int main()
{
   char s[100], r[100]; //variables declaration
   int n, c, d;         //Integer variables
 
   printf("Input a string\n");
   gets(s);             //Getting input string from user
 
   n = strlen(s);     
   //Logic for reversing a string without using function
   for (c = n - 1, d = 0; c >= 0; c--, d++)
      r[d] = s[c];
 
   r[d] = '\0';
 
   printf("%s\n", r);
 
   return 0;
}

C Program To Reverse a String Output

After you compile and run the above program, your C compiler asks you to enter a string to reverse. After you enter a string, programming llogic will reverse the string and show output like below expected output.

Input a string

programming

Reverse of entered string is

gnimmargorp

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program For Prime Numbers – Check  a Number is Prime or Not

C Program To Reverse a String with Using Function

C Program To Reverse a String Using STRREV Function

C Program To Reverse a String – If you are looking for reversing a string in C program, this article will guide you to learn how to reverse a string in C. Just go through this reverse string tutorial you will be able to write a program to reverse sting.

CodingCompiler.com publishing series of easy to complex C programs for beginners, students, B.Tech graduates to enhance their C programming skills and hands-on experience on coding. This is the fourth C programming example in the series, it helps newbies in reaching their goals in the career. All the best guys in learning c programming with coding compiler blog.

C Program To Reverse a String

Learn how to reverse a string in C programming. Reversing a string in C programming language can be done using various techniques, but here in this program, we used strrev() function to reverse a string.

C Program To Reverse a String without Using Function

C Program To Reverse a String Using Pointers

C Program To Reverse a Sting Using Recursion

C Program To Reverse a String Source Code

Copy paste the below source code or write your own logic into C compilers and run the program to see the result.

/* C PROGRAM TO REVERSE a STRING - REVERSESTRING.C */
#include <stdio.h>
#include <string.h>
 
int main()
{
   char arr[100];
 
   printf("Enter a string to reverse\n");
   gets(arr); //This function will get a string from user
 
   strrev(arr); //This function will reverse a string
 
   printf("Reverse of entered string is \n%s\n",arr);
 
   return 0;
}

C Program To Reverse a String Output

After you compile and run the above program, your C compiler asks you to enter a string to reverse. After you enter a string, strrev() function will reverse the string and show output like below expected output.

Enter a string to reverse programming

programming

Reverse of entered string is

gnimmargorp

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program For Prime Numbers – Check  a Number is Prime or Not

 

C Program For Prime Numbers – Check Whether A Number Is Prime Or Not

C Program For Prime Numbers – If you are looking for finding prime numbers in C program, this article will guide you to learn to check whether a number is prime or not in C. Just go through this C program to find prime number tutorial you will be able to write a program to check prime number in C.

CodingCompiler.com publishing series of C programs for beginners, students, B.Tech graduates to enhance their C programming skills and hands-on experience on coding. This is the third C programming example in the series, it helps newbies in reaching their goals in the career. All the best guys in learning c programming with coding compiler blog.

C Program For Prime Numbers

Check whether a number is prime or not in C in C programming language can be done using various techniques, but here in this program, we used three variables to find prime numbers.

C Program To Find Prime Number Source Code

Copy paste the below C program to find prime number source code or write your own logic into C compilers and run the program to see the result.

Source Code:

/*C PROGRAM FOR PRIME NUMBER - PRIMENUMBERS.C */

#include <stdio.h>
int main()
{
   int n, i, flag = 0; //Variables

   printf("Enter a positive integer: "); 
   scanf("%d",&n); //Reading input from users

  for(i=2; i<=n/2; ++i) // Logic for finding prime number
  {
    
    if(n%i==0)
     {
      flag=1;
      break;
     }
  }

   if (flag==0)
     printf("%d is a prime number.",n);
   else
     printf("%d is not a prime number.",n);
 
 return 0;
}

C Program For Prime Numbers Output

After you compile and run the above program, your C compiler asks you to enter a number to check whether a number is a prime number or not. After you enter a positive integer, then it will show output with the result saying the entered number is prime or not like below expected output.

Enter a positive integer: 29

29 is a prime number.

Related C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program To Swap Two Numbers Using Three Variables

C Program To Swap Two Numbers Using Three Numbers – If you are looking for swapping C program, this article will guide you to lean swapping two numbers in C using 3 variables. Just go through this swapping tutorial you will able write a program to swap two numbers using three variable a,b and c.

We are publishing series of C programs for beginners, students, B.Tech graduates to enhance their C programming skills and hands-on experience on coding. This is the second C programming example in the series, it helps newbies in reaching their goals in the career. All the best guys in learning c programming with coding compiler blog.

C Program To Swap Three Numbers

Swapping in C programming language can be done using various techniques, like using two variables, three variables, and functions, but here in this swapping program, we used three variables to perform swap two numbers in C.

C Program To Swap Three Numbers Source Code

Copy paste the below source code or write your own logic into C compilers and run the program to see the result.

/* Swapping 2 Using Three Variables In C - SWAPTWONUMBERS.C */

#include<stdio.h>
#include<conio.h>
void main()
{
    int a, b, c ;
    clrscr() ;
    printf("Enter two numbers : ") ;
    scanf("%d %d", &a, &b) ;
    printf("\nBefore swapping : \n\n") ;
    printf("a = %d \t b = %d", a, b) ;

    /* Swapping Two Numbers Logic */
    c = a ;
    a = b ;
    b = c ;
    printf("\n\nAfter swapping : \n\n") ;
    printf("a = %d \t b = %d", a, b) ;
    getch();
}

C Program To Swap Two Numbers Output

After you compile and run the above program, your C compiler asks you to enter two numbers to swap, then it will show output before swapping and after swapping two numbers like below expected output.

Enter two numbers: 10 20

Before swapping :
a = 10 b = 20

After swapping :
a = 20 b = 10

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program To Swap Two Numbers Using Two Variables