C PROGRAMME for BSC I YEAR COMPUTER SCIENCE

 



 .

1. Find the area of a circle and area of a triangle given three sides.

/* C program to find the area of a triangle given 2 sides of triangle (using Heron's formula) and area of

circle */

#include<stdio.h>

#include<math.h>

#include<conio.h>

#define PI 3.14

int main()

{

 float a, b, c, s, area; // declare variables to find area of Triangle

 float radius,area_circle; // declare variables to find area of Circle

 // Inputting values for triangles

 printf("\nEnter three sides of triangle\n");

 scanf("%f %f %f",&a,&b,&c);

// Inputting values for Circle

 printf("Enter Radius of Circle(in cm): "); // take input

 scanf("%f",&radius);

 //Calculate area of triangle

 s = (a+b+c)/2;

 area = sqrt(s*(s-a)*(s-b)*(s-c));

 //Area with 2 digits of precision

 printf("\nArea of triangle: %.2f\n",area);

 //Calculate area of Circle

 area_circle = PI*radius*radius; // calculate area

 printf("Area of Circle = %f cm\n",area_circle); // display result

 return 0;

}

Note:

Area of Triangle with Three Sides (Heron’s Formula)

The area of a triangle with 3 sides of different measures can be calculated using Heron’s formula.

Heron’s formula includes two important steps. The first step is to find the semi perimeter of a triangle

by adding all three sides of a triangle and dividing it by 2. The next step is to apply the semi-perimeter

of triangle value in the main formula called “Heron’s Formula” to find the area of a triangle.

Area of triangles for three sides-Heron's formula where, s is semi-perimeter of the triangle = s =

(a+b+c) / 2

Output:


--------------------------------------------------------------------


2. Largest of three numbers.

/* C program to find the largest of three numbers */

#include <stdio.h>

void main()

{

int num1, num2, num3;

 printf("Enter the values of num1, num2 and num3\n");

 scanf("%d %d %d", &num1, &num2, &num3);

 printf("num1 = %d\t num2 = %d\t num3 = %d\n", num1, num2, num3);

 if (num1 > num2)

 {

 if (num1 > num3)

 {

 printf("num1 is the greatest among three \n");

 }

 else

 {

 printf("num3 is the greatest among three \n");

 }

 }

 else if (num2 > num3)

 printf("num2 is the greatest among three \n");

 else

 printf("num3 is the greatest among three \n");

}

Output:

--------------------------------------------------------------------


3. Reversing the digits of an integer.

/* C program to reverse a number */

#include <stdio.h>

void main

{

 long num, reverse = 0, temp, remainder;

 printf("Enter the number:\n");

 scanf("%ld", &num);

temp = num;

 while (num > 0)

 {

 remainder = num % 10;

 reverse = reverse * 10 + remainder;

 num =num/ 10;

 }

 printf("Given number = %ld\n", temp);

 printf("Reverse of a Number is %ld\n", reverse);

}

--------------------------------------------------------------------


4. GCD of two integers.

#include <stdio.h>

void main()

{

 int num1, num2, gcd, lcm, remainder, numerator, denominator;

 printf("Enter two numbers\n");

 scanf("%d %d", &num1, &num2);

 //To find numerator and denominator

 numerator = (num1>num2)?num1:num2;

 denominator = (num1<num2)?num1:num2;

 remainder = numerator % denominator;

 while (remainder != 0)

 {

 numerator = denominator;

 denominator = remainder;

 remainder = numerator % denominator;

 }

 gcd = denominator;

 printf("GCD of %d and %d = %d\n", num1, num2, gcd);

}

--------------------------------------------------------------------


5. Computing nth Fibonacci numbers.

#include<stdio.h>

int Fibonacci(int n)

{

if (n <= 1)

return n;

return Fibonacci(n - 1) + Fibonacci(n - 2);

}

int main()

{

int n;

printf("Enter the n value: ");

scanf("%d", &n);

printf("%d", Fibonacci(n - 1));

}

--------------------------------------------------------------------


6. Finding Even and Odd numbers.

#include <stdio.h>

void main()

{

 int num1, rem1;

 printf("Input an integer : ");

 scanf("%d", &num1);

 rem1 = num1 % 2;

 if (rem1 == 0)

 printf("%d is an even integer\n", num1);

 else

 printf("%d is an odd integer\n", num1);

}

--------------------------------------------------------------------


7. Generating prime numbers

#include<stdio.h>

void main()

{

 int i, num, n, count;

 printf("Enter the range: \n");

 scanf("%d", &n);

 printf("The prime numbers in between the range 1 to %d:",n);

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

 {

 count = 0;

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

 {

 if(num%i==0)

 {

 count++;

 break;

 }

 }

 if(count==0 && num!= 1)

 printf("%d ",num);

 }

}

OR

#include<stdio.h>

void main ()

{

    int i,j,n;

    printf("Enter the Range ");

    scanf("%d",&n);

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

    {

        for(j=2;j<i;j++)

        {

            if(i%j==0)

            break;

        }

        if(i==j)

        printf("%d ",i);

    }

}





--------------------------------------------------------------------


8. Exchanging the values of two variables

#include <stdio.h>

void main()

{

 int x, y,temp;

 printf("Enter Value of x= ");

scanf("%d", &x);

 printf("\nEnter Value of y= ");

 scanf("%d", &y);

 temp = x;

 x = y;

 y = temp;

 printf("\nAfter Swapping: x = %d, y = %d", x, y);

}

--------------------------------------------------------------------


9. Counting: Print number from 100 to 200 which are divisible by 7 and display their sum

and count using for loop.

#include<stdio.h>

void main()

{

 int d,i,count=0,val;

 printf("The numbers which are divisible by 7\nbetween 100 and 200: \n");

 for(i=100;i<200;i++)

 {

 if(i%7==0)

 {

 printf("\t");

 count=count+1;

 val=i;

 printf("%d",val);

 }

 }

 printf("\n");

 printf("count=%d",count);

}

--------------------------------------------------------------------


10. Summation of set of Numbers.

#include<stdio.h>

int main()

{

 int Number, i, Sum = 0;

 printf("\n Enter any Integer Value\n");

 scanf("%d", &Number);

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

 {

 Sum = Sum + i;

 }

 printf("Sum of Natural Numbers = %d", Sum);

 return 0;

}

--------------------------------------------------------------------


11. Factorial Computation.

Factorial Program in C: Factorial of n is the product of all positive descending integers. Factorial of n

is denoted by n!. For example:

5! = 5*4*3*2*1 = 120

3! = 3*2*1 = 6

#include <stdio.h>

int main() {

 int n, i;

 unsigned long long fact = 1;

 printf("Enter an integer: ");

 scanf("%d", &n);

 // shows error if the user enters a negative integer

 if (n < 0)

 printf("Error! Factorial of a negative number doesn't exist.");

 else {

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

 fact *= i;

}

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

 }

 return 0;

}

Previous Post Next Post

Ads

Ads

ู†ู…ูˆุฐุฌ ุงู„ุงุชุตุงู„

×