USE STANDARD LIBRARY FUNCTIONS FROM math.h
i. Power ii. Sqr. Root iii.Floor iv. Ceiling v. Exit
#include<stdio.h>
#include<math.h>
main()
{
int n;
double pr,n1,p,a,f,sqr,n3,n4;
do
{
printf("Enter your choice");
printf("1.Power\n2.Square Root\n3.Floor\n4.Ceiling\n5.Exit");
scanf("%d",&n);
if(n==1)
{
printf("Enter a no and power");
scanf("%lf %lf",&n1,&p);
printf("%lf",pow(n1,p));
}
if(n==2)
{
printf("Enter a no");
scanf("%lf",&a);
sqr=sqrt(a);
printf("%lf",sqr);
}
if(n==3)
{
printf("Enter a no");
scanf("%lf",&n3);
printf("%lf",floor(n3));
}
if(n==4)
{
printf("Enter a no");
scanf("%lf",&n4);
printf("%lf",ceil(n4));
}
}while(n!=5);
}
ASSIGNMENT 6)
math.h() : this function is used to perform various mathematical operations
ceil smallest integer not less than parameter
cos
cosh
exp(double x) computes e^x
fabs absolute value
floor largest integer not less than parameter
fmod floating point reamainder
log
log10
pow(x,y) computes x^y
sin
sinh
sqrt
tan
tanh
Some of the "commands" in C are not really "commands" at all but are functions. For example, we have been using printf and scanf to do input and output, and we have used rand to generate random numbers - all three are functions.
i. Power ii. Sqr. Root iii.Floor iv. Ceiling v. Exit
#include<stdio.h>
#include<math.h>
main()
{
int n;
double pr,n1,p,a,f,sqr,n3,n4;
do
{
printf("Enter your choice");
printf("1.Power\n2.Square Root\n3.Floor\n4.Ceiling\n5.Exit");
scanf("%d",&n);
if(n==1)
{
printf("Enter a no and power");
scanf("%lf %lf",&n1,&p);
printf("%lf",pow(n1,p));
}
if(n==2)
{
printf("Enter a no");
scanf("%lf",&a);
sqr=sqrt(a);
printf("%lf",sqr);
}
if(n==3)
{
printf("Enter a no");
scanf("%lf",&n3);
printf("%lf",floor(n3));
}
if(n==4)
{
printf("Enter a no");
scanf("%lf",&n4);
printf("%lf",ceil(n4));
}
}while(n!=5);
}
ASSIGNMENT 6)
TO DEMONSTRATE MENU DRIVEN PROGRAMS AND USE OF STANDARD LIBRARY FUNCTIONS
You should read following topics before starting this exercise
1) Use of Switch statement to create menus as in Assignment 3.
2) Use of while and do- while as in Assignment 4.
ctype.h : contains function prototypes for performing various operations on characters. Some commenly used functions are listed below.
FUNCTION PURPOSE
isalpha() check whether a character is alphabet
isalnum() check whether a character is alphanumeric
isdigit() check whether a character is a digit
isspace() check whether a character is space
ispunct() check whether a character is a punctuation mark
isupper() check whether a character is in uppercase
islower() check whether a character is in lowercase
toupper() converts a character to uppercase
tolower() converts a character to lowercase
math.h() : this function is used to perform various mathematical operations
ceil smallest integer not less than parameter
cos
cosh
exp(double x) computes e^x
fabs absolute value
floor largest integer not less than parameter
fmod floating point reamainder
log
log10
pow(x,y) computes x^y
sin
sinh
sqrt
tan
tanh
Menu Driven programs
A program that obtains input from a user by displaying a list of options – the menu – from which the user indicates his/her choice. Systems running menu-driven programs are commonplace, ranging from microprocessor controlled washing machines to bank cash dispensers. In the case of the cash dispenser, single keys are pressed to indicate the type of transaction (whether a receipt is wanted with the cash, or if a statement of the bank balance is required) and with many, a single key is pressed to indicate the amount of money required.
Menu-driven systems are advantageous in two ways: firstly, because input is via single key strokes, the system is less prone to user error; secondly, because only a limited range of characters are “allowed”, the way in which the input is to be entered is unambiguous. This contributes toward making the system more user-friendly. Compare command-line interface.
The Standard Library Functions
Some of the "commands" in C are not really "commands" at all but are functions. For example, we have been using printf and scanf to do input and output, and we have used rand to generate random numbers - all three are functions.
There are a great many standard functions that are included with C compilers and while these are not really part of the language, in the sense that you can re-write them if you really want to, most C programmers think of them as fixtures and fittings. Later in the course we will look into the mysteries of how C gains access to these standard functions and how we can extend the range of the standard library. But for now a list of the most common libraries and a brief description of the most useful functions they contain follows:
- stdio.h: I/O functions:
- getchar() returns the next character typed on the keyboard.
- putchar() outputs a single character to the screen.
- printf() as previously described
- scanf() as previously described
- string.h: String functions
- strcat() concatenates a copy of str2 to str1
- strcmp() compares two strings
- strcpy() copys contents of str2 to str1
- ctype.h: Character functions
- isdigit() returns non-0 if arg is digit 0 to 9
- isalpha() returns non-0 if arg is a letter of the alphabet
- isalnum() returns non-0 if arg is a letter or digit
- islower() returns non-0 if arg is lowercase letter
- isupper() returns non-0 if arg is uppercase letter
- math.h: Mathematics functions
- acos() returns arc cosine of arg
- asin() returns arc sine of arg
- atan() returns arc tangent of arg
- cos() returns cosine of arg
- exp() returns natural logarithim e
- fabs() returns absolute value of num
- sqrt() returns square root of num
- time.h: Time and Date functions
- time() returns current calender time of system
- difftime() returns difference in secs between two times
- clock() returns number of system clock cycles since program execution
- stdlib.h:Miscellaneous functions
- malloc() provides dynamic memory allocation, covered in future sections
- rand() as already described previously
- srand() used to set the starting point for rand()
No comments:
Post a Comment