Find the Factorial of a Number: C

This video tutorial illustrates finding factorial of a given number using C programming.

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Ex:
if user enters 5, then the factorial is 1 * 2 * 3 * 4 * 5 i.e., factorial = 120
This logic must be handled with a c program.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include < stdio.h >
#include < conio.h >
 
void main()
{
int num, i, fact = 1;
clrscr();
 
printf("Enter a number\n");
scanf("%d", &num);
 
for( i = 1; i < = num; i++ )
 fact = fact * i;                // fact *= i;
 
printf("\nFactorial of %d is %d", num, fact);
 
getch();
 
}

Here the for loop stars from 1 and not 0. As anything multiplied by 0 would also be zero.

Note: Factorial of 0 is 1. i.e., 0! = 1

factorial-cpp


fact *= i; is the compact representation of fact = fact * i;

Video Tutorial: Factorial in c


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

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



Output:
Enter a number
5
Factorial of 5 is 120

Also Watch/Read: Find the Factorial of a Number: C++

Find Sum of Digits In A Given Number: C

Video Tutorial to illustrate, C Program to find sum of digits in the given/user entered integer number.

In this program we assign variable sum = 0 to avoid garbage values in sum before the calculation, which would result in wrong output.
We store the user entered value in num.

1
2
3
4
5
6
 while( num )
  {
    rem = num % 10;
    sum = sum + rem;
    num = num / 10;
  }

Here the loop executes until the value of num is zero.

If user enters 123, we apply the modulus to get the individual values.
Ex:
123 % 10 = 3
12 % 10 = 2
1 % 10 = 1

We get 123, 12 and 1 by dividing the original value by 10.
Ex:
123 user entered value.
123 / 10 = 12
12 / 10 = 1

Now the sum.

sum = sum + rem;

3 = 0 + 3
5 = 3 + 2
6 = 5 + 1

So the sum of digits in the given integer number is 6.

Video Tutorial: Find Sum of Digits In A Given Number: C


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

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



Full Free Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include < stdio.h >
#include < conio.h >
 
void main()
{
int num, rem, sum = 0;
clrscr();
 
printf("Enter an integer number\n");
scanf("%d", &num);
 
while(num)
{
  rem = num % 10;
  sum = sum + rem;
  num = num / 10;
}
 
printf("\nThe sum of digits is %d", sum);
getch();
}

Output:
Enter an integer number
123
The sum of digits is 6

Find Given Number Is Prime or Not: C

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

A natural number greater than 1 that is not a prime number is called a composite number. For example, 7 is prime, as only 1 and 7 divide it, whereas 10 is composite, since it has the divisors 2 and 5 in addition to 1 and 10.

In this video tutorial we show you 3 methods of finding whether the user entered number is a prime number or not.

Since every number is divisible by 1, we start the division(in for loop) from 2.

Note: A number can not be divided(to get a whole number) by another number which is greater than itself.

First Method:
Since every number is divisible by 1, we start the division(in for loop) from 2 till one number less than the user entered value.
Example: If user entered 10. For loop starts from 2 to 9.
for(i=2; i<10; i++) or for(i=2; i< =9; i++)

But the draw back: If user enters 500, the loop must get executed from 2 till 499. i.e., 497 times.

Second Method:
Here we reduce the number of iterations in for loop.
i.e., we divide the user entered value by 2 and divide the user entered value from 2 till num / 2;
Example: If user entered 500, we start the division from 2 till 500/2 i.e., till 250 times.

Third Method:
Here we still reduce the number of iterations in the for loop.
i.e., we take the square root of the user entered number and divide the user entered number from 2 till square root of number.
Example: If user entered 500, we start the division from 2 till sqrt(500) i.e., till 22 times.

So this third method is most optimum, as it involves least number of looping.

Video Tutorial: Find Given Number Is Prime or Not: C


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

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


Full Free Source Code:

#include < stdio .h >
#include < conio .h >
#include < math .h >
 
