0% found this document useful (0 votes)
48 views6 pages

Week 6

The document contains 5 C programming lab exercises: 1) Write a program to calculate the factorial of a given number using a for loop. 2) Write a program to check if a given number is prime or not. 3) Write programs to calculate sine and cosine series for a given value. 4) Write a program to check if a given number is a palindrome. 5) Write a program to print a pyramid of numbers based on user input for number of rows. The document provides the source code and output for each program.

Uploaded by

mrpacharyaalt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views6 pages

Week 6

The document contains 5 C programming lab exercises: 1) Write a program to calculate the factorial of a given number using a for loop. 2) Write a program to check if a given number is prime or not. 3) Write programs to calculate sine and cosine series for a given value. 4) Write a program to check if a given number is a palindrome. 5) Write a program to print a pyramid of numbers based on user input for number of rows. The document provides the source code and output for each program.

Uploaded by

mrpacharyaalt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

WEEK 6

LAB PROGRAM
1. Find the factorial of given number using any loop.

SOURCE CODE:

#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}

OUTPUT:
Enter a number: 5
Factorial of 5 is: 120

2. Find the given number is a prime or not.

SOURCE CODE:
#include<stdio.h>
int main(){
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}
OUTPUT:
Enter the number to check prime:2
Number is prime

3. Compute sine and cos series

SOURCE CODE FOR SIN SERIES:


#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;
/* Loop to calculate the value of Sine */
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 : 45
Enter the value for n : 5
The value of Sin(0.785398) = 0.7071

SOURCE CODE FOR COS SERIES:


#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 : 30
Enter the value for n : 5
The value of Cos(0.523598) = 0.8660

4. CHECK THE NUMBER PALINDROME OR NOT

SOURCE CODE:
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
OUTPUT:
enter the number=123454321
palindrome number

5. CONSTRUCT A PYRAMID OF NUMBERS

SOURCE CODE:

#include <stdio.h>
int main()
{
int i, space, rows, k = 0, count = 0, count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{
for (space = 1; space <= rows - i; ++space)
{
printf(" ");
++count;
}
while (k != 2 * i - 1) {
if (count <= rows - 1) {
printf("%d ", i + k);
++count;
}
else
{
++count1;
printf("%d ", (i + k - 2 * count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}

OUTPUT:
Enter the number of rows: 5
1
2 3 2
34543
4567654
567898765

You might also like