


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























st
st
Getting acquainted with major components and general form of a C Program.
Introducing Conditional Statements.
Introduction to C Loops
Introducing Loops (while loop and do-while loop).
Loop continued (nested loop).
Introducing Multiple Function Program.
Recursion and Recursive Function.
Introducing One Dimensional Array.
Introducing Two-Dimensional Array and Nested Loop.
Introducing Array of Characters and String.
Introducing advanced topics (Structure & Array of Structures)
Introducing advanced topics (Pointer and Function)
Introducing advanced topics (Pointer and File)
Session 1
i. Getting acquainted with major components of a C program ii. Typing and Executing a C Program Mark the elements of a function : int^1 main^2 (void^3 ) { ……………..^4 …………………; ……………….; . . . ……………^5 } 1 – Return type 2 – Function name 3 – Argument / parameter list 4 – A statement 5 – Special statement defining function output/ return Function head Body of the function as a ‘block’ of codes or sequence of statements within ‘{‘ and ‘}’. /* Instruction : Observe, Type, Save and Run / / A Program to find the sum of a series of integers / #include <stdio.h> int main() { int term1, interval, numberOfTerms; int sum, lastTerm; printf("Enter the first term:"); scanf("%d", &term1); printf("Enter the interval:"); scanf("%d", &interval); printf("Enter the number of terms:"); scanf("%d", &numberOfTerms); lastTerm = term1 + (numberOfTerms - 1) interval; sum = ((term1 + lastTerm) * numberOfTerms) /2; printf("The sum is %d.", sum); return 0; } Comment Header File: stdio.h Main function headline Function body begins Memory reservation or Variable declaration Output statement Input statement Calculating and assigning (remembering) values Output statement Function output / Return Statement Function body ends
****** In C programs the main function return type is int and therefore it should return an integer value. The return value of the main function is considered the "Exit Status" of the application. The 0 exit code is a widely accepted convention for 'OK the program execution was successfull'. return 0 means no error.
a. Binary operator: + , - , / , * , % b. Unary operator: + ,- c. Relational operator: > , > = ,< , < = , = = ,! = [true will return 1 and false will return 0] d. Logical operator: && (AND) , || (OR) ,! (NOT) [true will return a nonzero value and false will return 0] e. Bitwise operator: & (AND), | (OR), ^ (XOR) , << , >> , ~ f. Increment and decrement operator: + + , - - g. Assignment operator: = , + = , * = , - = , / = , % = , & = , | = , ^ = , << = , >> = /* Instruction : Observe, Type, Compile and Run / / A simple multiple function program / #include <stdio.h> #include <math.h> int main() { float n1, n2, n3; / Variable declaration / double n4, n5; printf("\nEnter two decimal fractions:"); scanf("%f %f", &n1, &n2); n3 = sum_xy(n1, n2); / Function call with Parameters */ n4 = prdct_xy(n1, n2); n5 = sqrt(n4); printf("\nThe 1st function returns %f.", n3); printf("\nThe 2nd function returns %f.", n4); printf("\nThe square root of %f is %f.\n", n4, n5); return 0; } Inclusion of Header files main function
Session 2
i. Conditional Statements ii. Relational and Logical Operators Expression: An expression is the combination of functions, operators, commas, variables, identifiers etc. Ex: x = 0 , x + +. Statement: An expression as x = 0 , x + + or printf(...) becomes a statement when it is followed by a semicolon(;) as in x + + ; x = 0 ; printf(...) ; Braces { and }are syntactically equivalent to a single statement. if ... else: if ... else statement is used to express decisions. Formally the syntax is: if (expression) statement1; else statement2; Here the else part is optional. The expression inside if ... else statement expects a value. If the value is nonzero then it is true. If the value is 0 then it is false. /* Instruction : Observe, Type, Compile and Run / / A program to find odd or even number */ #include<stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d", &number); if(number%2==0) printf(“Number is Even”); else printf(“Number is Even”); return 0; }
else ... if: if(expression 1) statement 1 else if(expression 2) statement 2 else if(expression 3) statement 3 . . . else statement n else ... if checks its expression sequentially. When it found one expression is true then it will not check other expressions within that else ... if block.
Problem: Write a program to input a three integer number and print them ascending order. [if a user input 9,2,8 then the output of the program will be 2 8 9] /* Instruction : Observe, Type, Compile and Run / / A program to find income tax of an employee according to salary */ #include<stdio.h> int main() { int salary; float tax; printf("Enter your monthly salary: "); scanf("%d", &salary); if(salary>=9000) tax = salary * 0.2; else if(salary>=7500 && salary<9000) tax = salary * 0.1; else if(salary>=6500 && salary<7500) tax = salary * 0.05; else tax = 0; printf("Calculated tax = %.2f\n", tax); return 0; }
Session 3
i. Introduction to Loops (for loop) Loop: Loops are used in C for repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. There are three methods in C for repeating a part of program. i) Using a for statement ii) Using a while statement iii) Using a do ... while statement for Loop: Syntax of for loop is given below: Expression1, expression2 and expression3 are normally used for initialization, loop condition and loop counter increment respectively. Expression2 returns a value which may true or false. Any non-zero value means true and zero means false in this case. All three expressions (phase) are optional in for loop. /* Instruction : Observe, Type, Compile and Run / / A program to demonstrate for loop */ #include<stdio.h> int main() { int i; for(i=1;i<6;i++) { printf("\n%d ",i); i=i+1; } for(i=4;i>=1;i=i- 1 ) { printf("%d ",i) } return 0; }
Problem: Write a program to find the factorial of a given integer.
Problem: Write a program to check whether a given integer is a prime number or not.
Enter Base: 3 Enter Power: 5 Result: 243
minimum possible computational cost in the range 1 to 1000. (A Pythagorean triplet is a set of three integers i, j, k such that i^2 + j^2 = k^2. Use ‘break’ and/or ‘continue’ statements, where possible.)
in words. You have to use switch statement and repeat the whole process ‘n’ [user input] times. Note that, you can use no more than 2 switch statements with 10 cases per switch at most. Sample Input Sample Output Enter an integer [20-99]: 38 In words: Thirty Eight Enter an integer [20-99]: 23 In words: Twenty Three
Session 6
i. Introduction to Multiple Function Program /Instruction: Observe/ /*A program to demonstrate simple use of ‘global’ & ‘local’ variable and function */ #include<stdio.h> char char_f1(); void odd_even(int); int count; int main() { int num1; char ch1; count = 1; ch1 = char_f1(); num1 = ch1; printf("\nYou have entered %c,", ch1); printf(" the code of which is %d.\n", num1); odd_even(num1); printf("\nValue of global 'count': %d", count); return 0; } char char_f1() { char ch2; int count; printf("\nEnter a character:"); scanf("%c", &ch2); count = ch2; printf("\nValue of local 'count' in char_f1(): %d", count); return ch2; } void odd_even(int n) { if (n % 2 == 0) printf("\nIt is even.\n"); else printf("\nIt is odd.\n"); ++count; }
Session 7
i. Introduction to Recursion and Recursive Function A function is called recursive if a statement within the body of a function calls the same function. Sometimes called circular definition, recursion is thus the process of defining something in terms of itself.
Problem: Write a program to find the sum of a series of positive even numbers using recursion.