0% found this document useful (0 votes)
33 views

Task 6

The document discusses several programs for calculating factorials, checking if a number is prime, computing sine and cosine series, checking if a number is a palindrome, constructing a number pyramid, finding the sum of digits in a number, and checking if a number is a strong number. The programs demonstrate the use of loops and conditional statements to solve mathematical problems.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Task 6

The document discusses several programs for calculating factorials, checking if a number is prime, computing sine and cosine series, checking if a number is a palindrome, constructing a number pyramid, finding the sum of digits in a number, and checking if a number is a strong number. The programs demonstrate the use of loops and conditional statements to solve mathematical problems.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

6.

1 Find the Factorial of given number using any loop


Aim: To find the factorial of given number using any loop
Description:
Factorial is a product of all positive numbers from 1 to n, here n is a number to
find factorial.
Ex: 5! = 5*4*3*2*1
for loop: first Initializes, then condition check, then executes the body and at
last, the update is done.
while loop: first Initializes, then condition checks, and then executes the body,
and updating can be inside the body.
do-while loop: do-while first executes the body and then the condition check is
done.

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

Aim: To compute sine series


Description:
Sine Series:
Sine Series is a series which is used to find the value of Sin(x). where, x is the angle
in degree which is converted to Radian.
The formula used to express the Sin(x) as Sine Series is

Expanding the above notation, the formula of Sine Series is

For example,

Let the value of x be 30.

So, Radian value for 30 degree is 0.52359.

So, the value of Sin(30) is 0.5.

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

Aim: To compute cos series

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

Expanding the above notation, the formula of Cosine Series is

For example,

Let the value of x be 30.

So, Radian value for 30 degree is 0.52359.

So, the value of Cos(30) is 0.8660.

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);

printf(" Enter the value for n : ");


scanf("%d",&n);
x=x*3.14159/180;
/* Loop to calculate the value of Cosine */
for(i=1;i<=n;i++)
{
t=t*(-1)*x*x/(2*i*(2*i-1));
sum=sum+t;
}
printf(" The value of Cos(%f) is : %.4f", x, sum);
return 0;
}

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.

Enter the number to check whether it is palindrome or not: 34543


Given number (34543)and reversed number (34543) are same,so it is a palindrome
number
6.5 Construct a Pyramid of Numbers
Aim: Construct a pyramid with numbers
Description:
Pyramid is a 3 – dimensional shape that has a polygon base and the sides of the pyramid
look like a triangle shape. Pyramid pattern printing is a logical program by which we can
develop logical thinking in programming. We can use conditional statements and loop
concepts for making pyramid patterns. Pyramid patterns can be printed using numbers,
characters, and special characters like stars.

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:

Enter the positive number:45678

sum of individual digits of given number (45678) is 30

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:

You might also like