[go: up one dir, main page]

0% found this document useful (0 votes)
10 views25 pages

U-2, C-1 (Input and Output)

The document provides algorithms and examples for writing C programs, specifically focusing on finding the roots of quadratic equations and managing input/output operations. It details the steps to calculate roots based on the discriminant and explains functions for reading and writing characters, formatted input, and output. Additionally, it covers the use of scanf and printf for handling various data types and formatting specifications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

U-2, C-1 (Input and Output)

The document provides algorithms and examples for writing C programs, specifically focusing on finding the roots of quadratic equations and managing input/output operations. It details the steps to calculate roots based on the discriminant and explains functions for reading and writing characters, formatted input, and output. Additionally, it covers the use of scanf and printf for handling various data types and formatting specifications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Programs

◼ Write a C Program to find the roots of a


quadratic equation.
◼ Write a C Program which uses looping
statements.
Quadratic Equation (QE)
Algorithm: Roots of a QE
Step 1: Input coefficients ‘a’, ’b’ and ‘c’.
Step 2: Check if a is 0, if true, please “Invalid
input”.
Step 3: Find discriminant d=b2-4ac.
Step 4: if d is 0, find roots, r1=r2=-b/2a,
Display r1 and r2
Step 5: if d is greater than 0, find roots,
r1= (−b+√(b2 − 4ac))/2a, r2= (−b-√(b2 − 4ac))/2a.
Display r1 and r2;
Quadratic Equation (QE)
Step 6: if d is less than 0, find real part and
imaginary part.
 real_part = -b/2a;
 Imag_part = sqrt(abs(b2 − 4ac))/2a
 r1= real_part + i * imag_part
 r2= real_part - i * imag_part
 Display r1 and r2

◼ Step 7: Stop
Managing Input and
Output Operations
Agenda
◼ Reading a Character
◼ Writing a Character
◼ Formatted Input
◼ Formatted Output
Reading a Character
◼ To read a single character from the
standard input device, getchar function
can be used.
var_name= getchar();
◼ Example:
char ch;
ch=getchar();
◼ getchar() can be used inside a loop with
proper terminating condition, to accept
multiple characters.
◼ Functions related to characters are
 isalnum(c) - Is ‘c’ an alphanumeric
character?
 isalpha(c) - alphabet
 isdigit(c) - digit
 islower(c) - lower case letter
 isprint(c) - printable character
 ispunct(c) - punctuation mark
 isspace(c) - white space
 isupper(c) - upper case letter

◼ ctype.h header file should be mentioned in


the program
Writing a Character
◼ To print a single character on the standard
output device, putchar function is used.
putchar(var_name);
ch=‘Y’;
putchar(ch);
◼ To print a new line character
putchar(‘\n’);
Formatted Input
◼ We go for formatted input when we have to
input multiple values of different types
Example: 15.75 123 John
◼ scan formatted (scanf) is the formatted
input statement.
Syntax:
scanf(“control string”,arg1,arg2,arg3,…argn);
◼ Control string specifies the format of the
data and arg1, arg2…argn specifies the
address or the location.
◼ Control string may contain numbers
specifying the numbers of characters to be
inputted.
◼ Inputting Integer Numbers
 Field specification will contain
%wd
 w- integer number specifying the field width
 d- integer number will be saved
scanf (“%2d %5d”, &n1,&n2);
◼ If input is 50 31426, then 50 is assigned
to n1 and 31426 is assigned to n2.
◼ If input is 31426 50, then 31 is assigned
to n1 and 426 is assigned to n2. 50 is kept
in the buffer, and used in the next scanf
statement.
◼ If scanf statement encounters incorrect
input then it stops taking further inputs.
◼ Values can be skipped using * symbol.
scanf (“%d %*d %d”, &a, &b);
◼ If the input line is 123 456 789, then 123 is
assigned to a, 456 is skipped and 789 is
assigned to b.
◼ “ld” indicates that number is long integer
and “h” indicates that number is short
integer.
◼ Do not use unnecessary symbols in scanf
statement.
Example: scanf (“%d-%d”,&n1,&n2);
◼ For the above scanf statement the inputs
will be successfully read only if the inputs
are given as 4-2 or 8-6, i.e First value
followed by ‘-’ symbol and followed by
second values.
◼ Inputting Real Numbers
 Field width is not specified in case of floating
point values.
scanf (“%f %f %f”, &x,&y,&z);
 To read double values, use %lf instead of %f
 To skip values use ‘ * ’ symbol.
◼ Inputting Character Strings
 %ws or %wc is used to input specified
number of characters.
 Some versions of scanf also support following
formats

%[characters] - accepts these characters

%[^characters] - Do not accept these


characters
◼ Reading Mixed Data Types
 To achieve this the order and the type should
be specified correctly
 scanf(“%d %c %f %s”, &n1,&ch,&n2,name);
 scanf returns the number of values
successfully read. This can be used for Error
detection in inputs.
X=scanf(“%d %c %f %s”, &n1,&ch,&n2,name);
 Ifall the inputs are taken successfully then x
will have the values 4.
Formatted Output
◼ The printf statement is used to display the
results in the required alignment and
format
◼ Syntax:
printf(“control string”,arg1,arg2,…argn);
◼ Control string can have characters, Format
specifications and Escape sequence
characters
% w.p type-specifier
◼ w- an integer values specifying total width
◼ p- integer value that specifies the number
of digits after decimal point
◼ Output of Integer values
% wd
 w is the width that has to printed, if the
original values is more than w, then the whole
values is printed.
printf(“%d”, 9876); 9 8 7 6
printf(“%6d”, 9876); 9 8 7 6
printf(“%2d”,9876); 9 8 7 6
printf(“%-6d”, 9876); 9 8 7 6
printf(“%06d”,9876); 0 0 9 8 7 6
◼ Default is right justified, to left justify it we
use ‘-’ symbol.
◼ To add preceding 0s we use 0 in the
control string
◼ Output of Real Numbers
% w.p f
 w- min number of positions to be displayed
 p- number of digits after decimal point
 If y=98.7654
printf(“%7.4f”,y); 9 8 . 7 6 5 4
printf(“%7.2f”,y); 9 8 . 7 7
printf(“%-7.2f”,y); 9 8 . 7 7
printf(“%e”,y); 9 . 8 7 6 5 4 0 e + 0 1
printf (“*.*f”, w,p,y);
 User can define the field size at run time.
◼ Printing a Single Character
 To print a single character, we use %w c
 Default is right justified, ‘ – ‘ is used to make it
left justified
◼ Printing of Strings
 Format is like printing real numbers
% w.ps
◼ w- specifies the field width
◼ p- indicates the number of characters to
be printed
◼ Default is right justified, ‘ - ’ is used to
make it left justified.
(Please refer text book for example)
◼ Mixed Data Output
 printf is used to print mixed data values as
shown below
printf (“%d %f %s %c”, a,b,c,d);
 printf returns the number of characters
printed.

You might also like