-->
R27mUISKY8MAeCpFpAtsSpjGWGukfoZYVKEfkHA4
Free Download Blogger Template High CTR AdSense

 

C PROGRAMME for BSC I YEAR

 

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;

}

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

12. Generate Fibonacci Series.

Note: Fibonacci Series is 0 1 1 2 3 5 8 13 21 ...

#include <stdio.h>

void main()

{

int fib1 = 0, fib2 = 1, fib3, limit, count = 0;

printf("Enter the limit to generate the Fibonacci Series \n");

scanf("%d", &limit);

printf("Fibonacci Series is ...\n");

printf("%d\n", fib1);

printf("%d\n", fib2);

count = 2;

while (count < limit)

{

fib3 = fib1 + fib2;

count++;

printf("%d\n", fib3);

fib1 = fib2;

fib2 = fib3;

}

}

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

13. Array Order Reversal

#include<stdio.h>
int main()
{
int arr[10], i;
printf("Enter any 10 elements: ");
for(i=0; i<10; i++)
{
scanf("%d", &arr[i]);
}
printf("\nThe array elements in reverse order:\n");
for(i=9; i>=0; i--)
{
if(i==0)
printf("%d", arr[i]);
else
printf("%d, ", arr[i]);
}
return 0;
}

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

14. Finding the Maximum Number in a Set.

#include <stdio.h>
int main()
{
int size, i, largest;
printf("\n Enter the size of the array: ");
scanf("%d", &size);
int array[size]; //Declaring array
//Input array elements
printf("\n Enter %d elements of the array: \n", size);
for (i = 0; i < size; i++)
{
scanf(" %d", &array[i]);
}
//Declaring Largest element as the first element
largest = array[0];
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("\n largest element present in the given array is : %d", largest);
return 0;
}

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

15. Removal of Duplicates from an Ordered Array

#include<stdio.h>
#include<stdlib.h>
int main(){
int a[50],i,j,k, count = 0, dup[50], number;
printf("Enter size of the array\n");
scanf("%d",&number);
printf("Enter Elements of the array:\n");
for(i=0;i<number;i++){
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: \n");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
for(i=0;i<number;i++){
for(j = i+1; j < number; j++){
if(a[i] == a[j]){
for(k = j; k <number; k++){
a[k] = a[k+1];
}
j--;
number--;
}
}
}
printf("\nAfter deleting the duplicate element the Array is:\n");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
}

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

16. Partitioning an Array.

#include<stdio.h>
#define N 10
int main()
{
int a[N], arr1[N], arr2[N], i, pos, k1 = 0, k2 = 0;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Enter position to split the array in to Two\n");
scanf("%d", &pos);
for(i = 0; i < N; i++)
{
if(i < pos)
arr1[k1++] = a[i];
else
arr2[k2++] = a[i];
}
printf("\nElements of First Array -> arr1[%d]\n", k1);
for(i = 0; i < k1; i++)
printf("%d\n", arr1[i]);
printf("\nElements of Second Array -> arr2[%d]\n", k2);
for(i = 0; i < k2; i++)
printf("%d\n", arr2[i]);
printf("\n");
return 0;
}

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

17. Finding the Smallest Element.

#include<stdio.h>
void main()
{
int a[30],i,n,small;
printf(" Enter Number of Elements in Array :");
scanf("%d",&n);
/* Read Elements in an array */
printf(" \n Enter the Elements: ");
for(i=0 ; i < n ; i++)
scanf("%d",&a[i]);
small = a[0];
for(i = 0 ; i < n ; i++)
{
if ( a[i] < small )
small = a[i];
}
printf(" Smallest Element in the Array is : %d",small);
}

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

18. Count the number of vowels, consonants and special characters in a given sentence.

#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];//declare and initialize char array
int i; int digits=0,spaces=0,spe_Char=0;//display and initialize variables
int vCount=0,cCount=0;
printf("Please enter the string \n");
gets(str);//stored the string entered by user
for(i=0; str[i] != '\0'; i++)
{
if(str[i] == 'a'|| str[i] == 'e'|| str[i] == 'i'|| str[i] == 'o'|| str[i] == 'u' || // check vowels
str[i] == 'A'|| str[i] == 'E'|| str[i] == 'I'|| str[i] == 'O'|| str[i] == 'U' )
{
vCount++;
}
else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' ))
{ //check consonants
cCount++;
}
else if(str[i]>='0' && str[i]<='9') //check digits
{
digits++;
}
else if(str[i]==' ')
{ //check spaces
spaces++;
}
else{
spe_Char++;
}
}
printf("\nTotal Vowels:%d ",vCount); //display total number of vowels
printf("\nTotal consonants:%d ",cCount); //display total number of consonants
printf("\nTotal digits:%d ",digits); //display total number of digits
printf("\nTotal white space :%d ",spaces); //display total number of spaces
printf("\nTotal special characters:%d ",(spe_Char)); //display total number of special characters
return 0;
}

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

19. To find the addition and subtraction of two matrices using function.

#include<stdio.h>
void matSum(int m, int n, int a[m][n], int b[m][n], int sum[m][n] )
{
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
sum[i][j]=a[i][j]+b[i][j];
}
}
}
void matDiff(int m, int n, int a[m][n], int b[m][n], int diff[m][n] ){
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
diff[i][j]=a[i][j]-b[i][j];
}
}
}
void readMatrix(int m, int n, int matrix[m][n]){
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&matrix[i][j]);
}
}

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

0;i<m;i++){
for(j=0;j<n;j++){
printf("%d\t",matrix[i][j]);
}
printf("\n");
}
}
void main(){
int m,n,i,j;
printf("Enter the size of the matrices:\nNo. of rows (m): ");
scanf("%d",&m);
printf("\nNo. of columns(n): ");
scanf("%d",&n);
int a[m][n];
int b[m][n];
int sum[m][n];
int diff[m][n];
printf("\nEnter the elements of matrix A:\n");
readMatrix(m,n,a);
printf("\nEnter the elements of matrix B:\n");
readMatrix(m,n,b);
matSum(m,n,a,b,sum);
printf("\nThe sum of the matrices A+B is:\n");
printMatrix(m,n,sum);
matDiff(m,n,a,b,diff);
printf("\nThe difference of the matrices A-B is:\n");
printMatrix(m,n,diff);
}

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

20. Read N (minimum 5) students marks and find number of students passed and fail depending on the marks.

#include<stdio.h>
int main(){
int marks[5],i,fail_count=0,pass_count=0;
printf("Enter marks of 5 students");
for(i=0;i<5;i++)
{
scanf("%d",&marks[i]);
if(marks[i]<35)
fail_count++;
else if(marks[i]>=35 && marks[i]<=100)
pass_count++;
else if(marks[i]>100)
{
printf("This is invalid number. Enter a valid Marks\n");
}
}
printf("Count of Failed students=%d",fail_count);
printf("\nCount of Passed students=%d",pass_count);
}

Related Posts
Newer Oldest

Related Posts

3 comments