Posts

Factorial

Factorial Program  in C: Factorial of n is the  product of all positive descending integers . Factorial of  n  is denoted by n!. For example: 5! = 5*4*3*2*1 = 120   3! = 3*2*1 = 6   Here, 5! is pronounced as "5 factorial", it is also called "5 bang" or "5 shriek". The factorial is normally used in Combinations and Permutations (mathematics). There are many ways to write the factorial program in c language. Let's see the 2 ways to write the factorial program. Factorial Program using loop Factorial Program using recursion Factorial Program using loop Let's see the factorial Program using loop. #include<stdio.h>    int  main()     {       int  i,fact=1,number;      printf( "Enter a number: " );       scanf( "%d" ,&number); ...

Prime Number

Prime number in C:  Prime number  is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17, 19, 23.... are the prime numbers. Note: Zero (0) and 1 are not considered as prime numbers. Two (2) is the only one even prime number because all the numbers can be divided by 2. Let's see the prime number program in C. In this c program, we will take an input from the user and check whether the number is prime or not. #include<stdio.h>    int  main(){     int  n,i,m=0,flag=0;     printf( "Enter the number to check prime:" );     scanf( "%d" ,&n);     m=n/2;     for (i=2;i<=m;i++)     {     if (n%i==0)     {   ...

Fibonacci

Fibonacci Series  in C: In case of fibonacci series,  next number is the sum of previous two numbers  for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1. There are two ways to write the fibonacci series program: Fibonacci Series without recursion Fibonacci Series using recursion Fibonacci Series in C without recursion Let's see the fibonacci series program in c without recursion. #include<stdio.h>      int  main()     {       int  n1=0,n2=1,n3,i,number;      printf( "Enter the number of elements:" );      scanf( "%d" ,&number);      printf( "\n%d %d" ,n1,n2); //printing 0 and 1        for (i=2;i<number;++i) //loop starts from 2 because 0 and...

Switch Statement in C

Image
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable. The syntax of switch statement in c language is given below: switch (expression){     case  value1:       //code to be executed;        break ;   //optional    case  value2:       //code to be executed;        break ;   //optional    ......          default :       code to be executed  if  all cases are not matched;     }  ...