Engineering Problem Solving with C

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Basic Input/Output and Variables Ethan Cerami New York
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Original Author : Turgay Korkmaz web: Revised for ME2008 Spring—(2009_ME2008a_C.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Topic:  Reading Input  identifier,  if statement,  Arithmetic Operation CSE 102 – Programming Fundamentals.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Lecture2.
Variables, Operators, and Expressions
Simple C Programs.
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 1.2 Introduction to C++ Programming
Arithmetic Expressions
CSCE 206 Structured Programming in C
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
BIL 104E Introduction to Scientific and Engineering Computing
Tokens in C Keywords Identifiers Constants
TMF1414 Introduction to Programming
Revision Lecture
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Copyright © 2012 Pearson Education, Inc.
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
A First Book of ANSI C Fourth Edition
Introduction to C Programming
Lectures on Numerical Methods
Assignment Operators Topics Increment and Decrement Operators
Chapter 2 - Introduction to C Programming
Assignment Operators Topics Increment and Decrement Operators
Lecture3.
elementary programming
Topics Designing a Program Input, Processing, and Output
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

Engineering Problem Solving with C CSCI-125/ENGR-144 Engineering Problem Solving with C Ch 2

2.1 Program Structure

{ } /*-----------------------------------------*/ Comments /* Program chapter1_1 */ /* This program computes the */ /* distance between two points. */ #include <stdio.h> #include <math.h> int main(void) { /* Declare and initialize variables. */ double x1=1, y1=5, x2=4, y2=7, side_1, side_2, distance; /* Compute sides of a right triangle. */ side_1 = x2 - x1; side_2 = y2 - y1; distance=sqrt(side_1*side_1 + side_2*side_2); /* Print distance. */ printf("The distance between the two " "points is %5.2f \n", distance); return 0; /* Exit program. */ } Comments Preprocessor, standard C library every C program must have main function, this one takes no parameters and it returns int value {  begin Variable declarations, initial values (if any) Statements must end with ; indentation return 0 }  end of function

General Form The main function contains two types of commands: declarations and statements Declarations and statements are required to end with a semicolon (;) Preprocessor directives do not end with a semicolon To exit the program, use a return 0; statement preprocessing directives int main(void) { declarations statements }

Another Program /***************************************************/ /* Program chapter1 */ /* This program computes the sum of two numbers */ #include <stdio.h> int main(void) { /* Declare and initialize variables. */ double number1 = 473.91, number2 = 45.7, sum; /* Calculate sum. */ sum = number1 + number2; /* Print the sum. */ printf(“The sum is %5.2f \n”, sum); system("pause"); /* keep DOS window on the screen*/ return 0; /* Exit program. */ } /****************************************************/

2.2 Constants and Variables What is a variable in math? f(x) = x2+x+4 In C, A variable is a memory location that holds a value An identifier or variable name is used to reference a memory location.

Memory double x1=1,x2=7,distance; 1 = 00000001 7 = 00000111 name address Memory - content … 11 12 13 14 15 16 1 = 00000001 7 = 00000111 ? = 01001101 How many memory cells does your computer have? Say it says 2Gbyte memory? 1K=103 or 210 = 1024 1M=106 or 220 = 10242 1G=109 or 230 = 10243 x1 x2 distance

Memory Snapshot Name Addr Content x1 1 y1 5 x2 4 y2 7 side_1 ? side_2 distance

Rules for selecting a valid identifier (variable name) Must begin with an alphabetic character or underscore (e.g., abcABC_) May contain only letters, digits and underscore (no special characters ^%@) Case sensitive (AbC, aBc are different) Cannot use C keywords as identifiers (e.g., if, case, while)

Are the following valid identifiers? initial_time DisTaNce X&Y distance 1x x_1 rate% x_sum switch

C Numeric Data Types

Example Data-Type Limits

C Character Data Type: char char result =‘Y’; In memory, everything is stored as binary value, which can be interpreted as char or integer. Examples of ASCII Codes

Memory name address Memory - content How to represent ‘a’ ? char My_letter=‘a’; int My_number = 97 Always we have 1’s and 0’s in the memory. It depends on how you look at it? For example, 01100001 is 97 if you look at it as int, or ‘a’ if you look at it as char ‘3’ is not the same as 3 1 2 3 4 5 6 … ‘a’= 01100001 97 = 01100001 ? = 01001101 My_letter My_number

Program to Print Values as Characters and Integers

Standard Output printf Function prints information to the screen requires two arguments control string Contains text, conversion specifiers or both Identifier to be printed Example double angle = 45.5; printf(“Angle = %.2f degrees \n”, angle); Output: Angle = 45.50 degrees Conversion Specifier Control String Identifier

Constants A constant is a specific value that we use in our programs. For example 3.14, 97, ‘a’, or “hello” In your program, int a = 97; char b =‘a’; double area, r=2.0; double circumference; area = 3.14 * r*r; circumference = 2 * 3.14 * r; 01100001 ? 2.0 a b area circumference r

Symbolic Constants What if you want to use a better estimate of ? For example, you want 3.141593 instead of 3.14. You need to replace all by hand  Better solution, define  as a symbolic constant, e.g. #define PI 3.141593 … area = PI * r * r; circumference = 2 * PI * r; Defined with a preprocessor directive Compiler replaces each occurrence of the directive identifier with the constant value in all statements that follow the directive

2.3 Assignment Statements Used to assign a value to a variable General Form: identifier = expression; /* ‘=‘ means assign expression to identifier */ Example 1 double sum = 0; Example 2 int x; x=5; Example 3 char ch; ch = ‘a’; 5 ‘a’ sum x ch

Assignment examples (cont’d) int x, y, z; x = y = 0; right to left! Z = 1+1; Example 4 y=z; y=5; x y z 2 2 5

Assignment examples with different types int a, b=5; double c=2.3; … a=c; /* data loss */ c=b; /* no data loss */ ? 5 2.3 2 a b c 5.0 long double, double, float, long integer, integer, short integer, char Data may be lost. Be careful!  No data loss

Exercise: swap Write a set of statements that swaps the contents of variables x and y x 3 x 5 y 5 y 3 Before After

Exercise: swap First Attempt x=y; y=x; After x=y 5 x y After y=x 5 x y 3 5 x y Before

Exercise: swap Solution temp= x; x=y; y=temp; after temp=x after x=y after y = temp 3 temp 5 x y ? temp 3 5 x y Before Will the following solution work, too? temp= y; y=x; x=temp; Write a C program to swap the values of two variables

Arithmetic Operators Addition + sum = num1 + num2; Subtraction - age = 2007 – my_birth_year; Multiplication * area = side1 * side2; Division / avg = total / number; Modulus % lastdigit = num % 10; Modulus returns remainder of division between two integers Example 5%2 returns a value of 1 Binary vs. Unary operators All the above operators are binary (why) - is an unary operator, e.g., a = -3 * -4

Arithmetic Operators (cont’d) Note that ‘id = exp‘ means assign the result of exp to id, so X=X+1 means first perform X+1 and Assign the result to X Suppose X is 4, and We execute X=X+1 4 X 5

Integer division vs Real division Division between two integers results in an integer. The result is truncated, not rounded Example: int A=5/3;  A will have the value of 1 int B=3/6;  B will have the value of 0 To have floating point values: double A=5.0/3;  A will have the value of 1.666 double B=3.0/6.0;  B will have the value of 0.5

Implement a program that computes/prints simple arithmetic operations Declare a=2, b=5, c=7, d as int Declare x=5.0, y=3.0, z=7.0, w as double d = c%a Print d d = c/a Print d w = z/x Print w d = z/x Print d w = c/a Print w a=a+1 Print a … try other arithmetic operations too..

Exercise int a=27, b=6, c; c = b%a; 2. int a=27, b=6; float c; c = a/(float)b; 3. int a; float b=6, c=18.6; a = c/b; 4. int b=6; float a, c=18.6; a = (int)c/b;

Mixed operations and Precedence of Arithmetic Operators int a=4+6/3*2;  a=? int b=(4+6)/3*2;  b=? a= 4+2*2 = 4+4 = 8 b= 10/3*2 = 3*2= 6 5 assign = Right to left

Extend the previous program to compute/print mixed arithmetic operations Declare a=2, b=5, c=7, d as int Declare x=5.0, y=3.0, z=7.0, w as double d = a+c%a Print d d = b*c/a Print d w = y*z/x+b Print w d = z/x/y*a Print d w = c/(a+c)/b Print w a=a+1+b/3 Print a … try other arithmetic operations too..

Increment and Decrement Operators Increment Operator ++ post increment x++; pre increment ++x; Decrement Operator -- post decrement x--; pre decrement --x; } x=x+1; } x=x-1; But, the difference is in the following example. Suppose x=10; A = x++ - 5; means A=x-5; x=x+1; so, A= 5 and x=11 B =++x - 5; means x=x+1; B=x-5; so, B=6 and x=11

