Exp CP
Exp CP
Area
#include <stdio.h>
int main() {
int l,b,area1;
printf("Enter the length and breadth value:");
scanf("%d%d",&l,&b);
area1=l*b;
printf("Area of rectangle=%d\n",area1);
int h,area2;
printf("Enter the value of of base and height:");
scanf("%d%d",&b,&h);
area2=0.5*b*h;
printf("Area of triangle=%d",area2);
return 0; }
3.Electricity bill
#include <stdio.h>
int main() {
int a,units;
printf("enter the consumer number");
scanf("%d",&a);
printf("enter the units consumed");
scanf("%d",&units);
float amt;
if (units<=100) {
amt=0.588*units;
printf("amt=%f",amt); }
else if(units>=101 && units<200) {
amt=50+0.65*(units-100);
printf("amt=%f",amt); }
else {
amt=115+0.8*(units-200);
printf("amt=%f",amt); }
return 0;}
4.Quadratic
#include <stdio.h>
#include <math.h>
int main() {
int a, b, c, disc, r1, r2;
disc = (b * b) - 4 * a * c;
if (disc > 0) {
printf("the roots are real and distinct\n");
r1 = (-b + sqrt(disc)) / 2 * a;
r2 = (-b - sqrt(disc)) / 2 * a;
printf("the roots are %d and %d", r1, r2);
} else if (disc == 0) {
printf("roots are real and equal\n");
r1 = (-b + sqrt(disc)) / 2 * a;
r2 = r1;
printf("the roots are %d and %d", r1, r2);
} else {
printf("roots are imaginary"); }
return 0; }
8.Sine cosine
#include<stdio.h>
#include<math.h>
int main() {
float i,j;
printf("sine series is :\n");
for(i=0;i<=90;i+=15) {
printf ("sin(%.01f)=%1f \n",i,sin(i*3.14159/180.0)); }
printf("\ncosine series is \n");
for(j=90; j>=0; j-=15) {
printf("cos(%0.1f)=%1f\n",j,cos(j*3.14159/180)); }
return 0; }
5.Calculator
#include <stdio.h>
int main() {
int a,b,r,m;
printf("enter the num1 ");
scanf("%d",&a);
printf("enter the num2 ");
scanf("%d",&b);
printf("enter the operator\n1. Addition\n2. Subtraction\n3. multiplication\n4. Division\n");
scanf("%d",&m);
switch(m) {
case 1:r=a+b;
printf("result=%d",r);
break;
case 2:r=a-b;
printf("result=%d",r);
break;
case 3:r=a*b;
printf("result=%d",r);
break;
case 4:r=a/b;
printf("result=%d",r);
break;
default:
printf("invalid operator");
break; }
return 0; }
6.Sum of digit
#include <stdio.h>
int main() {
int No, sum = 0, rem = 0;
printf("Enter a number: ");
scanf("%d", &No);
while (No > 0) {
rem = No % 10;
sum = sum + rem;
No = No/10; }
printf("Sum of digits: %d\n", sum);
return 0; }
7.Prime number
#include <stdio.h>
#include <math.h>
int isprime(int n) {
if (n <= 1) {
return 0; }
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 0; }}
return 1; }
int main() {
int start, end;
printf("enter the range (start and end): ");
scanf("%d %d", &start, &end);
printf("prime numbers between %d and %d are:\n", start, end);
for (int i = start; i <= end; i++) {
if (isprime(i)) {
printf("%d ", i); }}
return 0; }
9.Pascals
#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; }
10.Sum and average
#include <stdio.h>
int main() {
int n, i;
float sum = 0, avg;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum=sum+arr[i];}
avg = sum / n;
printf("Sum = %.2f\n", sum);
printf("Average = %.2f\n", avg);
return 0;}
11.Bubble sort
#include <stdio.h>
int main() {
int a[20], n, i, j, temp, swap;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]); }
for (i = 0; i < n - 1; i++) {
swap = 0;
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
swap = 1; }}
if (swap == 0) {
break; }}
printf("The sorted array is: ");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0; }