Professional Documents
Culture Documents
C Programming Lab Manual
C Programming Lab Manual
C Programming Lab Manual
1
C PROGRAM ON FACTORIAL OF A NUMBER.
PROGRAM:
#include <stdio.h>
int main()
{
int i, n;
int fact=1;
printf("Enter a positive integer to calculate its factorial: ");
scanf("%d",&n);
return 0;
}
ALGORITHM:
Step 1: Start
Step 2: Read a number n
Step 2: Initialize variables:
i = 1, fact = 1
Step 3: if i <= n go to step 4 otherwise go to step 7
Step 4: Calculate
fact = fact * i
Step 5: Increment the i by 1 (i=i+1) and go to step 3
Step 6: Print fact
Step 7: Stop
FLOWCHART:
PROGRAM NO.2
C PROGRAM ON PRIME NUMBER.
PROGRAM:
#include<stdio.h>
int main()
int n, i, flag = 0;
scanf("%d",&n);
if(n%i==0)
flag=1;
break;
if (flag==0)
else
getch();
}
ALGORITHM:
step 1: start
step 3: set i = 2
if n % i == 0 then
Goto step 7
else
Goto step 6
else
Step 8: Stop.
FLOWCHART:
PROGRAM NO.3
PROGRAM:
#include<math.h>
#include<stdio.h>
int main()
{
float a, b, c, disc, r1, r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
disc = b * b - 4 * a * c;
if (disc > 0)
{
r1 = (-b + sqrt(disc)) / (2 * a);
r2 = (-b - sqrt(disc)) / (2 * a);
printf("r1 = %.2f and r2 = %.2f", r1, r2);
}
else if (disc == 0) {
r1 = r2 = -b / (2 * a);
printf("r1 = r2 = %.2f;", r1);
}
else
{
real = -b / (2 * a);
imag = sqrt(-disc) / (2 * a);
printf("r1 = %.2f+%.2fi and r2 = %.2f-%.2fi", real, imag, real, imag);
}
return 0;
}
ALGORITHM:
Step 1. Start
Step 2. Read the coefficients of the equation, a, b and c from the user.
Step 3. Calculate discriminant = (b * b) – (4 * a * c)
Step 4. If discriminant > 0:
4.1: Calculate root1 = ( -b + sqrt(discriminant)) / (2 * a)
4.2: Calculate root2 = ( -b - sqrt(discriminant)) / (2 * a)
4.3: Display “Roots are real and different”
4.4: Display root1 and root2
Step 5: Else if discriminant = 0:
5.1: Calculate root1 = -b / (2 *a)
5.2: root2 = root1
5.3: Display “Root are real and equal”
5.4: Display root1 and root2
Step 6. Else:
6.1: Calculate real = -b / (2 * a)
6.2: Calculate imaginary = sqrt(-discriminant) / (2 * a)
6.3: Display “Roots are imaginary”
6.4: Display real, “±” , imaginary, “i”
Step 7. Stop
FLOWCHART:
PROGRAM NO.3
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
float x, sum, t;
clrscr();
printf(" Enter the value for x in degrees : ");
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);
getch();
}
FLOWCHART:
ALGORITHM:
Step 1: Start
x=x*3.14159/180.
Step 5: Initialize