Abbreviated Assignment Operator operator example equivalent statement += x+=2; x=x+2; -= x-=2; x=x-2; *= x*=y; x=x*y; /= x/=y; x=x/y; %= x%=y; x=x%y; !!! x *= 4+2/3  x = x*4+2/3 wrong x=x*(4+2/3) correct

Precedence of Arithmetic Operators (updated)

Writing a C statement for a given MATH formula Area of trapezoid area = base*(height1 + height2)/2; How about this

Exercise Tension = 2*m1*m2 / (m1 + m2) * g wrong Tension = 2*m1*m2 / (m1 + m2) * g Write a C statement to compute the following f = (x*x*x-2*x*x+x-6.3)/(x*x+0.05*x+3.14);

Exercise: Arithmetic operations ? 5 a b c x y Show the memory snapshot after the following operations by hand int a, b, c=5; double x, y; a = c * 2.5; b = a % c * 2 - 1; x = (5 + c) * 2.5; y = x – (-3 * a) / 2; Write a C program and print out the values of a, b, c, x, y and compare them with the ones that you determined by hand. a = 12 b = 3 c= 5 x = 25.0000 y = 43.0000

Exercise: Arithmetic operations Show how C will perform the following statements and what will be the final output? int a = 6, b = -3, c = 2; c= a - b * (a + c * 2) + a / 2 * b; printf("Value of c = %d \n", c);

