Showing posts with label Factorial Program in C. Show all posts
Showing posts with label Factorial Program in C. Show all posts
Sum of digits program in C

Sum of digits program in C

Sum of digits program in C With Example

C program to sum each digit: Let's Make a program in C language to the sum of digits program with the help of loop and mathematical operation.

Steps to design algorithm sum of Integers

To get sum of each digits by c program, use the following algorithm:
  • Step 1: Take number by user as Input
  • Step 2: Get the modulus/remainder of the number
  • Step 3: sum the remainder of the number
  • Step 4: Divide the number by 10
  • Step 5: Repeat the step 2 while number is greater than 0.
Let's see the sum of digits program in C.

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main()  
  4. {  
  5. int n,sum=0,m;  
  6. clrscr();  
  7. printf("Enter a number:");  
  8. scanf("%d",&n);  
  9. while(n>0)  
  10. {  
  11. m=n%10;  
  12. sum=sum+m;  
  13. n=n/10;  
  14. }  
  15. printf("Sum is=%d",sum);  
  16. getch();  
  17. }     

Output:

Enter a number:654
Sum is=15

Enter a number:123
Sum is=6

Armstrong Number in C

Armstrong Number in C Language

Before writing a program in C language to check whether the number is Armstrong or not, First we will understand what is Armstrong number.

What is the Armstrong Number

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.

Note: there are six Armstrong numbers in the range of 0 and 999.

Armstrong number 1: 0
Armstrong number 2: 1
Armstrong number 3: 153
Armstrong number 4: 370
Armstrong number 5: 371
Armstrong number 6: 407


Let's try to understand why 153 is an Armstrong number.



Let's try to understand why 371 is an Armstrong number.

Let's see the c program to check any number provided by user is Armstrong Number or not

Output:
enter the number=153
armstrong number
enter the number=5
not armstrong number
Factorial Program in C

Factorial Program in C

Factorial Program in C

Factorial Program in C: Factorial of n is the result of all positive diving numbers. Factorial of n is indicated by n!. For Example:

  1. 5! = 5*4*3*2*1 = 120  
  2. 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.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4.   int i,fact=1,number;  
  5.   clrscr();  
  6.   printf("Enter a number: ");  
  7.   scanf("%d",&number);  
  8.   
  9.   for(i=1;i<=number;i++){  
  10.       fact=fact*i;  
  11.   }  
  12.   printf("Factorial of %d is: %d",number,fact);  
  13.   getch();  
  14. }  
Output:
Enter a number: 5
Factorial of 5 is: 120

Factorial Program using recursion in C

Let's see the factorial program in c using recursion.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3.   
  4. long factorial(int n)  
  5. {  
  6.   if (n == 0)  
  7.     return 1;  
  8.   else  
  9.     return(n * factorial(n-1));  
  10. }  
  11.    
  12. void main()  
  13. {  
  14.   int number;  
  15.   long fact;  
  16.   clrscr();  
  17.   printf("Enter a number: ");  
  18.   scanf("%d", &number);   
  19.    
  20.   fact = factorial(number);  
  21.   printf("Factorial of %d is %ld\n", number, fact);  
  22.   getch();  
  23. }  
Output:
Enter a number: 6
Factorial of 5 is: 720

Popular Posts