Task 6
Task 6
PROGRAM :
#include <stdio.h>
int main( )
{
int n,i,f;
f=i=1;
printf("Enter a Number to Find Factorial: ");
scanf("%d",&n);
while(i<=n)
{
f*=i;
i++;
}
printf("The Factorial of %d is : %d",n,f);
return 0;
}
Output:
Enter a Number to Find Factorial: 5
The Factorial of 5 is : 120
Result: The given Program to find the factorial of given number using any loop has
successfully executed.
6.2 Find the given number is Prime or not
Aim: To find the given number is a prime or not
Description:
A natural number is said to be prime if it is only divisible by itself and 1. In short,
a prime number has only two factors that are 1 and the number itself. The numbers that
are not prime are called composite numbers.
A prime number can be written as a product of only two numbers. For example,
consider 3. Now, 3 can be written in the form of the product of two numbers in only one
way i.e., 1 * 3. Whereas, 8 which is a composite number can be written as 1 * 8 and 2 * 4.
Program 6.2:
#include <stdio.h>
int main()
{
int i, num, temp = 0;
printf("Enter any numb to Check for Prime: ");
scanf("%d", &num);
for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
temp++;
break;
}
}
if (temp == 0 && num != 1)
{
printf("%d is a Prime number", num);
}
else
{
printf("%d is not a Prime number", num);
}
return 0;
}
Output:
Enter any numb to Check for Prime: 27
27 is not a Prime number
Result: The given program to check whether given number is prime or not has been
executed successfully.
6.3 Compute sine and cos series
For example,
Program :
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n;
float x, sum, t;
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);
x=x*3.14159/180;
t=x;
sum=x;
for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
printf(" The value of Sin(%f) = %.4f",x,sum);
return 0;
}
Output:
Enter the value for x : 60
Enter the value for n : 3
The value of Sin(1.047197) = 0.8660
Description:
Cosine Series:
Cosine Series is a series which is used to find the value of Cos(x). where, x is the
angle in degree which is converted to Radian.
The formula used to express the Cos(x) as Cosine Series is
For example,
Program :
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n;
float x, sum=1, t=1;
printf(" Enter the value for x : ");
scanf("%f",&x);
Output:
Enter the value for x : 60
Enter the value for n : 3
The value of Cos(1.047197) is : 0.5000
Result: The given programs to compute sine and cos series has been executed
successfully.
6.4 checking a number Palindrome
Aim: Checking a number whether given number is palindrome or not.
Desciption:
Palindrome numbers are those numbers which after reversing the digits equals the
original number. For example, 12321 after reversing is 12321, so it is a palindrome
number. But the number 1232 after reversing is 2321, so it is not a palindrome number.
Program:
int main()
{
int org_num,num;
printf("Enter the number to check whether it is palindrome or not:");
scanf("%d",&org_num);
num=org_num;
int reversed = 0;
while (num != 0)
{
int r = num % 10;
reversed = reversed * 10 + r;
num /= 10;
}
if (org_num == reversed)
{
printf(" Given number (%d)and reversed number (%d) are same,so it is a
palindrome number", org_num,reversed);
}
else
{
printf(" Given number (%d) and reversed number(%d) are not same, so it is not
a palindrome number",org_num,reversed);
}
return 0;
}
Output:
Enter the number to check whether it is palindrome or not: 12345
Given number (12345) and reversed number(54321) are not same, so it is not a
palindrome number.
Program:
#include<stdio.h>
int main()
{
int rows,i,j,k;
printf("Number of rows: ");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
// inner loop 1 to print white spaces
for(j=1; j<=2*(rows - i);j++)
{
printf(" ");
}
// inner loop 2 to print numbers
for (k=1; k<2*i;k++)
{
printf("%d ", i);
}
printf("\n");
}
return 0;
}
Output:
Number of rows: 5
1
222
33333
4444444
555555555
Result: The given program to construct pyramid of numbers has been executed
successfully.
6.6 Find out sum of individual digits of a given positive integer
Aim: To find out the sum of individual digits of a given positive integer.
Description:
Sum of individual digits of a positive integer mean, we have to add each digit in a
given number and show that sum as ouput. If number is 3456 then operation will
3+4+5+6=18.
Program:
#include<stdio.h>
int main()
{
int onum, num, sum;
printf("Enter the positive number:");
scanf("%d", &onum);
num=onum;
sum = 0;
while (num != 0)
{
int r = num % 10;
sum = sum + r;
num = num/ 10;
}
printf("sum of individual digits of given number(%d) is %d", onum, sum);
return 0;
}
Output:
Result: The given program to find out sum of individual digits of a given positive
integer has been executed successfully.
6.7 Find out the given number is strong number or not
Aim: To find out the given number is strong number or not.
Description:
If we want to know that given number is strong number or not , then we have to check
sum of all digits factorial is equal to the given number.
N= 145
Now we have to do fact = 1! + 4! + 5! = 1 + 24 + 120 = 145
Sum of all digits factorial of given number (N) = given number(N)
Fact=N;
Program: