"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
PPS Unit-1.pdf
1. 1
Programming For Problem Solving
I B.Tech I Semester
BY
Mrs. Pavani B
Assistant Professor
Computer Science and Engineering
MRCET
2. 2
“Unit-I”
Introduction to Computing – Computer Systems, Computing
Environments, Computer Languages, Algorithms and
Flowcharts, Steps for Creating and Running programs.
Introduction to C – History of C, Features of C, Structure of C
Program, Character Set,C Tokens - keywords, Identifiers,
Constants, Data types, Variables. Operators, Expressions,
Precedence and Associativity, Expression Evaluation, Type
conversion, typedef, enum
Control Structures: Selection Statements(Decision Making) – if
and switch statements, Repetition Statements (Loops) - while,
for, do-while statements, Unconditional Statements – break,
continue, goto, Command line arguments.
3. Introduction to Computer System
A Computer is an electronic device that receives input, stores or
processes the input as per user instructions and provides output in
desired format.
Input-Process-Output Model
Computer input is called data and the output obtained after processing
it, based on user’s instructions is called information.
4. Introduction to Computer System
A computer is a system made of two major components.
1. Software
2. Hardware
Hardware: Physical parts of computer that we can touch
called as hardware.
i.e. Monitor ,CPU ,Keyboard ,Mouse ,etc.
Software: It is a set of instruction/program that tells the
computer what to do.
i.e. Operating System, MS Office, etc.
5. Introduction to Computer System
Computer Hardware : The hardware components of the
computer system is divided into 4 major components
1. Input Devices
2. Output Devices
3. CPU ( Central Processing Unit )
4. Storage Devices
6. 6
Components of a Computer System
Input and Output Devices of a Computer System
7. Components of a Computer System
The Central Processing Unit (CPU) is responsible for executing
instructions such as arithmetic calculations, comparisons among data,
and movement of data inside the system.
11. Compiler
A compiler is a computer program that translates computer code
written in one programming language (the source language) into
another language (the target language).
12. Computing Environments
In the early days of computer, there is only one environment: the
mainframe computer hidden in a central computing department.
With the advent of minicomputers and personal computer, the
environment changed, resulting in computers on virtually every
desktop
1. Personal Computing Environment
2. Time-Sharing Environment
3. Client/Server Environment
4. Distributed Computing
13. Computing Environments
Personal Computing Environment
In the personal computing environment, there is a single
computer system. All the system processes are available on the
computer and executed there. The different devices that constitute
a personal computing environment are laptops, mobiles, printers,
computer systems, scanners etc.
Time Sharing Computing Environment
The time sharing computing environment allows multiple users to
share the system simultaneously. Each user is provided a time
slice and the processor switches rapidly among the users
according to it. Because of this, each user believes that they are
the only ones using the system.
15. Client Server Computing Environment
In client server computing, the client requests a resource and the
server provides that resource. A server may serve multiple clients
at the same time while a client is in contact with only one server.
Both the client and server usually communicate via a computer
network but sometimes they may reside in the same system.
16. Distributed Computing Environment
A distributed computing environment contains multiple nodes that
are physically separate but linked together using the network. All
the nodes in this system communicate with each other and handle
processes in tandem. Each of these nodes contains a small part of
the distributed operating system software.
18. Machine Languages/Low Level Languages
In the earliest days of computers, the only programming
language available were machine languages. Each computer
had its own machine language, which was made of streams of
0s and 1s.
The only language
understood by a
computer is
machine language.
19. Symbolic Languages
• The Symbolic languages uses symbols or mnemonics to
represent the various machine language instructions but
symbolic language was not understood by the computer so it
must be translated to the machine language..
• A special program called assembler used to convert into
machine language. Symbolic languages are known as
Assembly languages..
Example
1. mov eax, 10
2. mov ecx, 20
3. add eax, ecx
20. High-level Languages
• Although assembly languages greatly improved programming
efficiency, they still required programmers to concentrate on
the hardware they were using.
• So, High level languages are designed to relive the
programmer from the details of the assembly language. These
are portable to many different computers, allowing the
programmer to concentrate on the application program.
• Over the years, various languages, most notably
BASIC,COBAL,pascal,Ada,C,C++ and java were developed
• High-level languages should be converted into machine
language. This process is called compilation.
21. Algorithm
• An algorithm is a step by step process to solve a problem using
a finite number of steps. Algorithms are used in mathematical
computing etc to carryout calculations or data processing.
• The algorithm is developed using pseudocode which can then be
converted to a c program
• Pseudocode : It is an artificial and informal language that helps
programmers to develop algorithm. Pseudocode is similar to
everyday english, it is convenient and user-friendly.
• Pseudocode programs are not executed on computers but they
are used in developing algorithms
22. All the algorithms must satisfy following properties
Properties of Algorithms
• Input – the algorithm must accept zero or more inputs
• Output - must produce at least one output
• Definiteness -Each instruction is clear and un ambiguous.
• Finiteness -An algorithm should terminate after a finite
number of steps. It should not enter into an infinite loop
• Effectiveness – Each operation must be simple and should
complete in a finite time
23. Algorithm : Addition of two numbers
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values into num1 and num2.
Step 4: sum = num1+num2
Step 5: print sum
Step 6: Stop
24. Algorithm : Area of circle
Step 1: Start
Step 2: Declare variables radius and Area
Step 3: Read value into radius
Step 4: Area = 3.14*radius*radius
Step 5: print Area
Step 6: Stop
25. Algorithm : Largest among 3 numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
26. Algorithm to print "n" natural numbers
Step 1: Start
Step 2: Declare n, i
Step 3: Assign i=1
Step 4: Read value into n
Step 5: Repeat step 5.1 and 5.2 until i<=n
5.1 Print i
5.2 Compute i=i+1
Step 7: Stop
27. Algorithm to sum "n" natural numbers
Step 1: Start
Step 2: Declare variables n, sum and i.
Step 3: Initialize variables or Assign
sum = 0
i = 1
Step 4: Read value into n
Step 5: Repeat 5.1 and 5.2 until i <= n
5.1 : sum = sum+i
5.2 : i = i+1
Step 6: Display sum
Step 7: Stop
28. Flowchart
• A flow chart is a graphical representation of an algorithm.
Flowcharts are drawn using certain special-purpose symbols
such as rectangles, diamonds, ovals, and small circles as shown
below.
• These symbols are connected by arrows called flow lines. Like
pseudo code, flowchart is useful for developing and
representing algorithms.
29.
30. Flowchart : Addition of two numbers
Step 1: Start
Step 2: Input/Read Number1 and Number2.
Step 3: Sum = Number1+Number2
Step 4: Print Sum
Step 5: Stop
31. Flowchart : Area of circle
Step 1: Start
Step 2: Read value into Radius
Step 3: Area = 3.14*Radius*Radius
Step 4: Display Area
Step 5: Stop
33. Flowchart : Largest among three numbers
Read A,B,C
Print B is
largest
Print C is
largest
Print A is
largest
Start
Is
A>B
Is
A>C
Is
B>C
Yes
No
No No Yes
Yes
Stop
37. Creating, Compiling and Executing a Program
Syntax to create a file in linux operating system
$ gedit filename.c
Example :
$ gedit add.c
How to Compile a Program?
$ gcc add.c
How to run the program?
$ ./a.out
38.
39.
40. Features
• It was mainly developed as a system programming language to
write an operating system.
• The main features of C language include
• Low-level access to memory
• A simple set of keywords
• Clean style
• Easy to learn
• Structured language
• It produces efficient programs.
• These features make C language suitable for system programming
like an operating system or compiler development.
42. Documentation section: To enhance the readability of the
program, programmers can provide comments about the
program in this section. Comments can be used any where in
the program but too many comments are avoided.
Example
/* This is a example to check largest number*/
Header file/link section: C program depends upon some
header files for function definition that are used in program.
Each header file by default is extended with .h. The file should
be included using #include directive.
#include <stdio.h>
or
#include “stdio.h”
43. Global declaration section: This section declares some
variables that are used in more than one function. These
variables are known as global variables. These must be declared
outside of all the functions.
Main program section: Every program written in C language
must contain main() function. The execution of the program
always begins with the function main().
The program execution starts with the opening brace ( { ) and
ends with the closing brace ( } ). Between these two braces the
programmer should declare the declaration part and the
executable part.
44. Declaration Part: The declaration part declares the entire
variables that are used in executable part. The initialization of
variables also done here. The initialization means providing
the initial value to the variables
Executable Part: This part contains the statements following
the declaration of the variables. This part contains a set of
statements. These statements are enclosed between the braces.
User-defined function: The function defined by the user is
called user defined function. These functions are generally
defined after the main() function. They can also be defined
before main() function. This portion in not compulsory now.
45.
46. /* Program to add two numbers */
#include<stdio.h> /* header file section */
int main()
{
int a=10,b=20,c; //declaration part
c=a+b; //Executable part
printf("%d",c);
}
OUTPUT : 30
Note : initializing values in program itself
47. /* Program to add two numbers */
#include<stdio.h>
int main()
{
int a,b,sum;
printf(“Enter any two values:”);
scanf(“%d %d”,&a,&b);
sum=a+b;
printf("%d",sum);
return 0;
}
OUTPUT : Enter any two values:15 30
45
Note : Taking input through keyboard
50. Identifiers
Identifiers are the names given to variables, functions and arrays
Each identified variable stored in a unique address.These identifier are
defined against a set of rules.
Rules for an Identifier
• The first character of an identifier can only contain alphabet(a-z ,
A-Z) or underscore (_).
• Identifiers are also case sensitive in C. For example name and
Name are two different identifiers in C.
• Keywords are not allowed to be used as Identifiers.
• No special characters, such as semicolon, period, whitespaces, slash
or comma are permitted to be used in or as Identifier.
• An identifier should not contain any white spaces.
51. Variables
A variable is a name of the memory location. It is used to store data.
Its value can be changed, and it can be reused many times.
Syntax:
Data_type variable_list;
Example:
int a;
float b;
char c;
Here, a, b, c are variables.
The int, float, char are the data types.
52. Datatypes
A data type specifies the type of data that a variable can store such as
integer, floating, character, etc.
53. Character Data Type :
•
•
•
•
Character data type allows a variable to store only one character.
Size of character data type is 1 byte(8 bits).
“char” keyword is used to refer character data type.
For example, ‘A
’can be stored using char datatype. Y
ou can’t store
more than one character using char data type.
Format specifier or control string is %c
Range for signed char -128 to 127 and unsigned char is 0 to 255.
•
•
• Example : char gender = ‘F’;
Datatypes
54. Integer Data Type :
• Integer data type allows a variable to store numeric values.
• “int” keyword is used to refer integer data type.
•
•
The storage size of int data type is 2
It varies depend upon the processor
are using 16 bit processor, 2 byte
allocated for int data type.
or 4 or 8 byte.
in the CPU that we use. If we
(16 bit) of memory will be
• Like wise, 4 byte (32 bit) of memory for 32 bit processor and
8 byte (64 bit) of memory for 64 bit processor is allocated for int
datatype.
Datatypes
55. • int (2 byte) can store values
int. 0 to 65535 for unsigned
from -32,768 to +32,767 for signed
int.
• int (4 byte) can store values from -2,147,483,648 to
+2,147,483,647 for signed int. 0 to 4,29,49,67,295 for unsigned
int.
If you want to use the integer value that crosses the above limit,
you can go for “long int” and “long long int” for which the
limits are very high.
•
Note:
• We can’t store decimal values using int data type.
56. Datatypes
Float and double
float and double are used to hold real numbers(decimal values).
For Example : float a=33.6;
Type Storage size Value range Precision Control
String
float 4 bytes(32 bits) 1.2E-38 to 3.4E+38 6 decimal
places
%f
double 8 bytes(64 bits) 3E-308 to 1.7E+308 15 decimal
places
%lf
long double 10 bytes(80 bits) 3.4E-4932 to 1.1E+4932 19 decimal
places
%Lf
57. MODIFIERS IN C LANGUAGE:
• Modifiers are prefixed with basic data types to modify
(either increase or decrease) the amount of storage space
allocated to a variable.
For example, storage space for int data type is 4 byte for 32
bit processor. We can increase the range by using long int
which is 8 byte. We can decrease the range by using short
int which is 2 byte.
•
• There are 5 modifiers available in C language. They are,
•
•
•
•
•
short
long
signed
unsigned
long long
58. -2(n-1) to 2(n-1) -1
To calculate range for char or int or long
Here n represents number of bits
59. Syntax and Logical Errors
Syntax errors are also known as the compilation errors as they
occurred at the compilation time.
These errors are mainly occurred due to the mistakes while typing or
do not follow the syntax of the specified programming language.
Example:
If we want to declare the variable of type integer,
int a; // this is the correct form
Int a; // this is an incorrect form.
Commonly occurred syntax errors are:
If we miss the parenthesis (}) while writing the code.
Displaying the value of a variable without its declaration.
If we miss the semicolon (;) at the end of the statement.
60. Syntax and Logical Errors
The logical error is an error that leads to an undesired output. These
errors produce the incorrect output. The occurrence of these errors
mainly depends upon the logical thinking of the developer.
Example.
Many different types of programming mistakes can cause logic
errors. For example, assigning a value to the wrong variable may
cause a series of unexpected program errors. Multiplying two
numbers instead of adding them together may also produce
unwanted results.
61. Constants
A constant is a value that can't be changed in the program, for
example: 10, 20, 'a', 3.4, "c programming" etc.
62. BACKSLASH CHARACTER CONSTANTS IN C:
• There are some characters which have special meaning
in C language.
• They should be preceded by backslash symbol to make
use of special function of them.
• Given below is the list of special characters and their
purpose.
n New line
t Horizontal tab
v Vertical tab
63. Variables can be declared as constant using the const keyword
#include <stdio.h>
int main()
{
int a=10;
a=20;
printf("a=%d", a);
return 0;
}
#include <stdio.h>
int main()
{
const int a=10;
a=20;
printf("%d", a);
return 0;
}
The const keyword
OUTPUT
a=20
OUTPUT
Error
64. 64
Input / Output Functions
printf() function:
The printf() function is used for output. It prints the given statement
to the console.
The syntax of printf() function is given below:
The format string can be %d (integer), %c (character), %s (string), %f
(float) etc.
scanf() function:
The scanf() function is used for input. It reads the input data from the
console.
scanf("format specifier",variable_list);
printf("format specifier",variables_list);
65. 65
Input / Output Functions Example
Program to print cube of given number
#include<stdio.h>
int main()
{
int number;
printf("enter a number:");
scanf("%d", &number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
66. 66
Operators in C
•C supports rich set of operators.
• An operator is a symbol that tells the compiler to perform certain
mathematical or logical manipulations.
• Operators are used in programs to manipulate data and variables.
67. 67
Operators in C
Types of ‘C’ operators
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and Decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
68. 68
Arithmetic Operators
An arithmetic operator performs mathematical operations such as
addition, subtraction and multiplication on numerical values (constants
and variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division( modulo division)
Note : - Modular division operator % cannot used on floating
point data
69. OUTPUT
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder = 1
Example
include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d n",c);
c = a-b;
printf("a-b = %d n",c);
c = a*b;
printf("a*b = %d n",c);
c = a/b;
printf("a/b = %d n",c);
c = a%b;
printf("Remainder = %d n", c);
return 0;
}
70. include <stdio.h>
int main()
{
int a = 9,b = 4, sum,sub,mul,div,rem;
sum = a+b;
sub = a-b;
mul = a*b;
div = a/b;
rem = a%b;
printf("%d n",sum);
printf(" %d n",sub);
printf("%d n",mul);
printf(“%dn",div);
printf(“%d n", rem);
return 0;
}
OUTPUT
13
5
36
2
1
71. include <stdio.h>
int main()
{
int a = 9,b = 4;
printf(“sum = %d n",a+b);
printf(“sub = %d n",a-b);
printf(“mul = %d n",a*b);
printf(“div = %d n",a/b);
printf("Remainder = %d n", a%b);
return 0;
}
OUTPUT
sum = 13
sub = 5
mul = 36
div = 2
Remainder = 1
72. 72
Operators in C
Relational Operators
Relational operators are used to find the relation between two
variables. i.e. to compare the values of two variables in a C program.
73. If a=20 b=10
a>b it returns 1 (True)
a<b it returns 0 (False)
a<=b it returns 0 (False)
a>=b it returns 1 (True)
a==b it returns 0 (False)
a!=b it returns 1(True)
If a=10 b=10
a==b it returns 1 (True)
a!=b it returns 0 (False)
Relational Operators with Example
78. 78
Operators in C
Assignment Operators
• Assignment operators are used to assign the result of an
expression to a variable.
• C has a set of ‘shorthand’ assignment
operator:
variable name =expression;
Left side must be a variable that
can receive a value
Example
c=a+ b;
81. 81
Increment and Decrement Operators
Increment operators are used to increase the value of the variable
by one and decrement operators are used to decrease the value of
the variable by one.
Syntax:
Increment operator: ++
pre increment : ++var_name;
post increment : var_name++;
Decrement operator: - -
pre decrement: – – var_name;
post decrement var_name– – ;
Example:
Increment operator : ++ i and i ++ ;
Decrement operator : – – i and i – – ;
82. Example
NOTE :
A postfix operator first assigns the value to the variable on left
and then increments the operand .
On the another hand, A prefix operator first adds 1 to the
operand and then result is assigned to the variable on left.
If x=5
a = x++ ; a=5 x=6
a = ++x; a=6 x=6
a = x– – ; a=5 x=4
a = – – x; a=4 x=4
85. #include <stdio.h>
main()
{
int a=9, b=9;
a=b++;
b=a++;
b=++b;
printf("%d %d",a,b);
}
a) 9,9
b) 10,10
c) 9,10
d) 10,9
#include <stdio.h>
main()
{
int a,b;
b = 10;
a = ++b + ++b;
printf("%d%d",a,b);
}
a) 24,12
b) 23,12
c) 23,10
d) 24,10
86. 86
Operators in C
Conditional Operators
• The conditional expression can be used as shorthand for some if-else
statements. It isaternaryoperator.
• This operator consist of two symbols: the question mark (?) and the
colon(:).
87. 87
Example
int main()
{
int a, b, c;
a= 10;
b = (a == 1) ?20:30;
printf( “Value of b is %d n ", b ); // 30
c = (a == 10) ? 20 : 30;
printf( “Value of c is %d n", c ); // 20
return 0;
}
89. 89
Operators in C
Bitwise Operators
•In the C programming language, operations can be performed on a
bit level using bitwise operators.
•Following are the bitwise Operators
91. 91
Operators in C
Shift Operator:
Left Shift Operator (<<):The left shift operator will shift
the bits towards left for the given number of times.
int a=2<<1;
printf(“%d”,a);// will print 4
If you left shift like 2<<2, then it will give the result as 8.
Therefore left shifting 1 time, is equal to multiplying the
value by 2.
Right shift Operator ( >>)
The right shift operator will shift the bits towards right for
the given number of times
int b=4>>1
printf(“%d”,b);// will print 2
Right shifting 1 time, is equivalent to dividing the value
by 2.
92. 92
Operators in C
Special Operators
C supports some special operators such as:
• comma operator “,” is used to link related expressions together
int a=5,b=6;
• size of operator “sizeof()”
• Address operator “&”
• pointer operator “*”
• member selection operator “. and -> ”
• Size of operator returns the number of bytes occupied in
memory..the operand may be variable or constant or data type
qualifier
93. #include<stdio.h>
int main()
{
int a;
float b;
char c;
double d;
printf("Size of int: %d bytesn", sizeof(a));
printf("Size of float: %d bytesn", sizeof(b));
printf("Size of char: %d bytesn", sizeof(c));
printf("Size of double: %d byten", sizeof(d));
return 0;
} Size of int: 4 bytes
Size of float: 4 bytes
Size of char: 1 bytes
Size of double: 8 byte
94. Expressions
An expression is a collection of operators and operands that
represents a specific value.
What is an expression?
Expression Types in C
In the C programming language, expressions are divided into
THREE types. They are as follows...
1.Infix Expression –Example : a+b
2.Postfix Expression – Example : a++
3.Prefix Expression – Example : ++a
95. 95
Operator Precedence and Associativity
What is Operator Precedence?
• Operator precedence is used to determine the order of operators
evaluated in an expression. In c programming language every
operator has precedence (priority).
• When there is more than one operator in an expression ,the
operator with higher precedence is evaluated first and the
operator with the least precedence is evaluated last.
What is Operator Associativity?
• Operator associativity is used to determine the order of
operators with equal precedence evaluated in an expression.
• In the c programming language, when an expression contains
multiple operators with equal precedence, we use associativity
to determine the order of evaluation of those operators.
101. 101
Type Conversion
The type conversion process in C is basically converting one type
of data type to other to perform some operation.
The conversion is done only between those datatypes wherein the
conversion is possible
example – char to int and vice versa.
There are two types of conversions:
1. Implicit Type Conversion
2. Explicit Type Conversion
Implicit Type Conversion
This type of conversion is usually performed by the compiler
when necessary without any commands by the user. Thus it is also
called "Automatic Type Conversion".
106. 106
Type Conversion-Example
#include<stdio.h>
int main()
{
float a = 4.5;
float b = 4.6;
float c = 4.9;
int result;
result = (int)a + (int)b + (int)c;
printf("result = %d", result);
return 0;
}
Output : 12
#include<stdio.h>
int main()
{
float a = 4.5;
float b = 4.6;
float c = 4.9;
float result;
result = a + b + c;
printf("result = %d", result);
return 0;
}
Output : 14.000000
107. 107
typedef
typedef is a keyword used in C language to assign alternative names to
existing datatypes.
Its mostly used with user defined datatypes.
Following is the general syntax for using typedef,
typedef <existing_name> <alias_name>
Example:
#include <stdio.h>
int main()
{
typedef int unit;
unit i,j;
i=10,j=20;
printf("Value of i is :%d",i);
printf("nValue of j is :%d",j);
return 0;
}
108. Conditional Statements
• Conditional Statements/Decision-making statements are the
statements that are used to verify a given condition and decide
whether a block of statements gets executed or not , based on the
condition result.
• In the c programming language, there are two decision-making
statements they are as follows.
1. if statement
2. switch statement
109. if statement in C
if statement is used to make decisions based on a condition. The if
statement verifies the given condition and decides whether a block of
statements are executed or not , based on the condition result.
In c, if statement is classified into four types as follows...
• Simple if statement
• if-else statement
• Nested if statement
• else-if ladder
110. Simple if statement
Syntax:
if(condition)
{
// Statements to execute
if condition is true
}
It is used to decide whether a certain
statement or block of statements will be
executed or not
111. Simple if statement- Example
// C program to illustrate If statement
#include <stdio.h>
int main()
{
int i = 10;
if (i > 15)
{
printf("10 is less than 15");
}
printf("I am Not in if");
}
112. if-else statement
Syntax:
if (condition)
{
// statement block 1
// condition is true
}
else
{
// statement block 2
// condition is false
}
if a condition is true it will execute a
statement block 1 and if the condition is
false it will executes a statement block 2
113. // C program to illustrate If-else statement
#include <stdio.h>
int main()
{
int n;
printf(“enter any number”);
scanf(“%d”,&n);
if(n%2==0)
{
printf(“number is even”);
}
else
{
printf(“number is odd”);
}
return 0;
}
114. Nested if-else statement
Syntax:
if( condition 1 )
{
if(condition 2 )
{
statement 1;
}
else
{
statement 2;
}
}
else
{
statement 3;
}
Nested if statements means an if statement inside another if statement.
115. Nested if-else statement-Example
#include <stdio.h>
void main( )
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}
117. #include<stdio.h>
int main()
{
int s1,s2,s3,s4,s5,sum,avg;
printf("Enter marks of 5 subjects each out of 100 n ");
scanf("%d %d %d %d %d",&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
avg = sum/5;
printf("sum=%d average=%d n",sum,avg);
if(s1<35 || s2<35 || s3<35 || s4<35 || s5<35)
{
printf(" failed");
}
118. if(avg>=80)
printf("nn Your Grade : A+");
else if(avg>=75)
printf("nn Your Grade : A");
else if(avg>=60)
printf("nn Your Grade : B");
else if(avg>=45)
printf("nn Your Grade : C");
else if(avg>=35)
printf("nn Your grade : D");
else
printf("nn You Are Fail");
return 0;
}
OUTPUT
Enter marks of 5 subjects each out of 100
89 78 65 57 46
sum=335 average=67
Your Grade : B
119. Switch Statement
• Switch statement is a control statement that allows us to choose
only one choice among the many given choices.
• The expression in switch evaluates is compared to the values
present in different cases.
• It executes that block of code which matches the case value.
• If there is no match, then default block is executed(if present).
121. Switch Statement Rules
1) The switch expression must be of an integer or character type.
2) The case value must be an integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If
there is no break statement found in the case, all the cases will be
executed present after the matched case.
122. Switch Statement Example
#include <stdio.h>
int main()
{
int ch, n1, n2;
printf("1.Addition n");
printf("2.subtraction n");
printf("3.Multiplication n");
printf("4.Division n");
printf(“5.Modular Division n");
printf("enter your choice : ");
scanf("%d", &ch);
printf("Enter two operands: ");
scanf("%d %d",&n1, &n2);
switch(ch)
{
case 1:
printf(“sum= %d", n1+n2);
break;
case 2:
printf(“sub = %d“, n1-n2);
break;
case 3:
printf(“mul = %d", n1*n2);
break;
case 4:
printf(“division = %d“,n1/n2);
break;
case 5: printf(“rem = %d“,n1%n2);
break;
default:
printf("Error! Select only from 1 to 4");
}
return 0;
}
123. OUTPUT
1. Addition
2. subtraction
3. Multiplication
4. Division
5.Modular Division
enter your choice : 2
Enter two operands: 50 30
Sub = 20
OUTPUT
1. Addition
2. subtraction
3. Multiplication
4. Division
5.Modular Division
enter your choice : 6
Enter two operands: 50 30
Error! Select only from 1 to 5
124. Switch Statement Example
#include <stdio.h>
int main()
{
int n1, n2;
char ch;
printf("1.Addition n");
printf("2.subtraction n");
printf("3.Multiplication n");
printf("4.Division n");
printf("enter your choice : ");
scanf("%c", &ch);
printf("Enter two operands: ");
scanf("%d %d",&n1, &n2);
switch(ch)
{
case ‘+’:
printf(“sum= %d", n1+n2);
break;
case ‘-’:
printf(“sub = %d“, n1-n2);
break;
case ‘*’:
printf(“mul = %d", n1*n2);
break;
case ‘/’:
printf(“division = %d“,n1/n2);
break;
default:
printf("Error! Enter valid choice");
}
return 0;
}
125. OUTPUT
1. Addition
2. subtraction
3. Multiplication
4. Division
enter your choice : +
Enter two operands: 50 30
Sum = 80
OUTPUT
1. Addition
2. subtraction
3. Multiplication
4. Division
enter your choice : &
Enter two operands: 50 30
Error! Enter valid choice
126. Iterative Statements/ Loops
A loop is used to repeat a block of code until the specified condition
is met. Using loops we do not need to write the same code again and
again.
C programming has three types of loops:
• while loop
• do...while loop
• for loop
These loops controlled either at entry level or at exit level hence loops
can be controlled two ways:
1.Entry Controlled Loop
2.Exit Controlled Loop
127. Iterative Statements/ Loops
Entry Controlled Loop
Loop, where test condition is checked before entering the loop body,
known as Entry Controlled Loop. Example: while loop, for loop
Exit Controlled Loop
Loop, where test condition is checked after executing the loop body,
known as Exit Controlled Loop. Example: do while loop
128. While Loop
• The while loop is addressed as entry controlled loop/ pretest loop.
• The while loop evaluates the test expression inside the parenthesis
().
• If the test expression is true, statements inside the body of while
loop are executed. Then, the test expression is evaluated again.
• The process goes on until the test expression is evaluated to false.
• If the test expression is false, the loop terminates (ends).
130. While Loop-Example
// Print numbers from 1 to 5
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%dt", i);
i++;
}
return 0;
}
// Print numbers from 1 to n
#include <stdio.h>
int main()
{
int i = 1,n;
printf(“Enter the range :”);
Scanf(“%d”,&n);
while (i <= n)
{
printf("%d ", i);
i++;
}
return 0;
}
OUTPUT
1 2 3 4 5
OUTPUT
Enter the range : 10
1 2 3 4 5 6 7 8 9 10
131. do-while Loop
• The do...while loop is addressed as exit controlled loop/ post test
loop
• The body of do...while loop is executed at least once. Only then,
the test expression is evaluated.
• If the test expression is true, the body of the loop is executed again
and the test expression is evaluated.
• This process goes on until the test expression becomes false.
• If the test expression is false, the loop ends.
135. for Loop
• The initialization statement is executed only once.
• Then, the test expression is evaluated. If the test expression is
evaluated to false, the for loop is terminated.
• However, if the test expression is evaluated to true, statements
inside the body of for loop are executed, and the update expression
is updated.
• Again the test expression is evaluated.
• This process goes on until the test expression is false. When the
test expression is false, the loop terminates.
136. #include <stdio.h>
int main()
{
int i;
for (i = 1; i < =5; i++)
{
printf("%d ", i);
}
return 0;
}
#include <stdio.h>
int main()
{
int i,n;
printf(“Enter the range:”);
scanf(“%d”,&n);
for (i = 1; i < =n; i++)
{
printf("%d ", i);
}
return 0;
}
// Print numbers from 1 to 5 // Print numbers from 1 to n
for Loop-Example
OUTPUT
1 2 3 4 5
OUTPUT
Enter the range: 12
1 2 3 4 5 6 7 8 9 10 11 12
137. Example Programs
// 1. factorial of a given number
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",num,fact);
return 0;
}
output
Enter a number: 5
Factorial of 5 is: 120
138. Example Programs
// 2. sum of n natural numbers
output
Enter a positive integer: 10
Sum = 55
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i)
{
sum=sum+i;
}
printf("Sum = %d", sum);
return 0;
}
139. Example Programs
// 3. Multiplication Table Up to 10
output
Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i)
{
printf("%d * %d = %d n", n, i, n * i);
}
return 0;
}
140. Example Programs
// 4. sum of individual digits of a number
OUTPUT
Enter a number:654
Sum is=15
Enter a number:123
Sum is=6
#include<stdio.h>
int main()
{
int n,sum=0,rem;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}
141. We often come across some situations where we want to make a
jump from one statement to other statement, jump out of a loop or
to jump to next iteration of the loop instantly,.
This can be accomplished by the statements like :
break
continue
goto
Jumps in loops:(unconditional statements)
142. Break statement
The break statement ends the loop immediately when it is
encountered.
Syntax:
break;
144. Break statement- Example
# include <stdio.h>
int main()
{
int i;
for(i=1; i <= 10; i++)
{
if(i==6)
break;
printf("%d",i);
}
}
Output
1 2 3 4 5
# include <stdio.h>
int main()
{
int i=1;
while(i <= 10)
{
if(i==6)
break;
printf("%d",i);
i++;
}
}
Output
1 2 3 4 5
145. Break statement- Example
// Program to calculate the sum of a maximum of 10 numbers
// If a negative number is entered, the loop terminates
# include <stdio.h>
int main()
{
int i;
int number, sum = 0;
for(i=1; i <= 10; i++)
{
printf("Enter number %d: ",i);
scanf("%d",&num);
if(number < 0)
break;
sum = sum+ num;
}
printf("Sum = %d",sum);
}
output
Enter a number 1: 5
Enter a number 2: 6
Enter a number 3: 10
Enter a number 4: 45
Enter a number 5: 26
Enter a number 6: -56
Sum=92
146. continue statement
The continue statement skips the current iteration of the loop and
continues with the next iteration.
Syntax:
continue;
continue;
148. continue statement- Example
# include <stdio.h>
int main()
{
int i;
for(i=1; i <= 10; i++)
{
if(i==6)
continue;
printf("%d",i);
}
}
Output
1 2 3 4 5 7 8 9 10
# include <stdio.h>
int main()
{
int i=0;
while(i <= 10)
{
i++;
if(i==6)
continue;
printf("%d",i);
}
}
Output
1 2 3 4 5 7 8 9 10
149. continue statement- Example
// Program to calculate the sum of 10 positive numbers
// Negative numbers are skipped from the calculation
# include <stdio.h>
int main()
{
int i, num, sum = 0;
for(i=1; i <= 10; i++)
{
printf("Enter number%d : ",i);
scanf("%d",&num);
if(number < 0)
continue;
sum = sum+num;
}
printf("Sum = %d",sum);
}
output
Enter a number1 : 10
Enter a number2 : 20
Enter a number3 : 30
Enter a number4 : 40
Enter a number5 : 50
Enter a number6 : -5
Enter a number7 : 10
Enter a number8 : -8
Enter a number9 : 20
Enter a number10 : 30
Sum=210
150. goto statement
The goto statement is known as jump statement in C.
The goto statement allows us to transfer control of the program to the
specified label.
Syntax :
The label is an identifier. When the goto statement is encountered, the
control of the program jumps to label: and starts executing the code.
151. goto statement- Example
#include <stdio.h>
int main()
{
int sum=0;
for(int i = 0; i<=10; i++)
{
sum = sum+i;
if(i==5)
{
goto addition;
}
}
addition:
printf(“sum =%d", sum);
return 0;
}
Output
sum=15
152. //program to find reverse of a given number
#include<stdio.h>
int main()
{
int n,rev=0,rem;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf(“Reverse of a given number is=%d",rev);
return 0;
}
OUTPUT
Enter a number:987
Reverse of a given number is= 789