GEC225 Module 1
GEC225 Module 1
The following steps are involved when a computer program is used to solve a problem.
i) Analyze the problem
ii) Identify the variables involved
iii) Design the solution
iv) Write the program
v) Enter it into a computer
vi) Compile the program and correct errors
vii) Correct the logical errors if any
viii) Test the program with data
ix) Document the program
4
Declaring A Variable
◦ Declaration
◦ The name of a variable may be any valid identifier.
◦ An identifier is the formal programming term for a word that can be used to name
something.
◦ In C, identifiers may be any length, and can contain letters, numbers, and
underscores (_).
◦ They may only start with a letter or an underscore (not a number)
◦ They are case-sensitive (meaning that abc is different from Abc and ABC is different
from both of them).
◦ The variable declaration ends with a semicolon—which is used to end many
statements in C.
5
Declaring A Variable
6
Assigning a Variable
7
Expressions with Common Operators
◦ Another common operator which you may not be as familiar with is the modulus
operator, %. The modulus operator evaluates to the remainder when dividing the
first operand by the second.
◦ That is a % b (read “a modulus b”, or “a mod b” for short) is the remainder when a
is divided by b. For example, 19 % 5 = 4 because 19 / 5 = 3 with a remainder of 4
8
Expressions with Common Operators
◦ A statement such as x = x + 1;
◦ The statement above is quite common in programming.
◦ The statement means to take current value of x, add 1 to it, and update x’s value
to whatever that result is.
Example:
int a;
int b;
a = 5;
b = a % 2 - (a + 1) / 2;
9
1.0 Decision
Making in C
OUTLINE
12
Decision Making in C
13
Decision Making in C
14
Decision Making in C
15
Friday, March 31, 2023
1.1 If Statement
Decision Making in C: If Statement
Otherwise
'statement-inside' is skipped and
only 'statement-outside' is
executed.
17
Friday, March 31, 2023
Decision Making in C: If Statement
20
Friday, March 31, 2023
Example Program for If…Else Statement in C
21
Friday, March 31, 2023
Decision Making in C: If…Else Statement
The Ternary Operator ?
Exp1 ? Exp2 : Exp3;
Where: Exp1, Exp2, and Exp3 are expressions.
It can be read as – If Exp1 then Exp2 otherwise Exp3
The outcome of the the expression is determined by:
• Exp1 is evaluated. If it is true, then Exp2 becomes the
result.
• If Exp1 is false, then Exp3 becomes the result.
E.g.
6 == 4? 1:0 // Evaluate 0 since 6 is not equal to 5
Interpret the following?
6>=4?1:0 //
22
Friday, March 31, 2023
1.3 Nested If
Statement
Decision Making in C: Nested If Statement
24
#include<stdio.h>
#include<conio.h>
void main() Example Program for Nested If Statement
{
int a;
in C:
printf (“enter a number”);
scanf(“%d”,&a); Note:
if(a%5==0 && a%8==0)
The scanf() function in C is a powerful tool for reading
{
printf(“divisible by both 5 and 8”); input from the user or from a file and storing it in
} variables.
else if(a%8==00 && a%5!=0)
{ When you use the printf family of functions in C
printf(“divisible by 8”); programming to print a formatted string, these functions
} use a percent sign ( % ) to indicate the beginning of a
else if(a%5==00 && a%8!=0) format specifier.
{ For example, %d means to print an int , and %u means to
printf(“divisible by 5”);
print an unsigned int .
}
else
{ The & is a unary operator in C which returns the
printf(“divisible by none”); memory address of the passed operand. This is also
} known as address of operator, and the value of operator.
25
getch();
}
Class Exercise 1
Questions
26
Friday, March 31, 2023
Area of a Circle
#include <stdio.h>
int main() {
int radius;
float area;
radius = 6;
area = 3.14*radius*radius;
printf("Area of the Circle = %f square inches\n", area);
return(0);
}
27
Questions
28
Friday, March 31, 2023
Questions
29
Friday, March 31, 2023
Questions
30
Friday, March 31, 2023
1.4 Case Control
Statement
Decision Making in C – Case Control Statement
33
Friday, March 31, 2023
1.4.1 Switch Case Statement in C:
Switch case statements are used to execute only specific case statements based on the
switch expression.
35
Friday, March 31, 2023
Switch Case Statement in C:
Limitations of switch
• Relational operators cannot be used with switch statement. For
instance
case k>=20: //is not allowed
• Switch case variables can have only int and char data types, so float data
type is not allowed.
36
Friday, March 31, 2023
Example Program for Switch…Case Statement in C:
37
37
Friday, March 31, 2023
Example Program for Switch…Case Statement in C:
38
Friday, March 31, 2023
1.4.2. Break Statement in C
39
Friday, March 31, 2023
Break Statement in C:
40
Friday, March 31, 2023
Break Statement in C:
Class Exercise 2
Give the output of this C program
41
Friday, March 31, 2023
Break Statement in C:
Result of Exercise 2
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
42
Friday, March 31, 2023
1.4.3. Continue Statement in C
Continue statement is used to continue the next iteration of for loop, while
loop and do-while loops.
Hence, the remaining statements are skipped within the loop for that particular
iteration.
• Syntax : continue;
43
Friday, March 31, 2023
Continue Statement in C: EXAMPLE
44
44
Friday, March 31, 2023
Continue Statement in C:
Class Exercise 3
Give the output of the following
program
45
Friday, March 31, 2023
Continue Statement in C:
47
Friday, March 31, 2023
GOTO Statement in C: EXAMPLE
48
Friday, March 31, 2023
GOTO Statement in C:
Class Exercise 4
Give the output of the following program
49
Friday, March 31, 2023
Module-1 Assignment
A) Write C program for the following tasks:
1) Check Whether an Integer is Prime or Not
2) Check Whether a Number is Even or Odd
3) Check Whether a Character is Vowel or consonant
4) Check Whether the Entered Year is Leap Year or Not
5) Check Whether a Number is Positive or Negative or Zero.