Type Casting, Command Line Arguments and Defining Constants (Tutorial)

 Tutorial 08




01. Write a program to evaluate the polynomial shown here:
3x3 - 5x2 + 6 for x = 2.55

#include <stdio.h>
int main (void)
{
float x = 2.55;
float sum;
sum = 3*x*x*x - 5*x*x + 6;
printf("The value is :%.2f\n" , sum);
return 0;
}


02. 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 (void)
{
float sum;
sum = (3.31 * 10 - 8 * 2.01 * 10 - 7) / (7.16 * 10 - 6 + 2.01 * 10 - 8);
printf("The value is :%.2f\n" , sum);
return 0;
}


03. 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)


#include <stdio.h>
int main ()

{

int i;
int j;
int value = 0;
printf("Enter the number your want to round :");
scanf("%i", &i);
printf("From which number you want to round? :");
scanf("%i", &j);
value = (i+j-i%j);
printf("Value is : %i\n", value);
return 5;
}


04. 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()
#define pi 3.14

{
float radius;
float volume;
printf("Enter radius : ");
scanf("%f", &radius);
volume = 4/3 *pi * radius *radius *radius;
printf("The volume of sphere is : %.2f\n" , volume);
return 5;
}


05. 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 ()

{
float pi = 3.14;
float radius;
float width;
float height;
float length;
float volume;
float level;
printf("Enter radius of sperical : ");
scanf("%f", &radius);

printf("Enter the height of box : ");
scanf("%f", &height);

printf("Enter the length of box :");
scanf("%f", &length);

printf("Enter the width of box :");
scanf("%f", &width);

volume =(height *length *width)  - (4/3 *pi *radius *radius *radius);
level = volume / (length *width);

printf("The water level is : %.2f\n", level);
return 5;
}

06. Write a program to input your mid term marks of a subject marked out of 30 and the final
exam marks out of 100. Calculate and print the final results.
Final Result = Mid Term + 70% of Final Mark


#include <stdio.h>
int main()

{
float Final_result;
float Final_mark;
float Mid_term;
printf("Enter your Mid Term Test Marks : ");
scanf("%f", &Mid_term);
if (Mid_term <= 30)
{
printf("Enter your Final marks of Test: ");
scanf("%f", &Final_mark);
Final_result = Mid_term + (Final_mark *70 /100);
printf("Your Result is : %.2f", Final_result);
}
else
{
printf("Check your marks");
}  
return 5;
}

07. Input the source file and destination file name as command line arguments and print the
following message to standard output:
“Copy the details in <file name 1> to <file name 2> at 23.59.59”

#include <stdio.h>

int main(int argc, char *argv[]) 

{
    if (argc != 3) 
{
        printf("Usage: %s <source_file_name> <destination_file_name>\n", argv[0]);
        return 1; 
    }

    char SourceFileName = argv[1];
    char DestinationFileName = argv[2];
   
    printf("Copy the details in %s to %s at 23.59.59\n", sourceFileName, destinationFileName);

    return 0; 
}


08. Input the target rate as command line argument and compute the total commission as
follows:
Total commission = ( total sale × 0.2 × target rate ) + cash flow.
Cash flow is a constant 
 
#include<stdio.h>
int main(int argc, char * argv[])
{
const int cash_flow = 1000;
float total_commission, total_sale, target_rate;
sscanf(argv[1], "%f",target_rate);
total_sale = 100000;
total_commission = (total_sale * 0.2 * target_rate) + cash_flow;
printf ("Total Commission = %.2f", total_commission);
return 0;
}

Post a Comment

Previous Post Next Post