C Program To Find Factorial of a Number using For Loop

Write a C program to find Factorial of a user input number, using for loop.

Related Read:
For Loop In C Programming Language
C Program To Find Factorial of a Number using While Loop

Example:
Factorial of 5 is 120 (1 x 2 x 3 x 4 x 5 = 120).

In general, n objects can be arranged in n(n – 1)(n – 2) … (3)(2)(1) ways. This product is represented by the symbol n!, which is called n factorial. By convention, 0! = 1.

Source Code: C Program To Find Factorial of a Number using For Loop

 
#include<stdio.h>
int main()
{
    int num, count, fact = 1;

    printf("Enter a number to find its Factorial\n");
    scanf("%d", &num);

    for(count = 1; count <= num; count++)
    {
        fact = fact * count;
    }

    printf("Factorial of %d is %d\n", num, fact);

    return 0;
}

Output 1:
Enter a number to find its Factorial
5
Factorial of 5 is 120

Output 2:
Enter a number to find its Factorial
4
Factorial of 4 is 24

Output 3:
Enter a number to find its Factorial
3
Factorial of 3 is 6

Output 4:
Enter a number to find its Factorial
6
Factorial of 6 is 720

Output 5:
Enter a number to find its Factorial
0
Factorial of 0 is 1

C Program To Find Factorial of a Number using For Loop


