Introductory Theory
Introductory Theory
1
Computer Programs
Computer programming is the process that professionals use to write code that
instructs how a computer, application or software program performs. At its most
basic, computer programming is a set of instructions to facilitate specific actions.
So, it -
Transformed into
Our ‘logical’ step Problem to be
programming
by step solution solved
techniques!
Transformed into
Our ‘logical’ step Problem to be
programming
by step solution solved
techniques!
Step 2 Step 1
Use Variables, If-else, We’ll use just “steps” or Flow Charts, Pseudocodes, algorithms
loops, arrays, pointers,
functions, structures,
classes etc. Step 3
We write in
Programming
Languages
What is Computer Programming?
int main()
{
printf(“Hello World”);
}
Simple C Program
Output of the program???
#include<stdio.h>
int main(){
printf(“Hello World”);
How to solve this??
printf(“Hello IPE 06”);
We can use “\n” to print a
} new line.
Simple C Program
Output of the program???
#include<stdio.h>
int main(){
printf(“Hello World”);
printf(“\n”);
printf(“Hello IPE 06”);
}
An Alternative of the Previous Program
Output of the program???
#include<stdio.h>
int main(){
printf(“Hello World\n”);
printf(“Hello IPE 05”);
}
Task
1. Print “Hello World” using printf () function.
2. Print a number using printf () function.
3. Print your details in separate lines using printf() function. Your details should
contain :
i. Your name
i. Your department name
ii. Your student id
iv. your level
Keywords
(CSE)_
CSE_
20CSE_
CSE_KUET_
CSE KUET_
123_
CSE20_
Example of Identifiers
(CSE)_ invalid
CSE_ valid
20CSE_ invalid
CSE_MIST_ valid
CSE MIST_ invalid
123_ invalid
CSE20_ valid
Data Types
In programming everything we deal with is “Data”
There are various types of data. Like character, decimal
numbers, natural numbers etc.
There are different data types for these.
Data Types
To visualize the need for different data types we can see an
example:
Suppose you have 3 material : sand, water, oxygen gas and 3
containers: 1 paper box, 1 bowl and 1 cylinder. You can put one
material in one container. What will you put in which container?
Data Types
Data Types
Data type Characteristics Size (bit) Range of values Example
char Letter, 8 -128 to 127 A, $,%
symbols, digit
int Positive and 32 -2147483648 to 0, 5,-5
negative 2147483647
whole
numbers
float Dec 32 3.4e-38 to 3.4e+38 1.3
{
15
int var;
Result is 15.
var = 7 + 8;
printf(“%d”, var);
printf(“\nResult is %d.”, var);
}
Example: Printing float Value
#include<stdio.h>
int main() Output:
{
float operand1, operand2, result;
Result of the addition operation is 5.2
operand1 = 2.1;
operand2 = 3.1;
result = operand1 + operand2;
printf(“\nResult of the addition
operation is %f”, result );
}
Example: Printing char Value
#include<stdio.h>
int main() Output:
{
char var;
A
var= ‘A’;
printf(“\n%c”, var);
}
ASCII Table
Example: Printing char Value
#include<stdio.h>
int main() Output:
{
char var;
A
var= ‘A’;
printf(“\n%c”, var); 65
printf(“\n%d”, var);
}
Example: Taking Input and Printing float Value
#include<stdio.h>
int main() { Ampersand sign = &
float operand1, operand2, result;
scanf(“%f”,&operand1 ); An alternative can be:
scanf(“%f%f”,&operand1 , &operand2 );
scanf(“%f”,&operand2 );
result = operand1 + operand2;
printf(“\nResult of the addition of %f and %f is %f”, operand1, operand2,result );
}
Why ampersand is used in scanf() function but not
in printf() function?
#include<st
dio.h> int
main() { Ampersand sign = &
float operand1, operand2, result;
scanf(“%f”,&operand1 ); The scanf function expects to receive in the
scanf(“%f”,&operand2 ); address of our variable. It can
result = operand1 + then store the value into our variable. & sign
operand2; indicates the address of the variable
printf(“\nResult of the addition
The printf function directly prints the value stored in
of
the variable. It does not need to print value from
%f and %f is %f”, result );
the variables address
}
More with Format Specifier
#include<stdio.h>
int main() { Output:
float var=1.3143547; 1.3143547
printf(“\n%f”, var);
1.31
printf(“\n%.2f”, var);
}
Prints upto 2 decimal point
More with Format Specifier
#include<stdio.h>
int main() { Output:
float var=1.3153547; 1.3153547
printf(“\n%f”, var);
1.32
printf(“\n%.2f”, var);
}
Prints upto 2 decimal point
More with Format Specifier
#include<stdio.h>
int main() { Output?
float var=1.3153547; 1.3153547
printf(“\n%f”, var);
1.315
printf(“\n%.3f”, var);
}
Rounds the value and Prints upto 3
decimal point
Task
Take input of an integer number and print it
Take input of 3 integer numbers and print the sum of them
Take input of 2 integer numbers and calculate the sum,
subtraction, multiplication of them. Print each of them in new
lines.
Take input of 1 integer and 1 float number. Print the sum of them
Take input of 5 integer numbers , calculate their average and
print the average result
Take a character input and print its corresponding ASCII value.
71
Example 1- A Simple Conversation
Let’s write a program (a chatbot) to have a simple conversation
with the user. In time, we’ll make more of a ‘intelligent’ chatbot!
Enter The year you were born: 1999 You’ll be 56 years old in 2055.
Enter the year to calculate your age in: 2055
Step 1 Step 2
Ask and store the year he/she wants to printf() + scanf() + use an int variable
calculate the age in (in calc_year)
Step 1 Step 2
Ask and store the principal amount printf() + scanf() + use an int variable
Ask and store the interest rate printf() + scanf() + use another int variable
Ask and store the time printf() + scanf() + use an int variable
Do (principal * rate * time) / 100.0 to find Arithmetic operation + store in an int variable
simple interest
Step 1 Step 2
Ask and store the basic daily salary printf() + scanf() + use a float variable
Ask and store how many days the worker printf() + scanf() + use an int variable
worded last month
Ask and store overtime hours worked by the printf() + scanf() + use an float variable
worker last month
Ask and store overtime rate per hour printf() + scanf() + store in a float variable
Step 1 Step 2
Ask and store the num1(dividend) printf() + scanf() + use a int variable
Ask and store the num 2 (divisor) printf() + scanf() + use an int variable
Take input of the daily gold rate (per bhori) and printf() + scanf() + use float variables
today’s discount rate
Take input from shopkeeper how much gold bought printf() + scanf() + use a float variable
(in bhori)
Calculate and show the price for the customer Explicit typecasting + Arithmetic operation + printf()
(discard all paisas, floor to nearest 100!)
Escape Sequences for printf()
Escape Sequence What it does
\n Prints a new line. More specifically, moves the cursor to a new line.
\r Moves the cursor to the beginning of the current line. Then whatever you write after \r
will overwrite the previous characters. This is called carriage return.
\b moves the cursor back one space. Then whatever you write overwrites what you had in
that space. However, it can’t go back to a previous line.
\a Doesn’t move the cursor. Just makes a “beep” or windows “alert” sound for that line.
\ Put \ before any of the above escape sequences from above if you want to print the
escape sequence itself rather than to move the cursor.
%% However, if you want to print % itself just put another % before that. Putting \ before %
won’t work.
Note: Escape characters take effect within that printf() (that line) only.
A Simple Task For Practice
Calculate the area of geometric shapes (circle, rectangle, square, triangle). Write separate
programs for each shape’s area calculation.