# 9.9 – functions – programs to practice Part-3

by on November 21, 2017

Specification: Accept an integer and print equivalent roman number

Show Logic

Roman equivalent to decimal numbers are
1- i , 5 – v , 10 – x , 50 – l , 100 – c , 500 – d  ,  1000  –  m

While converting a decimal number into roman numbers, we check how many 1000’s are there in the number and print ‘m’ for those many times

```for(i=1;i<=n/1000;i++)
printf("%c",'m');
n=n%1000;  /* removes thousands from the number */
```

We check how many 500’s are there in the number and print ‘d’  for those many times

```for(i=1;i<=n/500;i++)
printf("%c",'d');
n=n%500;  /* removes 500's from the number */
```

Printing roman characters can be automated using a sub function

```/* takes how many times and  what character to print as arguments */
void display(int times,char ch)
{
int i;
for(i=1;i<=times;i++)
printf("%c",ch);
}
```

To print thousands, we call display() as

```display(n/1000,'m');
```

To print five hundreds, we call display() as

```display(n/500,'d');
```

We continue the process for 100’s, 50’s, 10’s, 5’s  and 1’s

Program:

```#include<stdio.h>
void display(int,char);
int main()
{
int n,ths,fhs,hds,fis,tns,fvs,ons;
printf("Enter the decimal number:");
scanf("%d",&n);
display(n/1000,'m');   /* to print 'm' for n/1000 number of times */
n=n%1000;              /* removing 1000's from the number */
display(n/500,'d');    /* to print 'd' for n/500 number of times */
n=n%500;               /* removing 500's from the number */
display(n/100,'c');    /* to print 'c' for n/100 number of times */
n=n%100;               /* removing 100's from the number */
display(n/50,'l');     /* to print 'l' for n/50 number of times */
n=n%50;                /* removing 50's from the number */
display(n/10,'x');     /* to print 'x' for n/10 number of times */
n=n%10;                /* removing 10's from the number */
display(n/5,'v');      /* to print 'v' for n/5 number of times */
n=n%5;                 /* removing 5's from the number */
display(n,'i');        /* to print 'i' for n number of times */
return 0;
}
void display(int times,char ch)
{
int i;
for(i=1;i<=times;i++)
printf("%c",ch);
}
```

Execution:
Enter the decimal number:1978
mdcccclxxviii

Specification: If user gives temperature in centigrade then print it in Fahrenheit using the function myctof(), if user gives temperature in Fahrenheit then print it in centigrade using the function myftoc().

```#include<stdio.h>
float myctof(int);
float myftoc(int);
int main()
{
int temp;
char ch;
printf("Want to give temperature in c/f:");
scanf("%c",&ch);
if(ch=='c')
{
scanf("%d",&temp);
printf("Temperature in Fahrenheit %f",myctof(temp));
}
else
{
printf("Enter the temperature in Fahrenheit:");
scanf("%d",&temp);
}
return 0;
}
float myctof(int c)
{
/* F= C*(9/5)+32 */
return c*(9.0/5.0)+32;
}
float myftoc(int f)
{
/* C=(5/9)*(F-32)    */
return (5.0/9.0)*(f-32);
}
```

Execution:
Want to give temperature in c/f:c
Temperature in Fahrenheit 212.000000

Execution:
Want to give temperature in c/f:f
Enter the temperature in Fahrenheit:212

Specification: Print Pascal triangle for n number of terms

Show Logic

Pascal’s triangle is a triangular array of the binomial coefficients. To build the triangle, start with “1” at the top and then continue placing numbers below it in a triangular pattern.
Each number is just the two numbers above it added together (except for the edges, which are all “1”).

Logic:
We need to print n rows that is from 0th row to n-1th row

```for(r=0;r<=n-1;r++)
------------------
```

we have to print (n-r)-1 spaces for every row. Say for example, for 0th row spaces should be (5-0)-1 that is 4 spaces, for 1st row spaces must be (5-1)-1 that is 3 spaces must be printed

```for(r=0;r<=n-1;r++)
{
for(s=1;s<=(n-r)-1;s++)
printf("  ");              /* 2 spaces */
----------
}
```

We need to print binomial coefficients for every row. Every binomial coefficient (term) is calculated using factorial(r)/(factorial(t)*factorial(r-t)) and printed

```c=factorial(r)/(factorial(t)*factorial(r-t));
```

Putting all together

```for(r=0;r<=n-1;r++)
{
for(s=1;s<=(n-r)-1;s++)
printf("  ");              /* 2 spaces */
for(t=0;t<=r;t++)         /* printing r+1 terms */
{
/* calculating binomial coefficients */
c=factorial(r)/(factorial(t)*factorial(r-t));
printf("%4ld",c);
}
printf("\n\n");
}
```

Here the factorial is calculated using the function factorial()

```long factorial(int num)
{
long fact=1;
int i;
for(fact=i=1;i<=num;i++)
fact=fact*i;
return fact;
}
```

Program:

```#include<stdio.h>
long factorial(int);
int main()
{
int n,r,t,s;
long c;
printf("Enter the limit ");
scanf("%d",&n);
printf("\n");
for(r=0;r<=n-1;r++)
{
for(s=1;s<=(n-r)-1;s++)
printf("  ");              /* 2 spaces */
for(t=0;t<=r;t++)         /* printing r+1 terms */
{
/* calculating binomial coefficients */
c=factorial(r)/(factorial(t)*factorial(r-t));
printf("%4ld",c);
}
printf("\n\n");
}
return 0;
}
long factorial(int num)
{
long fact=1;
int i;
for(fact=i=1;i<=num;i++)
fact=fact*i;
return fact;
}
```

