You are on page 1of 6

Write a program that inputs a series of integers and passes them one at a time

to function even which uses the remainder operator to determine if an integer is


even. The function should take an integer argument and return 1 if the integer is
even and 0 otherwise.

#include <stdio.h>
char evenOrNot(int number);
int main(void) {
int number,i;
printf("Enter 5 numbers: ");
for (i=1;i<=5;i++){
scanf("%d",&number);
evenOrNot(number);
}
}
char evenOrNot(int number){
if (number%2==0)
return printf("%d is Even\n",number);
else return printf("%d is NOT Even\n",number);
}

Write a function that displays at the left margin of the screen a solid square of
asterisks whose side is specified in integer parameter side.

#include <stdio.h>
#include <conio.h>
void squareCustomFill(int size, char chara);
int main(void) {
int size,i,k;
char chara;
printf("Enter size of square and character to be filled with: ");
scanf("%d",&size);
scanf(" %c",&chara);
//Beware the space before %c! This is for flushing
stdin!

squareCustomFill(size,chara);
return 0;
}
void squareCustomFill(int size, char chara){
int i,k;
for (k=1;k<=size;k++){
for(i=1;i<=size;i++)
printf("%c",chara);
printf("\n");
}
}

Define a function called hypotenuse that calculates the length of the


hypotenuse of a right triangle when the other two sides are given.
Use this function in a program to determine the length of the
hypotenuse for each of the following triangles. The function should
take two arguments of type double and return the hypotenuse as a
double. The sample output is as following.

#include <stdio.h>
02
#include <math.h>
03
04
05
06
07

double hypotenuse (double x, double y)


{
double length = sqrt( x*x + y*y);

08
09
10
11

return length;

12

int main (void)

13
14
15
16

{
double side 1, side 2, side 3;

17 printf( "Enter lenth of first and second side:" );


18
scanf( %f%f, &side1, &side2 );
19
20
side 3 = hypotenuse (side 1, side 2);
21 printf ( The length of the hyotenuese is %f\n", side 3);
22
23

return 0;

24
25

scanf( "%lf%lf", &side1, &side2 );


printf ( The length of the hyotenuese is %f\n", side3);
there's a quote missing at the front of this line.
no need to make a variable in your hyp method, simply:
return = sqrt( x*x + y*y);

#include
#include
double hypotenuse( double s1, double s2 );
main()
{
int i;
double side1 double side2;
for ( i = 1; i <= 3; i++ )
{
printf( "Enter the sides of the triangle: " );
scanf( "%lf%lf", &side1, &side2 );

printf( "Hypotenuse: %.1f\n\n", hypotenuse( side1, side2 ) ); }


}
double hypotenuse( double s1, double s2 ) {
return sqrt( pow( s1, 2 ) + pow( s2, 2 ) );

/* Exercise 5.15 Solution */


#include #include double hypotenuse( double s1, double s2 ); /* function
prototype */ int main() { int i; /* loop counter */ double side1; /* value for first
side */ double side2; /* value for second side */ /* loop 3 times */ for ( i = 1; i <=
3; i++ ) { printf( "Enter the sides of the triangle: " ); scanf( "%lf%lf", &side1,
&side2 ); /* calculate and display hypotenuse value */ printf( "Hypotenuse:
%.1f\n\n", hypotenuse( side1, side2 ) ); } /* end for */ return 0; /* indicate
successful termination */ } /* end main */ /* hypotenuse calculates value of
hypotenuse of a right triangle given two side values */ double
hypotenuse( double s1, double s2 ) { return sqrt( pow( s1, 2 ) + pow( s2,
2 ) ); } /* end function hypotenuse */ Enter the sides of the triangle: 3.0 4.0
Hypotenuse: 5.0 Enter the sides of the triangle: 5.0 12.0 Hypotenuse: 13.0 Enter
the sides of the
}

Write a program which contains function double ceil(double x),


double floor(double x) in case of ceil function should return the
smallest integer value greater than or equal to x and on other hand
floor should return the largest integer value less than or equal to
x.Fuctions should be defined with in switch statement.

This function returns the smallest integral value not less than x.

Example
The following example shows the usage of ceil() function.
#include <stdio.h>
#include <math.h>

int main ()
{
float val1, val2, val3, val4;

val1 = 1.6;
val2 = 1.2;
val3 = 2.8;
val4 = 2.3;

printf ("value1 = %.1lf\n", ceil(val1));


printf ("value2 = %.1lf\n", ceil(val2));
printf ("value3 = %.1lf\n", ceil(val3));
printf ("value4 = %.1lf\n", ceil(val4));

return(0);
}

Let us compile

Basic Math Methods


Method

doubleabs(doubled)
floatabs(floatf)
intabs(inti)
longabs(longlng)
doubleceil(doubled)

doublefloor(doubled)

doublerint(doubled)
longround(doubled)

Description

Returns the absolute value of the argument.

Returns the smallest integer that is greater than or


equal to the argument. Returned as a double.
Returns the largest integer that is less than or equal
to the argument. Returned as a double.
Returns the integer that is closest in value to the
argument. Returned as a double.
Returns the closest long or int, as indicated by the

Basic Math Methods


Method

intround(floatf)

Description
method's return type, to the argument.

doublemin(doublearg1,doublearg2)
floatmin(floatarg1,floatarg2)
Returns the smaller of the two arguments.
intmin(intarg1,intarg2)
longmin(longarg1,longarg2)
doublemax(doublearg1,doublearg2)
floatmax(floatarg1,floatarg2)
Returns the larger of the two arguments.
intmax(intarg1,intarg2)
longmax(longarg1,longarg2)
The following program, BasicMathDemo , illustrates how to use some of these methods:

You might also like