Research 5
Research 5
C Programming
C Languageis a general purpose language is developed by Dennis
Ritchie at the Bell Laboratory in 1972
Types of Language
1 Low Level: *Assembly Language ,* Machine Language
In procedural programming, the program is divided into In object-oriented programming, the program is divided
small parts called functions. into small parts called objects.
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any proper way of Object-oriented programming provides data hiding so it
hiding data so it is less secure. is more secure.
In procedural programming, there is no concept of data In object-oriented programming, the concept of data hiding
hiding and inheritance. and inheritance is used.
In procedural programming, the function is more important In object-oriented programming, data is more important
than the data. than function.
Procedural programming is based on the unreal world. Object-oriented programming is based on the real world.
Procedural programming is used for designing medium- Object-oriented programming is used for designing large
sized programs. and complex programs.
Procedural programming uses the concept of procedure Object-oriented programming uses the concept of data
abstraction. abstraction.
Code reusability absent in procedural programming, Code reusability present in object-oriented programming.
It is programmer friendly It is a machine friendly
1.
language. language.
High level language is less Low level language is high
2.
memory efficient. memory efficient.
Debugging is complex
4. Debugging is easy.
comparatively.
It is complex to maintain
5. It is simple to maintain.
comparatively.
6. It is portable. It is non-portable.
Algorithms play a crucial role in various fields and have many applications. Some of
the key areas where algorithms are used include:
Computer Science: Algorithms form the basis of computer programming and are used
to solve problems ranging from simple sorting and searching to complex tasks such as
artificial intelligence and machine learning.
Mathematics: Algorithms are used to solve mathematical problems, such as finding
the optimal solution to a system of linear equations or finding the shortest path in a
graph.
Operations Research: Algorithms are used to optimize and make decisions in fields
such as transportation, logistics, and resource allocation.
Artificial Intelligence: Algorithms are the foundation of artificial intelligence and
machine learning, and are used to develop intelligent systems that can perform tasks
such as image recognition, natural language processing, and decision-making.
Data Science: Algorithms are used to analyze, process, and extract insights from large
amounts of data in fields such as marketing, finance, and healthcare.
Properties of an Algorithm:
1) Finiteness: - An algorithm terminates after a finite numbers of steps.
2) Definiteness: - Each step in algorithm is unambiguous. This means
that the action
specified by the step cannot be interpreted (explain the meaning of) in
multiple ways & can
be performed without any confusion.
3) Input: - An algorithm accepts zero or more inputs
4) Output:- An algorithm should produce at least one output.
What are the Characteristics of an Algorithm?
Step 2: Designing the algorithm
Now let’s design the algorithm with the help of the above pre-requisites:
Algorithm to add 3 numbers and print their sum:
1. START
2. Declare 3 integer variables num1, num2, and num3.
3. Take the three numbers, to be added, as inputs in variables num1, num2, and num3
respectively.
4. Declare an integer variable sum to store the resultant sum of the 3 numbers.
5. Add the 3 numbers and store the result in the variable sum.
6. Print the value of the variable sum
7. END
Flow Charts
5. Connectors: Whenever flowchart becomes complex or it spreads over more than one page, it is
useful to use connectors to avoid any confusions. It is represented by a circle.
6. Flow lines: Flow lines indicate the exact sequence in which instructions are executed. Arrows
represent the direction of flow of control and relationship among different symbols of flowchart .
Question 1. Draw a flowchart to
find the greatest number among
the 2 numbers.
Solution:
Algorithm:
1. Start
2. Input 2 variables from user
3. Now check the condition If a > b, goto step
4, else goto step 5.
4. Print a is greater, goto step 6
5. Print b is greater
6. Stop
Psuedocode
int main() {
printf("Hello World!");
return 0;
}
Example explained
Line 1: #include <stdio.h> is a header file library that lets us work with input and output functions,
such as printf() (used in line 4). Header files add functionality to C programs.
Don't worry if you don't understand how #include <stdio.h> works. Just think of it as something that
(almost) always appears in your program.
Line 2: A blank line. C ignores white space. But we use it to make the code more readable.
Line 3: Another thing that always appear in a C program is main(). This is called a function. Any code
inside its curly brackets {} will be executed.
Line 4: printf() is a function used to output/print text to the screen. In our example, it will output "Hello
World!".
Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
Statements
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by the compiler (will not be
executed).
Example
// This is a comment
printf("Hello World!");
Multi-line Comments
Example
/* The code below will print the words Hello World!
to the screen, and it is amazing */
printf("Hello World!");
Tokens:
The smallest individual units are known as tokens. C has
six types of tokens.
1: Identifiers
2: Keywords
3: Constants
4: Strings
5: Special Symbols
6: Operators
Identifiers:
A variable in C is a memory location with some name that helps store some
form of data and retrieves it when required. We can store different types of
data in the variable and reuse the same variable for storing some other data
any number of times.
Syntax
type variableName = value;
The general rules for naming variables are:
Types of Variables in C
There are many types of variables in c:
1. local variable
2. global variable
3. static variable
C Variable Names
All C variables must be identified with unique names.
Constants refer to fixed values that do not change during the execution of a program.
Note: constants are also called literals.
TYPES OF C CONSTANT:
1. Integer constants
2. Real or Floating point constants
3. Character constants
4. String constants
5. Backslash character constants
Two ways to define constant in C
There are two ways to define constant in C programming.
1. const keyword
2. #define preprocessor
1) C const keyword 2) C #define preprocessor
The const keyword is used to define The #define preprocessor is also used to define constant.
constant in C programming. C#define
1. const float PI=3.14; The #define preprocessor directive is used to define constant
Now, the value of PI variable can't be or micro substitution. It can use any
changed. basic data type.
1. #include <stdio.h> Syntax:
2. #include <conio.h> #define token value
3. void main(){ Let's see an example of #define to define a constant.
4. const float PI=3.14; #include <stdio.h>
5. clrscr(); 1. #define PI 3.14
6. printf("The value of PI is: %f",PI); 2. main() {
7. getch(); 3. printf("%f",PI);
8. } 4. }
Output: Output:
The value of PI is: 3.140000 3.140000
Example
// Student data
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25;
char studentGrade = 'B';
// Print variables
printf("Student id: %d\n", studentID);
printf("Student age: %d\n", studentAge);
printf("Student fee: %f\n", studentFee);
printf("Student grade: %c", studentGrade);
Calculate the Area of a Rectangle In this real-life example, we create a program to calculate the
area of a rectangle (by multiplying the length and width):
Example
// Create integer variables
int length = 4;
int width = 6;
int area;
Pre-Increment:
b = ++y;
In this example suppose the value of variable „y‟ is 5 then value of variable „b‟ will be 6 because
the value of „y‟ gets modified before using it in a expression.
Post-Increment:
b = x++;
In this example suppose the value of variable „x‟ is 5 then value of variable „b‟ will be 5 because
old value of „x‟ is used.
Bit Wise Operator
Special operators :-
--------------------------------------
C language provides 2 types of special operators.
a. Sizeof()
b. Comma( , )
Sizeof() :-
------------------
<18 Minor
F
else major
Switch :-
---------------------------------- case constantN : body; break;
❖ It is a control statement. default: body;
}
❖ It is a decision making statement.
❖ It is a conditional statement.
❖ It is multi way selection statement.
❖ It is a branching statement.
❖ It is used when we need to select a single choice from group of
choices.
Syntax :
switch ( expression )
{
case constant1: body; break;
case constant2: body; break;
case constant3: body; break;
…
WAP to read a single digit number.
Spell it.
case 4: printf("Four"); break;
#include<stdio.h> case 5: printf("Five"); break;
void main() case 6: printf("Six"); break;
case 7: printf("Seven"); break;
{ case 8: printf("Eight"); break;
case 9: printf("Nine"); break;
int no; default: printf("Invalid input");
printf("enter a single digit number : }
"); }
scanf("%d",&no);
Output1 :
switch( no ) Enter a single digit number : 5
{ Five
Output2 :
case 0: printf("Zero"); break;
case 1: printf("One"); break;
case 2: printf("Two"); break;
While :-
-------------------
❖ It is a control statement
❖ It is a conditional statement.
❖ It is an iterative tool.
❖ It is a pre test loop.
❖ It is used to execute the code repeatedly.
Syntax :
while( condition )
{
Body;
}
1. WAP to print your name 5 times.
#include<stdio.h>
void main()
{
int i=1;
while( i<=5 )
{
printf("\nHello World");
i++;
}
}
3. WAP to print odd numbers from 1 to 100.
#include<stdio.h>
void main()
{
int no=1;
while( no<=100 )
{
printf("%5d",no);
no+=2;
}
}
Output :
1 3 5 7 9 … 99
For :-
❖ It is a control statement.
❖ It is a conditional statement.
❖ It is an iterative statement.
❖ It is a pre test loop.
❖ It can carry initialization, condition and inc/dec in one line.
Syntax :
for( initialization ; condition ; inc/dec )
{
Body;
}
Note :
❖ If there are more than one initialization then we have to separate them by
camma.
❖ If there are multiple increments or decrements then we have to separate
them by camma.
WAP to read a number. Print the multiplication
table.
#include<stdio.h>
void main()
{
int no,i;
printf("Enter a number : ");
scanf("%d",&no);
for( i=1;i<=10;i++) Output :
Enter a number : 5
printf("\n%d*%d=%d",no,i,no*i); 5*1=5
} 5*2=10
5*3=15
…
5*10=50
Do-While :-
It is a control statement.
It is a conditional statement.
It is an iterative statement.
it is used to execute the code repeated.
It is a post test loop.
The body will execute at least once.
It is used when we don’t know exact count of iteration.
Syntax :
do
{
body;
}while( condition );
WAP to find sum of elements entered by user.
#include<stdio.h>
Output :
void main()
Enter a number : 1
{ Do want to add one more? y
int no,sum=0; char ch; Enter a number : 2
Do want to add one more? y
do Enter a number : 3
{ Do want to add one more? y
Enter a number : 4
printf("\nEnter a number : "); Do want to add one more? y
scanf("%d",&no); Enter a number : 5
sum=sum+no; Do want to add one more? n
printf("\nDo want to add one more? "); Sum=15
scanf(" %c",&ch);
}while( ch!='n’);
printf("Sum=%d",sum);
}
Break :-
It is a control statement.
It is an un conditional statement.
It is used to stop the loop at middle.
It can be used only with switch, while, for, do-while.
Example :
#include<stdio.h> void main()
{
int no;
for(no=1;no<=100;no++)
{
printf("%5d",no);
if( no==10 )
break;
}
}
Output :-
1 2 3 4 5 6 7 8 9 10
Continue :-
It is a control statement.
It is a decision making statement.
It is a un conditional statement.
It is used to skip the rest of the code of current iteration.
It can be used only with while, for, do-while.
Example :
#include<stdio.h> void main()
{
int no;
for(no=1;no<=100;no++)
{
if( no%2==0 ) continue;
printf("%5d",no);
}
}
Output :
1 3 5 7 9 …. 99
Goto :-
It is a control statement.
It is a un conditional statement.
It is a decision making statement.
It is used to move the control from one location to another
location.
It move the cursor to the specified Lable.
Label is a name which always ends with “:” symbol
Example :
#include<stdio.h>
void main()
{
printf("\none");
goto ss;
printf("\ntwo");
printf("\nthree");
ss:
printf("\nfour");
printf("\nfive");
}
Output :
One
four
five