Specification: Accept coordinates of two points and print the distance among them using the function length()

Logic:
if (x1,y1) and (x2,y2) are two points then the distance among them is calculated using the formula

```sqrt(pow(x2-x1,2)+pow(y2-y1,2));
```

Program:

```#include<stdio.h>
#include<stdio.h>
double distance(int,int,int,int);
int main()
{
int x1,x2,y1,y2;
double d;
printf("Enter the co-ords of first point:\n");
scanf("%d%d",&x1,&y1);
printf("Enter the co-ords of second point:\n");
scanf("%d%d",&x2,&y2);
d=distance(x1,y1,x2,y2);
printf("Distance among two points %lf",d);
return 0;
}
double distance(int x1,int y1,int x2,int y2)
{
return sqrt(pow(x2-x1,2)+pow(y2-y1,2));
}
```

Execution:
Enter the co-ords of first point:
10  0
Enter the co-ords of second point:
25  0
Distance among two points 15.000000

Specification: If coordinates of three points are input then print whether they are in straight line or not.

Logic:

Program:

```#include<stdio.h>
#include<stdio.h>
double distance(int,int,int,int);
int main()
{
int x1,x2,x3,y1,y2,y3;
double d1,d2,d3;
printf("Enter the first point:\n");
scanf("%d%d",&x1,&y1);
printf("Enter the second point:\n");
scanf("%d%d",&x2,&y2);
printf("Enter the third point:\n");
scanf("%d%d",&x3,&y3);
d1=distance(x1,y1,x2,y2); /* distance among (x1,y1) and (x2,y2) */
d2=distance(x2,y2,x3,y3); /* distance among (x2,y2) and (x3,y3) */
d3=distance(x1,y1,x3,y3); /* distance among (x1,y1) and (x3,y3) */
if(d1+d2==d3)
printf("In stright line");
else
printf("Not in stright line");
return 0;
}
double distance(int x1,int y1,int x2,int y2)
{
return sqrt(pow(x2-x1,2)+pow(y2-y1,2));
}
```

Execution:
Enter the first point:
10      10
Enter the second point:
20      20
Enter the third point:
30      30
In straight line

Specification:  Accept the center of a circle and radios; write a program to determine whether a given point is existed either inside or outside the circle.

Logic:

If the distance among the center and the given point is equal to the radios then the point is on the circle, if less than the radios then the point is within the circle and if greater than radios then the point is outside the circle.

Program:

```#include<stdio.h>
#include<stdio.h>
double distance(int,int,int,int);
int main()
{
int x1,y1,x2,y2;
double r,d;
printf("Enter the center of circle:\n");
scanf("%d%d",&x1,&y1);
scanf("%lf",&r);
printf("Enter the coordinates of point:\n");
scanf("%d%d",&x2,&y2);
d=distance(x1,y1,x2,y2); /* distance among (x1,y1) and (x2,y2) */
if(d==r)
printf("on the circle");
else if(d>r)
printf("Out side the circle");
else
printf("Within the circle");
return 0;
}
double distance(int x1,int y1,int x2,int y2)
{
return sqrt(pow(x2-x1,2)+pow(y2-y1,2));
}
```

Execution:
Enter the center of circle:
10 10
5
Enter the coordinates of point:
12 13
Within the circle

Execution:
Enter the center of circle:
10 10
5
Enter the coordinates of point:
15 15
Out side the circle

Specification: Accept the year from keyboard and print whether the year is a leap year or not using the function isleap()

Logic:
If the year is a century year then it must be divisible by 400
If the year is a not a century year then it must be divisible by 4

```(year%100==0&&year%400==0||year%100!=0&&year%4==0)
```

Here the function isleap() must accept an year and return 1 (true) if the year is a leap year otherwise must return 0 (false)

Program:

```#include<stdio.h>
int isleap(int);
int main()
{
int year;
printf("Enter any year:");
scanf("%d",&year);
if(isleap(year))
printf("Leap Year");
else
printf("Not a leap year");
return 0;
}
int isleap(int y)
{
/* Any logical expression returns either 1 or 0              *
* the below condition returns 1 if the year is a leap year  *
* otherwise returns 0                                       */
return (y%100==0&&y%400==0||y%100!=0&&y%4==0);
}
```

Execution:
Enter any year:1900
Not a leap year

Execution:
Enter any year:2000
Leap Year

Specification: Accept the number of days the member is late to return the book to library and calculate the late fee using the function finecalc() according to the following condition.
For first 5 days fine is \$1
For 6 to 10 days fine is \$4
Above 10 days and up to 30 days fine is \$10
If it exceeds more than 30 days then membership would be cancelled.

Logic:

Here the function finecalc() must accept days of late and must return fine amount. It must return 0 if the member ship is cancelled

Program:

```#include<stdio.h>
int finecalc(int);
int main()
{
int d;
printf("Enter the days of delay:");
scanf("%d",&d);
if(finecalc(d))
printf("Fine Amount %d",finecalc(d));  /* when function returns non-zero */
else
printf("Your membership has been canceled"); /* when function returns zero      */
return 0;
}
int finecalc(int dd)
{
if(dd>30)           /* when exceeds 30 days */
return 0;
else if(dd>10)     /* above 10 up to 30 days */
return 10;
else if(dd>5)       /* from 6 to 10 days */
return 4;
else
return 1;        /* from first 1 to 5 days */
}
```

Execution:
Enter the days of delay: 35