Step-by-step show how C will perform the operations output: Value of c = 27

Step-by-step show how C will perform the operations int a = 8, b = 10, c = 4;   c = a % 5 / 2 + -b / (3 – c) * 4 + a / 2 * b; printf("New value of c is %d \n", c);

2.4 Standard Input and Output Output: printf Input: scanf Remember the program computing the distance between two points! /* Declare and initialize variables. */ double x1=1, y1=5, x2=4, y2=7, side_1, side_2, distance; How can we compute distance for different points? It would be better to get new points from user, right? For this we will use scanf To use these functions, we need to use #include <stdio.h>

Standard Output printf Function prints information to the screen requires two arguments control string Contains text, conversion specifiers or both Identifier to be printed Example double angle = 45.5; printf(“Angle = %.2f degrees \n”, angle); Output: Angle = 45.50 degrees Conversion Specifier Control String Identifier

Conversion Specifiers for Output Statements Frequently Used

Standard Output Output of -145 Output of 157.8926 Specifier Value Printed %i -145 %4d %3i %6i __-145 %-6i -145__ %8i ____-145 %-8i -145____ Specifier Value Printed %f 157.892600 %6.2f 157.89 %7.3f 157.893 %7.4f 157.8926 %7.5f 157.89260 %e 1.578926e+02 %.3E 1.579E+02