[youtube https://www.youtube.com/watch?v=0YENXAwrlSI]

YouTube Link: https://www.youtube.com/watch?v=0YENXAwrlSI [Watch the Video In Full Screen.]


Logic To Find Factorial of a Number using For Loop

If user enters num as 4. Then here are the values of variable count and fact for each iteration of for loop.

Iteration 1
count = 1;
fact = 1;

fact = fact * count;
fact = 1 * 1;

count = 2; // value of count increments by 1 for each iteration.
fact = 1;

Iteration 2
count = 2;
fact = 1;

fact = fact * count;
fact = 1 * 2;

count = 3; // value of count increments by 1 for each iteration.
fact = 2;

Iteration 3
count = 3;
fact = 2;

fact = fact * count;
fact = 2 * 3;

count = 4; // value of count increments by 1 for each iteration.
fact = 6;

Iteration 4
count = 4;
fact = 6;

fact = fact * count;
fact = 6 * 4;

count = 5; // value of count increments by 1 for each iteration.
fact = 24;

Now the value of count is 5, which is greater than the user input number 4. So the control exits for loop. We print the value present inside variable fact as the Factorial of the number. So in this case, 24 is the Factorial of number 4.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Factorial of a Number

Write a C program to find Factorial of a user input number, using while loop.

Related Read:
while loop in C programming
Assignment Operators in C

Factorial of a number is the product of all the numbers preceding it. For example, Factorial of 5 is 120 (1 x 2 x 3 x 4 x 5 = 120).

In general, n objects can be arranged in n(n – 1)(n – 2) … (3)(2)(1) ways. This product is represented by the symbol n!, which is called n factorial. By convention, 0! = 1.

Source Code: C Program To Find Factorial of a Number

 
#include<stdio.h>

int main()
{
    long int num, count = 1, fact = 1;

    printf("Enter a number to find factorial\n");
    scanf("%d", &num);

    while(count <= num)
    {
        fact = fact * count; // fact *= count;
        count++;
    }

    printf("Factorial of %d is %d\n", num, fact);

    return 0;
}

Output 1:
Enter a number to find factorial
4
Factorial of 4 is 24

Output 2:
Enter a number to find factorial
5
Factorial of 5 is 120

Output 3:
Enter a number to find factorial
6
Factorial of 6 is 720

Output 4:
Enter a number to find factorial
8
Factorial of 8 is 40320

Output 5:
Enter a number to find factorial
10
Factorial of 10 is 3628800

C Program To Find Factorial of a Number using While Loop


[youtube https://www.youtube.com/watch?v=43RdmXXcr6Y]

YouTube Link: https://www.youtube.com/watch?v=43RdmXXcr6Y [Watch the Video In Full Screen.]


Logic To Find Factorial of a Number

If user enters num as 4. Then here are the values of variable count and fact for each iteration of while loop.

Iteration 1
count = 1;
fact = 1;

fact = fact * count;
fact = 1 * 1;

count = 2; // value of count increments by 1 for each iteration.
fact = 1;

Iteration 2
count = 2;
fact = 1;

fact = fact * count;
fact = 1 * 2;

count = 3; // value of count increments by 1 for each iteration.
fact = 2;

Iteration 3
count = 3;
fact = 2;

fact = fact * count;
fact = 2 * 3;

count = 4; // value of count increments by 1 for each iteration.
fact = 6;

Iteration 4
count = 4;
fact = 6;

fact = fact * count;
fact = 6 * 4;

count = 5; // value of count increments by 1 for each iteration.
fact = 24;

Now the value of count is 5, which is greater than the user input number 4. So the control exits while loop. We print the value present inside variable fact as the Factorial of the number. So in this case, 24 is the Factorial of number 4.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Mixins in Jade: Node.js

Mixins in Jade are much like functions/methods in many other programming languages.

mixin-jade-express-nodejs

Basically, if you’re repeating your code at many places, you could place that code inside a mixin and replace your code with the mixin call. This would make your code cleaner and maintainable over long run.

Related Read: Loops and Conditions In Jade: Node.js

mixins in Jade
mixin.jade

1
2
3
4
mixin fetch(users)
 ul
 -each user in users
  li= user

This would get the users as argument – loop through each user and put the individual user in the list item of an unordered list.

include in index file: Jade
index.jade

1
2
3
4
5
6
7
8
9
include mixin
 
- users = ["Apple", "IBM"];
mixin fetch(users)
 
<br /><br />
 
- users = ["Google", "Amazon"];
mixin fetch(users)

Here we have included the mixin.jade file, and now use our fetch mixin and pass-in our array variable, which is converted into an unordered list of items and output on the browser.

Mixins in Jade: Node.js


[youtube https://www.youtube.com/watch?v=g6DPkVpfauc]

YouTube Link: https://www.youtube.com/watch?v=g6DPkVpfauc [Watch the Video In Full Screen.]



Note: Clearly, in real-time applications the array or object come from the database.
Example: You might encounter the situation to list out all the users in your list and list of all the participating companies. Or list of all companies someone has worked at. In such cases you’ll get an array of objects which you need to out put to the user in a well formatted manner. In situations like this, mixins are a great tool to have, to avoid the necesity to repeat your code allover your application.

Have all your mixins in a single file and then include that file wherever you are calling your mixins.

Build VOTE / LIKE, UNLIKE Feature: PHP & MySQL

In Voting system we need to make sure, one person votes only once, to a particular poll. Or One person can only like a particular image once.
Person who votes must be allowed to vote down and the person who likes must be allowed to unlike the same image / item etc.

Looks complicated? Relax, its not that complicated!

Let me explain the logic and direct you to some videos..
First, build your User Signup and Login forms.
Make username as primary key.

Now create another table with two fields. id(primary key and auto_increment) and image.

Now another table with two fields again: username and id
Make username and id as primary keys..that is, composite primary key.

In database design, a composite key is a key that consists of 2 or more attributes that uniquely identify an entity occurrence. Each attribute that makes up the compound key is a simple key in its own right.

This way, same user can not like / vote for the same image twice! It would throw error, as it violets composite key rule. You could make use of Die() and show appropriate message or still better use if else and if the query has not executed(due to violation of composite key rule) show links in the else part to vote down or unlike. There you could simply delete the entry from vote table.

Video Tutorial: Build VOTE / LIKE, UNLIKE Feature: PHP & MySQL


[youtube https://www.youtube.com/watch?v=XZ6hBLNayr0]

YouTube Link: https://www.youtube.com/watch?v=XZ6hBLNayr0 [Watch the Video In Full Screen.]



Take 3 tables: user, image and vote
username is primary key in user table.
id is primary key in image table. attributes: id and image
id and username are composite primary key in vote table.

Vote / Like, Unlike

1
mysql> INSERT INTO vote VALUES( ‘2’, ’apple’ );

Count Vote / Like

1
mysql> SELECT count(*) FROM vote WHERE id= ‘2’;

Vote down / Unlike

1
mysql> DELETE FROM vote WHERE( id=‘2’ AND username=’apple’ );

Make sure to take the values of id and username dynamically. Am showing 2 and apple for the sake of illustration.
username will be present inside the session variable once the user logs in.
id value will be associated with the like or vote link/button of the image. [id is unique to each individual image]

Taking advantage of the error thrown by the composite primary key. i.e., using if else we check if the user has already voted or liked particular image or participated in a particular poll, if yes, we show him/her unlike or vote down link/button.

Vote, Vote Down or Like, Unlike Feature

1
2
3
4
If( mysql_query(“INSERT INTO vote VALUES( ‘2’, ’apple’ )”)  )
  echo “Thanks for the vote”;
else
  echo “<a href=‘vote.php?down=1’>Vote Down</a>”;

You could mix this simple logic with some CSS and other stuffs like AJAX and implement the voting system easily in a short period of time, like a pro!