void main()
{
int num, i, fact, inum;
clrscr();
 
printf("Enter a number\n");
scanf("%d", &num);
 
inum = sqrt(num);
 
for(i=2; i <= inum ; i++)
 if( num % i == 0 )
 {
   fact = 0;
   break;
 }
 else
   fact = 1;
 
if(fact)
 printf("%d is Prime\n", num);
else
 printf("%d is NOT prime\n", num);
 
getch();
}

Table of all prime numbers up to 1,000:

Biggest of 2 Numbers Using Function: C++

Find the biggest of two numbers using function and ternary operator.

Basics of functions:

A function is a sub program to perform a specific task.
OR
a group of instructions to perform specific task.

Along with main() it is possible to define our own functions by following these steps:
1. Function prototype or Function declaration.
2. Function call.
3. Function definition.

Function Prototype:

Giving information about the function such as return type, function name and arguments type to the compiler is known as function prototype; It is written at the declaration section.
Syntax:

< return_type > < function_name >( arguments_type );

Example:
int findsum(int a, int b);
float findsum(int a, int b);
void findsum(int a, int b);
int findsum(int a[], int size);
int findsum(int, int);

Function Definition:
Writing the actual code of the function in a block. It is at this stage the task of the function is defined. It is written after the main function.
Syntax:

< return_type >< function_name >(parameters)
{
 
}

Example:

int findsum(int a, int b)
{
 int sum;
 
 sum = a + b;
 return(sum);
}

Function Call:
It is a technique used to invoke a function.
Syntax:

[variable] < function_name >([arguments]);

Example:
res = findsum(10, 20);
res = findsum(x, y);
res = findsum();

Full Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include< iostream.h >
#include< conio.h >
 
void main()
{
  int Big(int x,int y);  // Prototype
 
  int a, b;
  clrscr();
 
  cout< <"Enter 2 numbers\n";
  cin>>a>>b;
 
  int res = Big(a, b);   // Function call
 
  cout< <"Biggest = "<<res;
  getch();
}
 
int Big(int x, int y)   // Function Definition
{
  return( x>y?x:y );
}

You must also watch these videos, before continuing with this program:
Find Biggest of 2 Numbers: C++
Biggest of Two Numbers Using Ternary Operator: C++

Video Tutorial: Biggest of 2 Numbers Using Function


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

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



Output:
Enter 2 numbers
420
305
Biggest = 420

(Basic) Find Sum Using Dynamic Memory Allocation: C++

This video tutorial illustrates basics of Dynamic memory allocation in C++. It shows the use of new and delete operator for allocating and deallocating the memory dynamically.

Find the sum of entered elements using dynamic memory allocation in c++.

In cpp, dynamic memory management can be done using the operators new and delete.
Operator new is used to allocate the memory during execution time or run time, the dynamically allocated memory can be freed / released using the operator delete.
Syntax:

< data_type >  < pointer_variable > = new < data_type >[size];

Full Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include< iostream .h>
#include< conio .h>
 
void main()
{
  int sum=0, N;
  clrscr();
 
  cout< <"Enter array size\n";
  cin>>N;
 
  int *a = new int[N];
  cout< <"\nEnter "<<N<<" integer numbers"<<endl;
  for(int i=0; i<N; i++)
   cin>>a[i];
 
   cout< <"Input array is.."<<endl;
   for(i=0; i<N; i++)
   {
    cout<<a[i]<<endl;
    sum = sum + a[i]; // sum += a[i];
   }
   cout<<"Total Sum: "<<sum;
 
   delete(a);
   getch();
}

We need not include any extra header file to perform dynamic memory allocation or de-allocation.

int *ptr = new int[N];

here it is mandatory to take pointer variable for dynamic memory allocation.

for de-allocation we use delete operator: delete(ptr);

NOTE:
Student *p = new Student[3];

where Student is a user defined data type, maybe structure or class.

Student *p = new Student; // This is for only 1 student.

Video Tutorial:(Basic) Find Sum Using Dynamic Memory Allocation


[youtube https://www.youtube.com/watch?v=Iqqfa-UjONo]

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



Output:
Enter array size
5
Enter 5 integer numbers
1
2
3
4
5
Input array is..
1
2
3
4
5
Total Sum: 15