Special Characters \n newline each escape sequence \t tab represents only one \b backspace character \\ backslash \? Question mark \’ Single quote \” Double quote /*what does this print? */ printf(“\”The End.\” \n”); printf(“\nA\nB\tC\nDE\aF\n”); In addition to character constants and decimal values stored in chars, certain special characters can be produced via escape sequences. ASCII value could be used \a or char bell = 7; \n or char newl = 10; \f or char form = 12;

Exercise int sum = 65; double average = 12.368; char ch = ‘b’; Show the output line (or lines) generated by the following statements. printf("Sum = %5i; Average = %7.1f \n", sum, average); printf("Sum = %4i \n Average = %8.4f \n", sum, average); printf("Sum and Average \n\n %d %.1f \n", sum, average); printf("Character is %c; Sum is %c \n", ch, sum); printf("Character is %i; Sum is %i \n", ch, sum);

Exercise (cont’d) Solution Sum = 65; Average = 12.4 Sum = 65 Sum and Average 65 12.4 Character is b; Sum is A Character is 98; Sum is 65

Standard Input scanf Function inputs values from the keyboard required arguments control string memory locations that correspond to the specifiers in the control string Example: double distance; char unit_length; scanf("%lf %c", &distance, &unit_length); It is very important to use a specifier that is appropriate for the data type of the variable

scanf() int customer_status = 2; 2 ? Scanf() can scanf(“%d”, &customer_status); before scanf() after scanf() address operator only conversion code(s) no plain text customer_status ? Scanf() can input all types of data input only specific characters Skip or exclude specific characters new value is whatever was keyed by user

Conversion Specifiers for Input Statements Frequently Used

Sample Code #include <stdio.h> main() { int customer_status = 2; output would be as follows: Starting with Status = 2 Enter new status: 5(value entered by user) New status value is 5 #include <stdio.h> main() { int customer_status = 2; printf(“Starting with Status = %d\n”, customer_status); printf(“Enter new status: “); /* prompt user */ scanf(“%d”, &customer_status); printf(“\nNew status value is %d\n”, customer_status); } Identify parts of the program, variables, etc. Note use of preprocessor statement; Use in all programs & we will discuss later Point out printf() uses value of variable, while scanf() uses & to indicate address of variable, not value Indicate still a very simple program with much missing

Data Stream with scanf() main() { char letter1, letter2, letter3; int num1, num2; float fpnum; double dblnum; scanf(“%c”, &letter1); scanf(“%c”, &letter2); scanf(“%c”, &letter3); scanf(“%d”, &num1); scanf(“%f”, &fpnum); scanf(“%lf”, &dblnum); printf(“int num is %d\n”, num1); printf(“fpnum is %f”, fpnum); printf(“letter1 is %c, value %d\n”, letter1, letter1); } How would this program work with the following input data? (The box represents the input data stream, or buffer) represents ‘Enter’ key

Data Stream with scanf() input data stream Why is this needed? scanf(“%c”, &letter1); scanf(“%c”, &letter2); scanf(“%c”, &letter3); scanf(“%d”, &num1); scanf(“%f”, &fpnum); scanf(“%lf”, &dblnum); printf(“int num is %d\n”, num1); printf(“fpnum is %f\n”, fpnum); printf(“letter1 is %c, value %d\n”, letter1, letter1);

Exercise int i; scanf(“%f %i“, &f, &i); float f; int i; scanf(“%f %i“, &f, &i); What will be the values stored in f and i after scanf statement if following values are entered 12.5 1 12 45 12 23.2 12.1 10 12 1

