0% found this document useful (0 votes)
44 views12 pages

Study

The first code example calculates the distance between two points defined by x and y coordinates by taking the square root of the sum of the squared differences between x1 and x2 and y1 and y2. The second converts specified days into years, weeks and remaining days by dividing and modular arithmetic. The third breaks down an integer amount into the minimum number of higher denomination banknotes.

Uploaded by

trxxx.axx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views12 pages

Study

The first code example calculates the distance between two points defined by x and y coordinates by taking the square root of the sum of the squared differences between x1 and x2 and y1 and y2. The second converts specified days into years, weeks and remaining days by dividing and modular arithmetic. The third breaks down an integer amount into the minimum number of higher denomination banknotes.

Uploaded by

trxxx.axx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Write a C program to calculate the distance between the two


points.
Note: x1, y1, x2, y2 are all double values.

Formula:

C Code:
#include <stdio.h>

#include <math.h>

int main() {

float x1, y1, x2, y2, gdistance;

printf("Input x1: ");

scanf("%f", &x1);

printf("Input y1: ");

scanf("%f", &y1);

printf("Input x2: ");

scanf("%f", &x2);

printf("Input y2: ");

scanf("%f", &y2);

gdistance = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));

printf("Distance between the said points: %.4f", sqrt(gdistance));

printf("\n");

return 0;

Copy
Sample Output:
Input x1: 25
Input y1: 15
Input x2: 35
Input y2: 10
Distance between the said points: 11.1803
2. Write a C program to convert specified days into years, weeks
and days.

C Code:

#include <stdio.h>

int main()

int days, years, weeks;

days = 1329;

// Converts days to years, weeks and days

years = days/365;

weeks = (days % 365)/7;

days = days- ((years*365) + (weeks*7));

printf("Years: %d\n", years);

printf("Weeks: %d\n", weeks);

printf("Days: %d \n", days);

return 0;

Copy
Sample Output:
Years: 3
Weeks: 33
Days: 3
3. Write a C program to read an amount (integer value) and break
the amount into smallest possible number of bank notes.
Note: The possible banknotes are 100, 50, 20, 10, 5, 2 and 1.

C Code:
#include <stdio.h>
int main() {
int amt, total;
printf("Input the amount: ");
scanf("%d",&amt);

total = (int)amt/100;

printf("There are: ");

printf("\n%d Note(s) of 100.00\n", total);

amt = amt-(total*100);

total = (int)amt/50;

printf("%d Note(s) of 50.00\n", total);

amt = amt-(total*50);

total = (int)amt/20;

printf("%d Note(s) of 20.00\n", total);

amt = amt-(total*20);

total = (int)amt/10;

printf("%d Note(s) of 10.00\n", total);

amt = amt-(total*10);

total = (int)amt/5;

printf("%d Note(s) of 5.00\n", total);

amt = amt-(total*5);

total = (int)amt/2;

printf("%d Note(s) of 2.00\n", total);

amt = amt-(total*2);

total = (int)amt/1;

printf("%d Note(s) of 1.00\n", total);

return 0;
}
Copy
Sample Output:
Input the amount: 375
There are:
3 Note(s) of 100.00
1 Note(s) of 50.00
1 Note(s) of 20.00
0 Note(s) of 10.00
1 Note(s) of 5.00
0 Note(s) of 2.00
0 Note(s) of 1.00

4. Write a C program to convert a given integer (in seconds) to


hours, minutes and seconds.
C Code:
#include <stdio.h>

