Greetings readers.This is part 2 of basic c programs series which is continuation of part1. In the part two of the series we will look at factorial, swapping of two numbers(with andd without tempoarary variables) ,prime number or not

### 4.Factorial

• Factorial is the product of integers less than or equal to n.

• For eg :

$5 ! = 5 \times 4 \times 3 \times 2 \times 1 = 120 \$
• Here we declare 3 variables i,n,fact with fact initialised to 1.

• We recieve the range from the user in n.

• We open a for loop from i value 1 to less than or equal to n.

• Inside the for loop we multiplly the value of fact to i which is initially set to 1.

• when the value of i increases the fact value changes and the loop is stopped when it reaches n.

• The factorial is stored in the fact variable which is printed out after the termination of loop.

• The Snippet is given below:

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

int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(num==1)
printf("\n The number is neither prime nor composite");
else if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
&#91;/c&#93;

<h3><strong><span style="font-family: lucida sans unicode,lucida grande,sans-serif;"><span style="font-size: 20px;">5.Swapping of two Numbers (with and without temporary variable)</span></span></strong></h3>
<ul>
<li>
<p><span style="font-family: lucida sans unicode,lucida grande,sans-serif;"><span style="font-size: 14px;">Mutually exchanging the value of two variables with data in memory.</span></span></p>
</li>
<li>
<p><span style="font-family: lucida sans unicode,lucida grande,sans-serif;"><span style="font-size: 14px;">We have two functions swap1 and swap2.</span></span></p>
</li>
<li>
<p><span style="font-family: lucida sans unicode,lucida grande,sans-serif;"><span style="font-size: 14px;">swap1 swaps it using temporary variables the value of a is moved to a then b to a finally t to b.</span></span></p>
</li>
<li>
<p><span style="font-family: lucida sans unicode,lucida grande,sans-serif;"><span style="font-size: 14px;">The swapped values are printed.</span></span></p>
</li>
<li>
<p><span style="font-family: lucida sans unicode,lucida grande,sans-serif;"><span style="font-size: 14px;">In swap2 the values are moved to variables a1 and b1.</span></span></p>
</li>
<li>
<p><span style="font-family: lucida sans unicode,lucida grande,sans-serif;"><span style="font-size: 14px;">The values a1 and b1 are added and stored in a1.</span></span></p>
</li>
<li>
<p><span style="font-family: lucida sans unicode,lucida grande,sans-serif;"><span style="font-size: 14px;">The added value is subtracted with b1 and assigned to b1(b1=a1-b1)</span></span></p>
</li>
</ul>
<ul>
<li>
<p><span style="font-family: lucida sans unicode,lucida grande,sans-serif;"><span style="font-size: 14px;">The a1 value is subtracted again with b1 and stored in a1 so the final values are swapped.(a1=a1-b1) </span></span></p>
</li>
</ul>
<ul>
<li>
<p><span style="font-family: lucida sans unicode,lucida grande,sans-serif;"><span style="font-size: 14px;">The Snippet is given below:</span></span></p>
</li>
</ul>

#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,y;
printf("\n Enter the value of A and B:");
scanf("%d %d",&x,&y);
swap1(x,y);
swap2(x,y);
getch();

}
void swap1(int a,int b) //swapping using temporary variable
{
int t;
t=a;
a=b;
b=t;
printf("\n The value of A and b after Swapping:%d %d",a,b);
}
void swap2(int a1,int b1) //swapping without using temporary variable
{
a1=a1+b1;
b1=a1-b1;
a1=a1-b1;
printf("\nThe value of A and B after swapping:%d %d",a1,b1);
}


### 6.Prime Number or Not

• Prime Number is a number which has no divisors other than 1 and itself.

• 1 is neither prime nor composite.

• We get input from the user and store it in num.

• we loop from i =2 since to the num divided by (num/2)

• We check for divisbility test using the mod opeartor (%) which gives the remainder if equals 0 we increment the value of count.

• After the end of loop we have a if to check if it equals 1 to print that its neither prime nor composite.

• In the else if we check if count equals 0 and number not equal to 1..

• swap1 swaps it using temporary variables the value of a is moved to a then b to a finally t to b.

• The swapped values are printed.

• In swap2 the values are moved to variables a1 and b1.

• The values a1 and b1 are added and stored in a1.

• The added value is subtracted with b1 and assigned to b1(b1=a1-b1)

• The a1 value is subtracted again with b1 and stored in a1 so the final values are swapped.(a1=a1-b1)

• The Snippet is given below:

#include
#include

int main(){

int num,i,count=0;
printf(“Enter a number: “);
scanf(“%d”,&num);
for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(num==1) printf("\n The number is neither prime nor composite"); else if(count==0 && num!= 1) printf("%d is a prime number",num); else printf("%d is not a prime number",num); return 0; } [/c]

Note:All above code has been tested in GNU GCC Compiler in Linux.

Also dont forget to Visit our part1 of the Basic C program series

-Regards

Sathesh Bm