Syntax Errors You have broken the C "rules of grammar" The compiler may report several errors for one mistake (That's C for you) Fix the syntax errors one at a time and recompile Don't despair! Warnings ought to be fixed too.

Compiler Listing with Syntax Errors 1 /* Calculates and displays the area and circumference of a circle */ 2 #include <stdio.h> 268 #define PI 3.14159 269 270 int 271 main(void) 272 { 273 double area, circum 275 /* Get circle radius */ 276 printf("Enter radius> "); ***** Semicolon added at the end of the previous source line

Compiler Listing with Syntax Errors 277 scanf("%lf", &radius); ***** Identifier "radius" is not declared within this scope ***** Invalid operand of address-of operator 278 279 /* Calculate the area */ 280 area = PI * radius * radius;

Compiler Listing with Syntax Errors 281 282 /* Calculate the circumference */ 283 circum = 2 * PI * radius; ***** Identifier "radius" is not declared within this scope 284 285 /* Display the area and circumference * / 286 printf("The area is %f\n", area); 287 printf("The circumference is %f\n", circum); 288 return (0); 289 } ***** Unexpected end-of-file encountered in a comment ***** "}" inserted before end-of-file

Link-Time Errors After compilation, there may be link errors Inconsistent function names No "main" More than one "main" The compiler being awkward (save and re-invoke the studio)

Run-Time (Semantic) Errors The program does not do what you want This is nearly always your own error Look carefully through the code Else use the debugger (we meet this later)

111 #include <stdio.h> 262 263 int 264 main(void) 265 { 266 int first, second; 267 double temp, ans; 268 printf("Enter two integers> "); 269 scanf("%d%d", &first, &second); 270 temp = second + first; 271 ans = first / temp; 272 printf("The result is %.3f\n", ans); 273 274 return (0); 275 } Enter two integers> -3 3 Arithmetic fault, divide by zero at line 272 of routine main

Undetected Errors Incorrect results probably due to Character and Numeric scanning problem Incorrect results probably due to & omission Debugging a problem is very time-consuming Plan your program carefully and check them to eliminate bugs before running the program

/* Incorrect revision of welcoming message program */ #include <stdio.h> /* printf, scanf definitions */ int main(void) { char letter_1, letter_2, letter_3; /* three letters */ int year; /* current year */ printf("Enter the current year and press return> "); scanf("%d", &year); printf("Enter a 3-letter nickname and press return> "); scanf("%c%c%c", &letter_1, &letter_2, &letter_3); printf("Welcome, %c%c%c. %d is a great year to study C!\n", letter_1, letter_2, letter_3, year); return (0); } Enter the current year and press return> 1994 Enter a 3 letter nickname and press return> Bob Welcome, Bo. 1994 is a great year to study C!

#include <stdio.h> int main(void) { int first, second, sum; printf("Enter two integers> "); scanf("%d%d", first, second); /* ERROR!! should be &first, &second */ sum = first + second; printf("%d + %d = %d\n", first, second, sum); return (0); } Enter two integers> 14 3 5971289 + 5971297 = 11942586

Library Functions

2.7 Math Functions #include <math.h> fabs(x) Absolute value of x. sqrt(x) Square root of x, where x>=0. pow(x,y) Exponentiation, xy. Errors occur if x=0 and y<=0, or if x<0 and y is not an integer. ceil(x) Rounds x to the nearest integer toward  (infinity). Example, ceil(2.01) is equal to 3. floor(x) Rounds x to the nearest integer toward - (negative infinity). Example, floor(2.01) is equal to 2. exp(x) Computes the value of ex. log(x) Returns ln x, the natural logarithm of x to the base e. Errors occur if x<=0. log10(x) Returns log10x, logarithm of x to the base 10. Errors occur if x<=0.

Exercise Evaluate the following: floor(15.8) floor(15.8+0.5) ceil(-7.2) *pow(4.0,2.0) sqrt(floor(fabs(-16.8))) log10(1000.0)

Trigonometric Functions sin(x) Computes the sine of x, where x is in radians. cos(x) Computes the cosine of x, where x is in radians tan(x) Computes the tangent of x, where x is in radians. asin(x) Computes the arcsine or inverse sine of x, where x must be in the range [-1, 1]. Returns an angle in radians in the range [-/2,/2]. acos(x) Computes the arccosine or inverse cosine of x, Returns an angle in radians in the range [0, ]. atan(x) Computes the arctangent or inverse tangent of x. The Returns an angle in radians in the range [-/2,/2]. atan2(y,x) Computes the arctangent or inverse tangent of the value y/x. Returns an angle in radians in the range [-, ].

Parameters or Arguments of a function A function may contain no argument or contain one or more arguments If more than one argument, list the arguments in the correct order Be careful about the meaning of an argument. For example, sin(x) assumes that x is given in radians, so to compute the sin of 60 degree, you need to first conver 60 degree into radian then call sin function: #define PI 3.141593 theta = 60; theta_rad = theata * PI / 180; b = sin(theta_rad); /* is not the same as sin(theta); */

Exercise velocity = sqrt(vo*vo+2*a*(x-xo)); Write an expression to compute velocity using the following equation Assume that the variables are declared velocity = sqrt(vo*vo+2*a*(x-xo)); velocity = sqrt(pow(vo,2)+2*a*(x-xo));

Exercise center = (38.19*(pow(r,3)-pow(s,3))*sin(a))/ Write an expression to compute velocity using the following equation Assume that the variables are declared Make sure that a is given in radian; otherwise, first convert it to radian center = (38.19*(pow(r,3)-pow(s,3))*sin(a))/ ((pow(r,2)-pow(s,2))*a); center = (38.19*(r*r*r - s*s*s)*sin(a))/((r*r –s*s)*a);

Exercise: Compute Volume Write a program to compute the volume of a cylinder of radius r and height h r h

Solution: Compute Volume Problem Solving Methodology 1. Problem Statement 2. Input/Output Description 3. Hand Example 4. Algorithm Development 5. Testing

Solution: Compute Volume (cont’d) Problem Statement compute the volume of a cylinder of radius r and height h Input Output Description radius r volume v height h

Solution: Compute Volume (cont’d) Hand example r=2, h =3, v=37.68 Algorithm Development Read radius Read height Compute Volume Print volume Convert to a program (see next slide)

Solution: Compute Volume (coding) #include <stdio.h> #define PI 3.141593 int main(void) { /* Declare Variables */ double radius, height, volume; printf("Enter radius: "); scanf("%lf",&radius); printf("Enter height: "); scanf("%lf",&height); /* Compute Volune */ volume = PI*radius*radius*height; /* Print volume */ printf("Volume = %8.3f \n", volume); system("pause"); exit(0); }

Exercise Write a program to find the radius of a circle given its area. Read area from user. Compute radius and display it. r

2.8 Character Functions #include <ctype.h> putchar(‘a’); C= getchar(); toupper(ch) If ch is a lowercase letter, this function returns the corresponding uppercase letter; otherwise, it returns ch isdigit(ch) Returns a nonzero value if ch is a decimal digit; otherwise, it returns a zero. islower(ch) Returns a nonzero value if ch is a lowercase letter; otherwise, it returns a zero. isupper(ch) Returns a nonzero value if ch is an uppercase letter; otherwise, it returns a zero. isalpha(ch) Returns a nonzero value if ch is an uppercase letter or a lowercase letter; otherwise, it returns a zero. isalnum(ch) Returns a nonzero value if ch is an alphabetic character or a numeric digit; otherwise, it returns a zero.

Exercise What is the output of the following program #include <stdio.h> #include <ctype.h> int main(void) { char ch1='a', ch2; char ch3='X', ch4; char ch5='8'; ch2 = toupper(ch1); printf("%c %c \n",ch1,ch2); ch4 = tolower(ch3); printf("%c %c \n",ch3,ch4); printf("%d\n",isdigit(ch5)); printf("%d\n",islower(ch1)); printf("%d\n",isalpha(ch5)); system("pause"); return(0); }