Subject Name: Programming for Problem Solving
Subject Code: 3ES101CS
UNIT 1: Introduction to Computers of a computer system (disks, memory,
processor, where
a program is stored and executed, operating system, compilers etc.).
Algorithm, Flowchart / pseudo code with Examples
Introduction to C Language: History of C, Features, Structure of C program,
Character set, Tokens, Variables,
Data types, I/O statements, Type conversion Syntax and Logical Errors in
compilation, object and executable code.
Programming for problem solving
Main goal of programming is to solve a problem.
A problem can be either simple or complex
Simple problem can be solved in single step example: c= a+b
Complex problem can be solve in multiple steps
Example z = (a+b)*(c+d)/e
We can solve in multiple steps like
o T1= a+b
o T2= c+d
o T3=t1* t2
o T4= t3/e
o Z=t4
Program: A collection of statements or instructions which are used to solve a problem.
These problem either a simple or complex.
Instruction: A statement which comprises of operators and operands or variables to
perform a sub task or specific task
Example: C=a+b; a,b,c are variables or operands + = ; operators
Problem development steps
1. Define the problem ‘P’
2. Understand the problem clearly
3. Divide the problem P into sub problems p1,p2,p3 – pn
4. Do the analysis or thinking the problem how to solve it
5. Find the sub solutions to the various sub problems t1, t2, t3, -- tn
6. Combine all the sub solutions
7. Compare the solution by using any tool
Computer Languages
Like human beings understand various languages, in order to program we need three
types of programming languages
1. High level programming languages
This can be written in English language using simple steps
Easy to understand
Will be used by the users
Examples: Pascal, C, C++, Java and so on
2. Low level languages:
This can be written in binary or low level or machine language
Complex to understand by the users
Will be used by the computers
Examples: Binary languages
3. Assembly Level Languages:
This can be written in mnemonics which is combination of both high and low level
languages
Moderate to understand needed because program first converted to assembly than
to low level languages
These languages can be used to interact the hardware devices
Example: Assembly level language, Micro processors, micro controllers and so on
Introduction to computers
A Computer is an electronic device which performs operations such as accepts data
as an input, store the data, manipulate or process the data and produce the results
an output.
Used for problem solving weather general problem or specific problem
Architecture of Computer Systems
Computer Software
A set of programs which are used to operate or manage the hardware system to work.
System Software
System software is responsible for managing a variety of independent hardware
components, so that they can work together.
Device drivers : A special kind of software which controlled a particular hardware connected
to system example: Device drivers, printer drivers, each hardware unit controlled by divers
Operating systems
Servers
Utilities
Window systems
Divide into Operating Systems , System support and System development
Operating Systems
An operating system (OS) is the program that, after being initially loaded into the computer
by a boot program, manages all of the other application programs in a computer.
Interface between the user and the systems, access the file or DB, Interface to
communication system internet protocols
Goal is to provide efficient access to the user to operate the system
System Support
System support software is software that supports, or facilitates the smooth and efficient
execution of various programs and operations of a computer.
There are four major categories of systems support software: utility programs, language
translators, database management systems, and performance statistics software.
Utility Software performs certain tasks like virus detection, installation, and un installation,
data backup, deletion of unwanted files, etc. Some examples are antivirus software, file
management tools, compression tools, disk management tools,
Language translators which translates the high level language program written in simple
English language statements to machine language statements examples Compilers ,
Assemblers and Interpreters
Compilers which translates the high level language program (C, C++ .. ) to low level language
and vice versa. The complete program will be translated to machine code at once where as
interpreter translates line by line (Java, python .. )
Databases Management Systems: A database management system (DBMS) is system
software for creating and managing databases. A DBMS makes it possible for end users to
create, protect, read, update and delete data in a database.
performance statistics software allow users to perform complex statistical analysis (SPSS)
System Development
Systems development is the process of defining, designing, testing, and implementing a
new software application or program.
Language translators: Compilers, Interpreters and Assemblers
General-purpose software
It refers to the software that is mass-produced for a broad range of common business
applications such as word-processing, graphics, pay roll, and accounting.
Application Software: An application is computer software developed specifically to aid a
user to perform any specific tasks.
: • Business software • Computer aided design • Databases • Decision making software •
Educational software • Image edition
Programming Software
Programming Software usually provides tools to assist a programmer in writing computer
programs, and software using different programming languages in a more convenient way
Compilers • Debugge
rs • Interpreters • Linkers • Text editors
Translators:
Used to translate the high level to low level languages
Can be listed as compilers, interpreters and assemblers
Compilers: Used to translate the high level language (Source language) to machine code (target
languages) and vice versa. The compilers translates the complete program to machine code one at a
time
Example: C, C++ ..
Interpreters: Interpreters are used to translate the high level language to low level language and
vice versa. Using interpreters used to translate the program to machine code line by line
Example: Java, python –
Assemblers: Assemblers are used to translate the high level language to Assembly level language .
Computing Environment:
Computing Environment is a collection of computers / machines, software, and networks that
support the processing and exchange of electronic information meant to support various types of
computing solutions.
Personal Computing Environment
Client Server Environment
Time sharing Environment
Distributed Environment
Personal Computing Environment: Personal PC
All of the computer hardware components are tied together in our personal computer
Time-Sharing Environment:
In the time-sharing environment, all computing must be done by the central computer. The central
computer the shared resources, it manage the shared data and printing
Client/Server Environment Client/Server computing environment splits the computing
function between a central computer and user‟s computers.
Distributed Computing Environment
C programming language execution
1. Open the Gedit in the linuex environment
2. Save the file with filename.c (extension)
3. Execute the program
4. Compile the program ( cc filename.c)
5. If no errors than Object file name created with filename.o
6. Object file is the file which will be created like assembly language before the
machine code
7. Linker is used to combine all the libraries and object files to get machine code
8. Machine code is the executable file to see the output ./a.out
C Language Errors
There are 3 types of errors occurred during the execution
Syntax errors every line of C program need to be framed in a built in syntax form other
than this will be treated as syntax errors. These errors are those which contains syntax
errors like semicolons missing, spelling mistakes, undeclared variables, misuse of
operators and any syntactical errors.
Semantic errors: Semantics errors are those which deals with meaning of each
instructions or statement in program. Each line need to have some specific task in
problem solving for which the program is designed. These errors are syntactically perfect
but meaning or semantics have errors
Run time errors: These errors are those which makes the problem when it nuns on the
operating systems. These errors are those which occur during the execution of program.
These errors are occurred when the program successfully compiled but not performs the
action which is intended.
Algorithms
- Step by step procedure written in simple English language to solve a problem
- It is blue print of a program which does not follow any syntax rules
- It is program development step which are abstract and high level uses some logic
and English explanations.
- Algorithms does not have any implementations
Pseudocode:
- Step by step procedure written in stylish English which also follows some rules to
solve problem
- It is also a blue print of the program which follows certain rules
- It is program development step which are abstract and high level uses some logic
and stylish English explanations with some rules.
- Does not have any practical implementation but helps in practical language
constructs
Flowcharts
- It is a step by step procedure to solve a problem uses some pictorial symbols
- It also a blue print of the program where logic is written in symbols
- It is also a program development step which is abstract and high level
- Help full to implement in Unified programming language which converted to
`programs or code
Symbols used in flow chart
Differences between Algorithm, flowchart and pseudo code
S. No Algorithm Flow Chart Pseudo Code
Sequence of instruction to Pictorial Representation of
1 Sequence of instruction
solve the Particular Problem algorithm
to solve the Particular
Problem
It’s a English language
It’s pure English Language, it It has symbol to Represent the
2 But it has, some set of
has no rules to write instruction
rules
Pseudo code are tools
It’s not a tool for document Flow chart are tools document
3 document represent and
purpose and represent algorithm
Algorithm
Pictorial representation of
Sequence of instruction to Rules of structured
4 algorithm using standard
solve the Particular Problem design & programming.
symbols.
1. Algorithm for displaying message
2. Start
3. Display the message with string as Hello World.
4. End
Pseudocode for displaying message
1. Start
2. Print “ Hello World”
3. End
Program to print hello message
// Online C compiler to run C program online
#include <stdio.h>
int main() {
// Write C code here
printf("Hello world");
return 0;
}
Output:
Hello World
Flow chart (Exercise)
2.Algorithm for Adding Two Numbers:
1. Start
2. Input the first number (let's call it num1).
3. Input the second number (let's call it num2).
4. Calculate the sum by adding num1 and num2 and store it in a variable sum.
5. Display the value of sum as the result of the addition.
6. End
Pseudocode for adding two numbers
1. Start
2. Input num1
3. Input num2
4. Sum – num1 + num2
5. Print sum
6. End
Program to add two numbers
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter first numbers: ");
scanf("%d", &num1);
printf("Enter first numbers: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum: %d\n", sum);
return 0;
Output: Enter First number 10
Enter the second number 20
Sum: 30
Flow Chart (exercise)
3.Algorithm for Accepting and Printing Different Data Types:
1. Start
2. Declare variables for different data types: integer (intVar), float (floatVar),
character (charVar), and string (stringVar).
3. Input an integer value and store it in intVar.
4. Input a floating-point value and store it in floatVar.
5. Input a single character and store it in charVar.
6. Input a string and store it in stringVar.
7. Display the values of intVar, floatVar, charVar, and stringVar.
8. End
Pseudocode
1. Start
2. Declare a as integer
3. Declare b as float
4. Declare c as char
5. Declare d as string
6. Input “ Enter a value as integer” into a
7. Input “Enter a value as float “ into b
8. Input “ Enter a value as char “ into c
9. Input “Enter a value as string “ into d
10. Print “ the integer value”+ a
11. Print “ The float value “ + b
12. Print “ The char value is “ +c
13. Print “ the string value is “+d
14. End
Program to accept inputs (integer, float, char and string ) and print the same as output
#include <stdio.h>
int main() {
int a;
float b;
char c;
char d[100]; // Assuming a maximum string length of 100 characters
// Input an integer
printf("Enter an integer: ");
scanf("%d", &a);
// Input a float
printf("Enter a float: ");
scanf("%f", &b);
// Input a character
printf("Enter a character: ");
scanf(" %c", &c); // Notice the space before %c to consume any leading
whitespace
// Input a string
printf("Enter a string: ");
scanf(" %s", d); // Read up to 99 characters until a newline
// Output the inputs
printf("Integer: %d\n", a);
printf("Float: %f\n", b);
printf("Character: %c\n",c);
printf("String: %s\n", d);
return 0;
}
Output:
Enter an integer: 10
Enter a float: 20.5
Enter a character: A
Enter a string: Methodist
Integer: 10
Float: 20.500000
Character: A
String: Methodist
Flow chart (exercise)
7. Algorithm to swap any two numbers
Algorithm SwapTwoNumbers
1. Start
2. Declare a,b,c three variables as integer
3. Input a,b values through the key board
4. Assign the a value in temp variable
5. Assign the b value in a variable
6. Assign the temp variable with b
7. Print the value of a
8. Print the value of b
9. End
Pseudo code
1. Start
2. Declare a, b, temp as integer
3. Input a
4. Input b
5. temp=a;
6. a=b;
7. b=temp
8. print a;
9. print b;
10. end
// Program to swap numbers
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Enter two numbers: 10
20
After swapping: a = 20, b = 10
Flow chart
8. Algorithm for Area of rectangle (exercise)
Pseudo code:
Program to find the area of rectangle
#include <stdio.h>
int main() {
double length, width, area;
// Input length and width
printf("Enter the length of the rectangle: ");
scanf("%lf", &length);
printf("Enter the width of the rectangle: ");
scanf("%lf", &width);
// Calculate the area
area = length * width;
// Display the result
printf("The area of the rectangle is: %.2lf square units\n", area);
return 0; }
Output:
9. C program to evaluate the arithmetic expression
#include <stdio.h>
int main() {
double a, b, c, d, e, z;
// Input values for a, b, c, d, and e
printf("Enter the value of 'a': ");
scanf("%lf", &a);
printf("Enter the value of 'b': ");
scanf("%lf", &b);
printf("Enter the value of 'c': ");
scanf("%lf", &c);
printf("Enter the value of 'd': ");
scanf("%lf", &d);
printf("Enter the value of 'e': ");
scanf("%lf", &e);
// Evaluate the expression
z = (a + b) * (c + d) / e;
// Display the result
printf("The value of 'z' is: %.2lf\n", z);
return 0;
}
Output
Enter the value of 'a': 1
Enter the value of 'b': 1
Enter the value of 'c': 2
Enter the value of’d’: 2
Enter the value of 'e': 3
The value of 'z' is: 2.67
Write the algorithm and pseudo code to evaluate the arithmetic expression (Exercise)
10. Program to find the area of a circle
#include <stdio.h>
#include <math.h> // To use the math functions
int main() {
double radius, area;
// Input the radius of the circle
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
// Calculate the area
area = M_PI * pow(radius, 2);
// Display the result
printf("The area of the circle is: %.2lf square units\n", area);
return 0;
}
Enter the radius of the circle: 20
The area of the circle is: 1256.64 square units
Write the pseudocode for to find the area of circle (Exercise)
Introduction to C Language: History of C, Features, Structure of C program, Character set,
Tokens, Variables,
Structure of C Language program
Comment line
Pre-processor directive
Global variable declaration
main function( )
Local variables;
Statements;
User defined function
Comment line: The lines which are written by // or /* ---- */ will not be compiled its for
user purpose
Pre-processor directive: #include<stdio.h> includes the std input and output files like printf
& scanf to the program . #define PI 3.14 also used declare the variables
Global variable declaration any variables declared before main will be considered as global
variables. These variables can be accessed through out main and other functions
main function( ) : Followed by () . Every program have one main function which followed by
{ }. Main function can define any user defined functions within it.
Local variables The variables which are defined under or main or other functions called as
local variables. These variables can be accessed for that sub function only
Statement: Which contains some operators and operands
-
History of C Language
History of C language is interesting to know. Here we are going to discuss a brief
history of the c language.
C programming language was developed in 1972 by Dennis Ritchie at bell
laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A.
Dennis Ritchie is known as the founder of the c language.
It was developed to overcome the problems of previous languages such as B, BCPL,
etc.
Language Year Developed By
Algol 1960 International Group
BCPL 1967 Martin Richard
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K&RC 1978 Kernighan & Dennis Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee
C99 1999 Standardization Committee
Features of C language
C is the widely used language. It provides many features that are given below.
1. Simple
2. Machine Independent or Portable
3. Mid-level programming language
4. structured programming language
5. Rich Library
6. Memory Management
7. Fast Speed
8. Pointers
9. Recursion
10. Extensible
C Tokens:
1. Identifiers
2. Keywords
3. Constants
4. Strings
5. Operators
6. Special Symbols
Identifiers are user defined word used to name of entities like variables, arrays, functions,
structures etc. Rules for naming identifiers are:
1) Name should only consists of alphabets (both upper and lower case), digits and
underscore (_) sign.
2) First characters should be alphabet or underscore
3) Name should not be a keyword
4) Since C is a case sensitive, the upper case and lower case considered differently, for
example code, Code, CODE etc. are different identifiers.
5) Identifiers are generally given in some meaningful name such as value, net_salary, age,
data etc
Keywords: These are the words which have some special meaning inside the
compiler. The keywords cannot be used as variables names.
Example: main, int, float, char, double, return, extern, auto, break, struct, if , else, for,
while, union, short, unsigned, register, short, signed, void, continue, goto, static,
volatile and so on.
Constants:
C Constants are like a variable, except their value never changes during execution
once defined. This tutorial describes C Constants
Example or syntax
#include<stdio.h>
void main()
{
const int SIDE = 10;
int area;
area = SIDE*SIDE;
printf("The area of the square with side: %d is: %d sq. units", SIDE, area);
}
Character set
The alphabets, numbers and special symbols when properly combined form constants,
variables and keywords
Strings: Combinations of characters is called strings. Collection of array of char
Syntax char[10]=”Hello”;
Operators:
Arithmetic Operators : + - * / %
Guess the output of the following program
#include <stdio.h>
int main() {
int num1 = 10, num2 = 4;
// Addition
int sum = num1 + num2;
printf("Addition: %d + %d = %d\n", num1, num2, sum);
// Subtraction
int difference = num1 - num2;
printf("Subtraction: %d - %d = %d\n", num1, num2, difference);
// Multiplication
int product = num1 * num2;
printf("Multiplication: %d * %d = %d\n", num1, num2, product);
// Division
float quotient = (float)num1 / num2; // Ensure floating-point division
printf("Division: %d / %d = %.2f\n", num1, num2, quotient);
// Modulus (remainder)
int remainder = num1 % num2;
printf("Modulus: %d %% %d = %d\n", num1, num2, remainder);
return 0;
}
Output:
Increment and Decrement Operators ++ --
I=10
i++ means first I initializes and then I increments i=11
i=10
i—means i=9
Write a C program which explain the concept of Increment and Decrement operator
Program
#include<stdio.h>
void main()
{
int a,p,q,r,s;
a=10;
printf("\n The a value before inc and dec operations a=%d",a);
p=a++;
printf("\n p=%d",p);
printf("\nafter increment a=%d",a);
q=++a;
printf("\n q=%d",q);
printf("\n after increment a=%d",a);
r=a--;
printf("\n r=%d",r);
printf("\n after Dec. a=%d",a);
s=--a;
printf("\n s=%d",s);
printf("\n after Dec. a=%d",a);
}
The a value before inc and dec operations a=10
p=10after increment a=11
q=12
after increment a=12
r=12
after Dec. a=11
s=10
after Dec. a=10
Relational Operators < <= > >= != ==
Logical Operators && Logical and !! Logical OR ! Logical not
Bitwise Operators
<< Binary Left Shift Operator
>> Binary Right Shift Operator
~ Binary Ones Complement Operator
& Binary AND Operator
^ Binary XOR Operator
| Binary OR Operator
Assignment Operators a= b the value of the b is assigned to a. The right side value will be
assigned to left side
Conditional Operator The conditional operator is a concise way to perform conditional
assignments and expressions, and it's often used in situations where you want to assign a value
based on a simple condition.
Condition ? expression 1: expression 2?
Max= (num1>num2)?num 1:num2;
Special Operators
Size of operator (sizeof), conditional operator, comma (,), pointer ( *),arrow (->) operator,
increment and decrement operator.
Variables Variables are containers for storing data values, like numbers and characters.
In C, there are different types of variables (defined with different keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single
quotes
type variableName = value;
Type Conversions
C language provides great provision of type’s conversion to convert one data type to
another there are two types of conversions
1. Implicit conversion : This conversion done by compiler itself automatically for
example 1
float f;
f=9;
Printf(“ The float value is %f”,f);
Output:
The float value Is 9
Example 2
// Automatic conversion: float to int
int myInt = 9.99;
printf("%d", myInt); // 9
2. Explicit conversions: Explicit conversion is done manually by placing the type in
parentheses () in front of the value. Considering our problem from the example
above, we can now get the right result:
// Manual conversion: int to float
float sum = (float) 5;
printf("%f", sum); // 2.500000
Example 2:
Int a = (int)2.5;
Printf(“%d”,a);
Questions
1. Explain the need of programming languages and list types of it?
2. Differentiate between high level and low level languages along with examples?
3. Draw the neat architecture for computer and explain its parts?
4. Discriminate between compilers and interpreters?
5. Write the differences between primary and secondary memory?
6. List few operators available in C language?
7. Outline the differences between Algorithm, pseudo code, flowchart and program?
8. Write advantages of algorithms, flowcharts?
9. Name few features of C Languages?
10. Write the structure or syntax for c program?
11. Write the steps for execution of C program?
12. Name few differences between object and executable code during compilation of C
program?
13. Write the ranges of data types?
14. Explain the concept of type conversions along with examples?
15. What are the rules to be followed while naming the variables?
16. Define the following terms
a. Keywords.
b. Identifiers
c. Constants
d. Variables
e. Tokens
17. Write the C program / Algorithm / Pseudo code / flowchart for the following
a. Print the display message “Hello world”
b. Accept integer, float, char and string as input from the input and display
same as output
c. Accept integer values as the input and do c= a + b
d. Evaluate the arithmetic expression c= (a+b) / (2+p)
e. Find the area of rectangle
f. Find the area of square
g. Find the area of circle
h. Accept the maths, computers, physics and chemistry as input values
calculate the sum and average and display the result
i. If i=1 find the value for p=i++, q=++I, r=i--, s=--i
------------------------------------------- * -----------------------------------------