In: Computer Science
c program
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).
When libraries are not given,in some cases (some compilers) warnings will appear and we will get output.But not all the time.
Question no.1
Images of the Code:
Note: If the below code is missing indentation
please refer code Images
Typed Code:
/*it is used for input and output operations*/
#include <stdio.h>
/*this is the library that needs to be used for abs()*/
#include <stdlib.h>
int main()
{
/*declaring "a" as integer*/
int a;
printf("Enter a value to return the absolute value: ");
/*taking the value from the user*/
scanf("%d",&a);
/*abs() function returns the absolute value of integer*/
int x= abs(a);
/*printing the absolute value of an integer*/
printf("The absolute value is %d",x);
return 0;
}
/*code ended here*/
Output:
Question no.2
Images of the Code:
Note: If the below code is missing indentation
please refer code Images
Typed Code:
/*it is used for input and output operations*/
#include <stdio.h>
/*this is the library that needs to be used for rand()*/
#include <stdlib.h>
int main()
{
/*LOWER values is starting value and UPPER value is last
value*/
int LOWER = 0, UPPER;
printf("Enter the value for Upper limit: ");
/*UPPER value is taken from the user*/
scanf("%d",&UPPER);
/*this formula is used to generate the random number
with in a range [LOWER,UPPER]*/
int num = (rand() % (UPPER - LOWER + 1)) + LOWER;
/*printing the random value*/
printf("%d",num);
return 0;
}
/*code ended here*/
Output:
Question no.3
Images of the Code:
Note: If the below code is missing indentation
please refer code Images
Typed Code:
/*it is used for input and output operations*/
#include <stdio.h>
/*this is the library that needs to be used for log10()*/
#include <math.h>
/*this is the library that needs to be used for exit()*/
#include <stdlib.h>
int main()
{
/*declaring double value*/
double a;
printf("Enter a value to return log10() value: ");
/*taking the value from the user*/
scanf("%lf",&a);
/*if a value is less than or equal to 0*/
if(a<=0)
{
/*exit the program*/
exit(0);
}
/*apply log10() and storing it in x*/
double x = log10(a);
/*printing the value*/
printf("log10(%lf) = %lf\n", a, x);
return 0;
}
/*code ended here*/
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!