Tutorial_8
CT/2022/139
R.S.R. Ranathunga
1. Type in and run the all programs presented in lecture note 7 and 8. Briefly describe the theory/ concept you learned from these programs separately.
[01]
#include <stdio.h>
int main()
{
int sum = 17, count = 5;
double mean;
mean = sum / count;
printf("Value of mean : %f\n", mean );
return 0;
}
output: Value of mean : 3.000000
sum and count are integer data types.
integer type sum value dividing by integer type count value.
Data type of mean is double with 6 decimal zero values.
[02]
#include <stdio.h>
int main()
{
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %f\n", mean );
return 0;
}
output : Value of mean : 3.400000
sum and count are integer data type.
mean is double.
first sum conveted to type double and divided by count.
#include <stdio.h>
int main()
{
int i = 17;
char c = 'c'; /* ascii value is 99 */
int sum;
sum = i + c;
printf("Value of sum : %d\n", sum );
return 0;
}
output= Value of sum : 116
finding sum of i and c
Doing integer promotion and converting the value of 'c' to ASCII before performing the actual addition operation. ASCII value of c is 99
[04]
#include <stdio.h>
int main (void)
{
float f1 = 123.125, f2;
int i1, i2 = -150;
char c = 'a';
/* floating to integer conversion */
i1 = f1;
printf ("%f assigned to an int produces %i\n", f1, i1);
/* integer to floating conversion */
f1 = i2;
printf ("%i assigned to a float produces %f\n", i2, f1);
/* integer divided by integer */
f1 = i2 / 100;
printf ("%i divided by 100 produces %f\n", i2, f1);
/* integer divided by a float */
f2 = i2 / 100.0;
printf ("%i divided by 100.0 produces %f\n", i2, f2);
/* type cast operator */
f2 = (float) i2 / 100;
printf ("(float) %i divided by 100 produces %f\n", i2, f2);
return 0;
}
output:
123.125000 assigned to an int produces 123
-150 assigned to a float produces -150.000000
-150 divided by 100 produces -1.000000
-150 divided by 100.0 produces -1.500000
(float) -150 divided by 100 produces -1.500000
void- the function main takes no arguments.
shows Typical Arithmetic Conversion.
i2 integer type converted to float before calculation.
[05]
#include <stdio.h>
int main( int argc, char *argv[] )
{
printf("The first argument supplied is %s\n", argv[0]);
printf("The second argument supplied is %s\n", argv[1]);
return 0;
}
this program indicates Command Line arguments and handled using main()function arguments.
argc refers to the number of arguments passed, argc[] is a pointer array and which points to each argument passed to the program.
[06]
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("The first argument supplied is %s\n", argv[0]);
printf("The second argument supplied is %s\n", argv[1]);
printf("The third argument supplied is %s\n", argv[2]);
return 0;
}
argv[0]= indicates name of this program(my program name)
argc[1]=indicates arg1
argc[2]=indicates arg2
[07]
#include <stdio.h>
#define PI 3.1412
int main ()
{
float radius;
printf ("Enter the radius in mm : \n");
scanf ("%f",&radius);
printf("The perimeter is : %.2f", 2*PI*radius);
return 0;
}
this program indicates the perimeter of a circle.
#define- this indicates a way of the define constant and "#" using for define directive.
[08]
#include <stdio.h>
int main ()
{
const float PI = 3.1412;
float radius;
printf ("Enter the radius in mm : \n");
scanf ("%f",&radius);
printf("The perimeter is : %.2f", 2*PI*radius);
return 0;
}
this program indicates the perimeter of a circle.
const float PI = 3.1412;- declared a constant named as "PI" with a float data type.
[09]
#include <stdio.h>
#include <math.h>
int main ()
{
printf("Value 8.0 ^ 3 = %.2lf\n", pow(8.0, 3));
printf("Sqrt of 100 = %.2f\n", sqrt(100));
printf("log (100) = %.2f\n", log(100.0));
printf("sin (90) = %.2f\n", sin (90.0));
printf("ceil (10.45) = %.2f\n", ceil(10.45));
printf("floor (10.45) = %.2f\n", floor(10.45));
printf("fabs (-10.45) = %.2f\n", fabs(-10.45));
printf("round (10.45) = %.2f\n", round (10.45));
return(0);
}
output:
Value 8.0 ^ 3 = 512.00
Sqrt of 100 = 10.00
log (100) = 4.61
sin (90) = 0.89
ceil (10.45) = 11.00
floor (10.45) = 10.00
fabs (-10.45) = 10.45
round (10.45) = 10.45
The math.h header defines various mathematical functions. All the functions available in this library take double as an argument and return double as the result.
[10]
#include <stdio.h>
int main (void)
{
int a = 100;
int b = 2;
int c = 25;
int d = 4;
int result;
result = a - b;
printf ("a - b = %i\n", result);
result = b * c;
printf ("b * c = %i\n", result);
result = a / c;
printf ("a / c = %i\n", result);
result = a + b * c;
printf ("a + b * c = %i\n", result);
printf ("a * b + c * d = %i\n", a * b + c * d);
return 0;
}
output:
a - b = 98
b * c = 50
a / c = 4
a + b * c = 150
a * b + c * d = 300
this program indicates operations of operaters-(-,*,/,+)
[11]
#include <stdio.h>
int main (void)
{
int a = 25;
int b = 2;
float c = 25.0;
float d = 2.0;
printf ("6 + a / 5 * b = %i\n", 6 + a / 5 * b);
printf ("a / b * b = %i\n", a / b * b);
printf ("c / d * d = %f\n", c / d * d);
printf ("-a = %i\n", -a);
return 0;
}
output:
6 + a / 5 * b = 16
a / b * b = 24
c / d * d = 25.000000
-a = -25
this program indicates some arithmetic operates(+,/,*)
[12]
#include <stdio.h>
int main (void)
{
int a = 25, b = 5, c = 10, d = 7;
printf ("a %% b = %i\n", a % b);
printf ("a %% c = %i\n", a % c);
printf ("a %% d = %i\n", a % d);
printf ("a / d * d + a %% d = %i\n", a / d * d + a % d);
return 0;
}
output:
a % b = 0
a % c = 5
a % d = 4
a / d * d + a % d = 25
this indicates operation of some basic arithmetic operators and oparation of modulus operator(%)
[13]
#include <stdio.h>
int main()
{
int a = 5;
int b = 20;
int c = 1;
printf( "( a && b ) = %d \n", a && b );
printf( "( a || b ) = %d \n", a || b );
printf( " !a = %d \n", !a );
return 0;
}
output:
( a && b ) = 1
( a || b ) = 1
!a = 0
indicates Logical operators-(&&, || , !)
[14]
#include <stdio.h>
int main()
{
int a = 5;
int b = 20;
int c = 5;
printf( "( a < b ) = %d \n", a < b );
printf( "( a <= b ) = %d \n", a <= b );
printf( "( a > b ) = %d \n", a > b );
printf( "( a >= b ) = %d \n", a >= b );
printf( "( c == a ) = %d \n", c == a );
printf( "( a != b ) = %d \n", a != b );
return 0;
}
output:
( a < b ) = 1
( a <= b ) = 1
( a > b ) = 0
( a >= b ) = 0
( c == a ) = 1
( a != b ) = 1
indicates operations of rational operators( <,>,<=,>=,!=,==)
[15]
#include <stdio.h>
int main()
{
int a = 60;
int b = 13;
printf( "( a & b ) = %d \n", a & b );
printf( "( a | b ) = %d \n", a | b );
printf( "( a ^ b ) = %d \n", a ^ b );
printf( "( ~ a ) = %d \n", ~a );
printf( "( a << 2 ) = %d \n", a << 2);
printf( "( a >> 2 ) = %d \n", a >> 2);
return 0;
}
output:
( a & b ) = 12
( a | b ) = 61
( a ^ b ) = 49
( ~ a ) = -61
( a << 2 ) = 240
( a >> 2 ) = 15
indicates the operation of Bitwise operators.
[16]
#include <stdio.h>
int main()
{
int a = 21;
int c =10;;
c = a++;
printf( "Line 1 - Value of a and c are : %d - %d\n", a, c);
c = ++a;
printf( "Line 2 - Value of a and c are : %d - %d\n", a, c);
c = a--;
printf( "Line 3 - Value of a and c are : %d - %d\n", a, c);
c = --a;
printf( "Line 4 - Value of a and c are : %d - %d\n", a, c);
return 0;
}
output:
Line 1 - Value of a and c are : 22 - 21
Line 2 - Value of a and c are : 23 - 23
Line 3 - Value of a and c are : 22 - 23
Line 4 - Value of a and c are : 21 - 21
indicates operation of Increment and Decrement Operators.
[17]
#include <stdio.h>
int main()
{
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200;
c %= a;
printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
c <<= 2;
printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );
c >>= 2;
printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );
c &= 2;
printf("Line 9 - &= Operator Example, Value of c = %d\n", c );
c ^= 2;
printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
c |= 2;
printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
return 0;
}
output:
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - = Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
Line 11 - |= Operator Example, Value of c = 2
indiactes the operations of some Assignmnet operations.
[19]
#include<stdio.h>
int main()
{
int num;
int flag;
printf("Enter the Number : ");
scanf("%d",&num);
flag = ((num%2==0)?1:0);
printf ("Flag value : %d \n", flag);
return 0;
}
this program finds the flag value of a number. flag is used as a ineteger data type in this program.
[20]
#include<stdio.h>
int main()
{
int ivar = 100;
char cvar = 'a';
float fvar = 10.10;
double dvar = 18977670.10;
printf("%d\n", sizeof(ivar));
printf("%d\n", sizeof(cvar));
printf("%d\n", sizeof(fvar));
printf("%d\n", sizeof(dvar));
return 0;
}
output:
4
1
4
8
this program indicates ths operations of ivar,cvar,fvar,dvar.
ivar-integer data type, cvar as a type char, fvar as float type and dvar as type double.
[21]
#include<stdio.h>
int main()
{
int num1 = 1, num2 = 2;
int res, x, a, b;
x = (a = 2, b = 4, a+b);
res = (num1, num2);
printf("%d\n", res);
x = (a = 2, b = 4, a+b);
printf("%d\n", x);
return 0;
}
output:
2
6
this is describe the operations of comma operator.
[22]
(1 > 2 + 3 && 4) can be written as follows;
=((1 > (2 + 3)) && 4)
=((1>5) && 4)
=(0 && 4)
=0
this calculations indicates the operations of Operator Precedence & Associativity.
2. Write a program to evaluate the polynomial shown here:
3x3-5x2+6 for x = 2.55
#include <stdio.h>
int main ()
{
float x=2.55;
float result;
result=(3*(x*x*x))-(5*(x*x))+6;
printf("The result is: %f", result);
return 0;
}
output=The result is: 23.231623
3. Write a program that evaluates the following expression and displays the results
(remember to use exponential format to display the result):
(3.31 × 10-8 × 2.01 × 10-7) / (7.16 × 10-6 + 2.01 × 10-8)
#include <stdio.h>
int main ()
{
double a=3.31e-8;
double b=2.01e-7;
double c=7.16e-6;
double d=2.01e-8;
double result;
result=(a*b)/(c+d);
printf("The result is: %.15f", result);
return 0;
}
output=The result is: 0.000000000926603
4. To round off an integer i to the next largest even multiple of another integer j, the
following formula can be used:
Next_multiple = i + j - i % j
For example, to round off 256 days to the next largest number of days evenly divisible by
a week, values of i = 256 and j = 7 can be substituted into the preceding formula as
follows:
Next_multiple = 256 + 7 - 256 % 7
= 256 + 7 - 4
= 259
Write a program to find the next largest even multiple for the following values of i and j:
(Use keyboard to input values for i and j)
| i | j |
|365 | 7 |
|12258| 23 |
|996 | 4 |
#include <stdio.h>
int main ()
{
int i, j, next_largest_multiple;
printf("Enter i: ");
scanf("%i", &i);
printf("Enter j: ");
scanf("%i", &j);
next_largest_multiple= i + j - i % j;
printf("Next largest multiple : %i", next_largest_multiple );
return 0;
}
5. Write a program to input the radius of a sphere and to calculate the volume of the sphere.
Volume = 4/3*pi*radius3
#include <stdio.h>
int main ()
{
float radius, volume;
float pi=3.14;
printf("Enter radius: ");
scanf("%f", &radius);
volume=(4.0/3.0)*pi*(radius*radius*radius);
printf("The volume of the sphere: %.2f", volume);
return 0;
}
6. 100 spherical ice (cubes) of a given radius are placed in a box of a given width, length
and height. Calculate the height of the water level when the ice melts. Neglect the change
in volume when ice converts to water.
#include <stdio.h>
int main ()
{
int ice_cubes=100;
float PI=3.14;
float radius, width, lenght, height;
float ice_cube_volume, total_ice_volume, water_volume;
float box_volume, empty_volume, x;
printf("Enter radius: ");
scanf("%f", &radius);
printf("Enter box lenght: ");
scanf("%f", &lenght);
printf("Enter box width: ");
scanf("%f", &width);
printf("Enter box height: ");
scanf("%f", &height);
ice_cube_volume=(4.0/3.0)*PI*(radius*radius*radius);
total_ice_volume=ice_cube_volume*100;
box_volume=width*lenght*height;
empty_volume=box_volume-total_ice_volume;
water_volume=box_volume-empty_volume;
x=water_volume/(lenght*width);
printf("The height of the water level: %.2f", x);
return 0;
}
Comments
Post a Comment