Day – 01 Problem Solving with Flow Charts & Basics of C Programming
The following symbols are used to represent the Flow Charts Ellipse : To represent start or stop  Parellagram : To represent input/output  Rectangle : To represent the processing or calculations Â
Rhambus : To represent the conditional statements  Small Circle : To use as a connector  Arrows : For directions
1. Write an algorithm to calculate the addition of 2 numbers. start step 1: read a step 2: read b step 3: c := a+b step 4: print a step 5: print b step 6: print c stop
2. Draw the flow chart to calculate the addition of 2 numbers.
3. write an algorithm to calculate the addition of 3 numbers. start step 1: read a step 2: read b step 3: read c step 4: d := a+b+c step 5: print a step 6: print b step 7: print c step 8: print d stop Â
Rewrite the above the algorithm to represent more input and output(read or print) statements in a single line. start step 1: read a,b,c step 2: d := a+b+c step 3: print a,b,c,d stop
Draw the flow chart for addition of 3 numbers.
Algorithm 3. write an algorithm to calculate the total marks and average marks of 3 subjects ( maths,physics and chemistry) start step 1: read maths,physics,chemistry step 2: total := maths+physics+chemistry step 3: avg := total/3 step 4: print maths,physics,chemistry,total,avg stop
Draw the flow chart for the above algorithm
Algorithm 4. write an algorithm to calculate the area of a circle. start step 1: read radius step 2: area_circle := 3.14 * radius * radius step 3: print radius,area_circle stop
Draw the flow chart for the above algorithm
Algorithm 7. write an algorithm to find the biggest of two numbers. start step 1: read num1,num2 step 2: if num1 > num2 then print "num1 is big" else print "num2 is big" stop
Draw the flow chart for above algorithm
Algorithm 8. write an algorithm to find the biggest of three numbers. start step 1: read num1,num2,num3 step 2: if num1 > num2 and num1 > num3 then print "num1 is big" else if num2 > num1 and num2 > num3 then print "num2 is big" else print "num3 is big" stop
Draw the flow chart for the above algorithm
Introduction to C Programming Language C Programming Language is Procedural Programming Language It is developed by Dennis Ritchie in the year 1972 It is developed as system programming language to write operating systems and compilers.
Features of C Programming Language: Low level access to memory rich set of keywords Alphabets, Digits and Symbols keywords statements programs Alphabets : a-z, A-Z
Digits : 0-9 Symbols : ~ tilde ! Exclamatory @ at the rate # Hash(preprocessor) $ Dollar % Percentage ^ carret & ampersand * star ( parenthesis open ) parenthesis close - highfun /minus _ underscore + plus = equal : colo n
; semi colon " Double Quotes ' Single Quote < less than > greater than , comma . dot ? question mark / slash or division \ backward slash | vertical bar { brace or curley brace open } brace or curley brace close [ bracket open ] bracket close
Keywords : Keywords are the reserved words in the programming language. Each keyword in c language is having its own specific purpose and meaning. These keywords are not used for any other purpose. All the keywords are written in lower case alphabets.
The following are the keywords of C language auto break case char const or constant continue default do double else enum extern
float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while
Structure of C Programming Language
Link/Header File Include Section: In this section we can include the header libraries or we can link the other programs with the current programs We can include the header library using the preprocessor directive # includein the program
Here # symbol is known as a preprocessor and #include is a preprocessor directive. Example : #include< stdio.h > here stdio.h is a standard input/output header library. here .h indicates and header library/file.
Definition Section: Here, we define the macro's(constants) or user defined function prototypes. Global Variable Decleration Section: Here we declare the global variables required in the program.
main function: main function is compulsory in all the c programs. The program execution always begins from the main function only. (or) The starting point of C program execution is from main only.
We can write the main function as follows. main() { } : main followed by open parenthesis and closed parenthesis then immediately continued with open brace and closed brace.
We cannot write anything between the closed parenthesis and the open brace of main function. In between the open brace and closed brace of main function we write the decleration section and execution section.
Decleration Section: In the decleration section we can declare the variables required in the program. The first section of main function must be the declreration section.
Execution Section: All the executable statements can be written under the execution section. The executable statements like input, output and calculation statements.
The main function returns an integer value always, Hence we must write a return statement at the end of main function. We can write the return statement using the keyword return. example: return 0;
User defined functions: We can write zero or n number of user defined functions in the C program.
Datatypes : A datatype specifies the compiler that a type of value is stored in the variable.
Datatypes : 1. Predefined Datatypes /Basic Datatypes i . integer types - short int - int - long int ii. real types - float - double - long double iii. charecter types - char
2. Derived Datatypes i . arrays ii. strings iii. functions iv. pointers
3. User Defined Datatypes i . structures ii. unions iii. enumerations
Datatypes sizes and range:
Identifier: An identifier is a name of variable, constant, pointer, array, string, function, structure and union.
Rules for defining the identifiers: 1. we can use all the lower case alphabets from a to z, digits from 0 to 9 and only one symbol underscore _ in defining the identifier name. 2. we can start with either any alphabet from lower case a to z or _ symbol. 3. we cannot start with any digit. 4. we cannot give any white spaces( space,tab or newline charecter ) in the identifier name. 5. The length of an identifier will be from one charecter to 36 charecters .
Ex: a student_name gross_salary _subject result_1
Variable: A variable is a named memory location to store the values. We can declare the variables using the datatype as follows. syntax: datatype variablename ;
ex: int num1; int num2; int num3; (or) int num1,num2,num3; num1=10; num2=20; num3=num1+num2;
Input/Output functions: In C language the input/output functions are available in the stdio.h (standard input/output header library). The input/output functions are classified into 2 categories. 1. Formatted Input/Output Functions 2. Unformatted Input/Output Functions
Formatted Input/Output Functions: In Formatted Input/Output functions we should specify the format specifier to make the compiler to understad the type of values used to read/write.
The following are the Formatted input/output functions 1. printf () 2. scanf () 3. fprintf () 4. fscanf ()
printf (): This function used to display the output on the console. The following the is the syntax to write the printf () function. syntax: printf (" Formatstring",list of variables);
Format string is divided into 2 parts they are i . Text or Message : Some message or text to be displayed on console. If no text to be displayed then we cannot write any text. ii. Format specifiers : Format specifiers of corresponding datatype will be written. List of Variables: The variables list to be displayed on the console. When there is more than one variable to be displayed, each variable should be separted by comma (,) operator.
ex:1 int a; => %d float b; => %f char c; => %c
printf ("the values are % d%f%c",a,b,c ); printf ("the values are % d%f%c",b,c,a ); <= Its incorrect [Mismatch of datatypes ] printf ("the values are % d%f%c",a,b ); <= Its incorrect [Mismatch of Format specifiers and variables number]
Note: > When we give different types of variables we must follow the order of format specifiers with the order of variables list. > The number of variables must be matched with the number of format specifiers .
ex:2 int a,b ; printf ("% d%d",a,b ); 2. scanf (): This function will read the input from the standard input device(key board) to the variables. syntax: scanf ("Format Specifiers",List of variables);
Ex:1 int a,b ; scanf ("% d%d",&a,&b ); Here the variables must preceed with address(&) operator. All the rules of printf () are applicable here.
Ex:2 int a; float b; char c; scanf("%d%f%c",&a,&b,&c);
3. fprintf (): This function will writes the data to the file. syntax: fprintf ( filepointer,"Format String",List of variables);
Ex: fprintf ( stdout ,"% d",a ); Here stdout is standard output device You can also specify any other file pointer
4. fscanf (): This function will read the data from a file. syntax: fscanf ( filepointer,"Format Specifiers",List of variables);
Ex: fscanf ( stdin ,"% d",&a ); Here stdin is a standard input device(keyboard) You can also specify any other file pointer.
2. Unformatted Input/Output Functions: These functions does not required to specify the format specifier . The following are the unformatted input/output functions. 1. getchar () 2. putchar () 3. getc ()/ fgetc () 4. putc ()/ fputc () 5. gets() 6. puts()
1. getchar (): This function will read a single charecter from the keyboard. ex: char ch ; ch = getchar (); 2. putchar (): This function will display a single charecter on the console. ex: putchar ( ch );
3. getc (): This function will read a single charecter from the file. ex: char ch ; ch = getc ( fp ); here fp is a file pointer from which we need to read a single charecter .
4. putc (): This function will write a single charecter to the file. ex:putc ( ch,fp ); here fp is file pointer to which we need to write a single charecter .
5. gets(): This function will read a string. A string is a collection of charecters . ex:gets ( str ); here str is a string variable. 6. puts() This function will display the string on the console. ex:puts ( str ); here str is a string variable.
Comment lines: Comment lines are ignored by the compiler. These are for the information purpose to the programmer The comment lines can be written as a single line or multiple lines as per theneed . We can write the comment line in C Program between /* and */ symbol.
Ex: /* its a declerative statement */ Note: 1. All the statements in C language should be written in lower case alphabets only. C language is a case sensitive language. 2. All the statements must be terminated with semi colon.
write a program to display a welcome message
To erase the previous output on the console we can use clrscr () function inthe program.