C Programming Lab Manual

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

PROGRAM NO.

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

for(i = 1; i<=n ; i++)


{
fact = fact * i;
}

printf("Factorial of %d = %d \n", n , fact);

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;

printf("Enter number to check prime number or not");

scanf("%d",&n);

for(i=2; i<=n/2; i++)

if(n%i==0)

flag=1;

break;

if (flag==0)

printf("%d is a prime number.",n);

else

printf("%d is not a prime number.",n);

getch();

}
ALGORITHM:

step 1: start

step 2: [Accept a number] read n

step 3: set i = 2

step 4: Repeat steps 5 and 6 until i <= n/2

step 5: [ check whether n is divisible or not]

if n % i == 0 then

Goto step 7

else

Goto step 6

step 6: set i=i+1

step 7: if i==n then

Print "number is prime"

else

Print "number is not prime"

Step 8: Stop.
FLOWCHART:
PROGRAM NO.3

C PROGRAM ON QUADRATIC EQUATIONS

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

C PROGRAM ON SINE SERIES.

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

Step 2: Declare the variables i, n, x, sum, t.

Step 3: Read the value of ‘x’ in degrees and value of ‘n’.

Step 4: Convert value of ‘x’ from degree to radians by

x=x*3.14159/180.

Step 5: Initialize

 tx and sumx.


Step 6: Repeat the steps below until i<=n.
 t=(t*(-1)*x*x)/(2*i*(2*i+1)).
         sum=sum+t.
nn+1.
Step 7: Display sum of n terms in sine series as sinx.
Step 8: Stop.

You might also like