int main() {

int sec, h, m, s;

printf("Input seconds: ");

scanf("%d", &sec);

h = (sec/3600);

m = (sec -(3600*h))/60;

s = (sec -(3600*h)-(m*60));

printf("H:M:S - %d:%d:%d\n",h,m,s);

return 0;

Copy
Sample Output:
Input seconds: 25300
H:M:S - 7:1:40
5. Write a C program that read 5 numbers and sum of all odd
values between them.

C Code:
#include <stdio.h>

int main() {

int j, numbers[5],total=0;

printf("\nInput the first number: ");

scanf("%d", &numbers[0]);

printf("\nInput the second number: ");

scanf("%d", &numbers[1]);

printf("\nInput the third number: ");

scanf("%d", &numbers[2]);

printf("\nInput the fourth number: ");

scanf("%d", &numbers[3]);

printf("\nInput the fifth number: ");

scanf("%d", &numbers[4]);

for(j = 0; j < 5; j++) {

if((numbers[j]%2) != 0)

total += numbers[j];

printf("\nSum of all odd values: %d", total);

printf("\n");

return 0;

Copy
Sample Output:
Input the first number: 11

Input the second number: 17

Input the third number: 13


Input the fourth number: 12

Input the fifth number: 5

Sum of all odd values: 46

6. write a c program that will compute the final price of a product after putting
the original price and percentage of discount.

7. write a c program that will convert inches to centimetre and vice versa
#include <stdio.h>
#include <stdlib.h>

int main()
{

float in_cm;
char in_inches[4];

printf("Convert inches: ");


scanf("%s", &in_inches);

in_cm = atoi(in_inches)*2.54;

printf("%.2f cmn", in_cm);


return(0);

}
To compile the code with gcc:

$ gcc inches_cm.c -o inches_cm

Exmaple run:

$ ./inches_cm

Convert inches: 12

30.48 cm

$ ./inches_cm

Convert inches: 563478

1431234.12 cm
8. write a c program that will convert meter to kilometer and vice versa
Centimeter to meter and centimeter to kilometer conversion formula is given by -

#include <stdio.h>

int main()
{
float cm, meter, km;

/* Input length in centimeter from user */


printf("Enter length in centimeter: ");
scanf("%f", &cm);

/* Convert centimeter into meter and kilometer */


meter = cm / 100.0;
km = cm / 100000.0;

printf("Length in Meter = %.2f m \n", meter);


printf("Length in Kilometer = %.2f km", km);

return 0;
}

Example
Input
Enter length in centimeter = 1000

Output

Length in meter = 10 m
Length in kilometer = 0.01 km
9. C program to convert pounds to kilograms

#include <stdio.h>
#define POUNDTOKG 0.453592
int main() {
float pound, kilogram;

/* get the input from the user */


printf("Enter weight in pound:");
scanf("%f", &pound);

/* converting pound to kilogram */


kilogram = pound * POUNDTOKG;

/* printing the output */


printf("%.2f pound = %.2f KiloGram\n", pound, kilogram);
return 0;
}

Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter weight in pound:2.20462
2.20 pound = 1.00 KiloGram
C program to find the mean,median and mode of a
set of numbers
Share16

#include<stdio.h>
main()
{
int i,j,a[20]={0},sum=0,n,t,b[20]={0},k=0,c=1,max=0,mode;
float x=0.0,y=0.0;
printf("\nEnter the limit\n");
scanf("%d",&n);
printf("Enter the set of numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
x=(float)sum/(float)n;
printf("Mean\t= %f",x);

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
if(n%2==0)
y=(float)(a[n/2]+a[(n-1)/2])/2;
else
y=a[(n-1)/2];
printf("\nMedian\t= %f",y);

for(i=0;i<n-1;i++)
{
mode=0;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
mode++;
}
}
if((mode>max)&&(mode!=0))
{
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
c++;
}
if(c==n)
printf("\nThere is no mode");
else
{
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
}
C Program to Calculate the Mean, Variance & Standard
Deviation

#include <stdio.h>
#include <math.h>
#define MAXSIZE 10

void main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, sum = 0, sum1 = 0;

printf("Enter the value of N \n");


scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
/* Compute the sum of all elements */
for (i = 0; i < n; i++)
{
sum = sum + x[i];
}
average = sum / (float)n;
/* Compute variance and standard deviation */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
}

Runtime Test Cases

$ cc pgm23.c -lm
$ a.out
Enter the value of N
5
Enter 5 real numbers
34
88
32
12
10
Average of all elements = 35.20
variance of all elements = 794.56
Standard deviation = 28.19

You might also like