0% found this document useful (0 votes)
31 views

Unit 1

Uploaded by

ashishadicted4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Unit 1

Uploaded by

ashishadicted4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Unit-1

Introduction to Programming Methodology


Programming Methodology is the approach to solve a program. The solution need to verbalised or expressed in a
language. It does not refer to a specific language but the way how you solve the problem using a language. There are lots
of programming languages that are well-known but all of them need to follow some strategy when they are implemented.

Problem solving encompasses the following activities:-

❖ Defining the problem


❖ Analysis of the problem
❖ Detailed specification with system
❖ Design
❖ Implementation
❖ Testing and debugging
❖ Documentation
❖ Training and support
❖ Maintenance
Types of Programming Methodologies

Procedural Programming

Object-oriented Programming

Logic programming

Functional Programming
High-level languages include the popular BASIC programming language as well as
other languages that just aren’t that popular any more BASIC reads almost like
English, and all its commands and instructions are English words — or at least
English words missing a few vowels or severely disobeying the laws of spelling.

The lowest of the low-level programming languages is machine language. That


language is the actual primitive grunts and groans of the microprocessor itself.
Machine language consists of numbers and codes that the microprocessor
understands and executes.
Source code : The source code file is a text file on disk. The file contains
instructions for the computer that are written in the C programming language.
(the file extension must be .c)
#include<stdio.h>

int main()

{ puts("Hello, World");

return 0;

}
Steps for C program
1. Save source code in a text editor and save it as a text file with the c (single
letter c) extension.
2. Compile
3. Link
4. Run
Compiler,Assembler,Interpreter
Compiler is a computer program that reads a program written in one language,
which is called the source language, and translates it in to another language,
which is called the target language. Most often, the source language is a high level
language and the target language is a low level language.

Assembler is a software or a tool that translates Assembly language to machine


code. So, an assembler is a type of a compiler and the source code is written in
Assembly language.

The Interpreter translates the program written in high level language into machine
language at the time of executing that program, instructions by instructions. That
is, it reads the first instruction written in the program and converts that into
equivalent machine language instructions.
#include<stdio.h> /* For
printfsum() & scanf() */
#include<conio.h> /* For
clrscr() & getch() */
int main() /* Starting point of the
program execution*/
{
int a,b,c,d,e,sum;
float avg; /* Variable Declarations */
clrscr(); /* Clear Screen */
printf("enter five numbers"); /* Request for Input */
scanf("%d %d%d%d%d”,&a,&b,&c…); /* Input from user
*/
sum=a+b+c+d+e; /* Adding
Tokens
The smallest individual unit known as tokens. C recognizes six types of tokens. C
programs are written using these tokens and the syntax of the language. Following
are the C Tokens:

Keywords :- Keywords are the reserved words whose meaning has already been
explained to the C compiler. These words are defined in the language itself. There
are 32 keywords in C.

Identifiers :- Identifiers are the names given to variables. Using this identifier we
can access that variable. All other names in a C program such as array name or a
function name are also known as Identifiers.

Variable :- Variable names are names given to locations in memory. These


locations can contain integer, real or character constants. All the variables that are
used in the program should be declared i.e. typed at the top with their respective
data types.
Constants :- Constants in C refer to fixed values that do not change during the
execution of a program.
Operators :- Operators can be used to perform the required computations on the
values. Types of operators: There are three types of operators:
1. Unary Operator: Operators which work on one operand only.
2. Binary Operator: Operators which work on two operands.
3. Ternary Operator: Operators which work on three operands.
Special Characters in C :- Some special characters are used in C, and they have a
special meaning which cannot be used for another purpose.

● Simple brackets ( ): It is used in function declaration and function calling. For example, printf() is a
predefined function.
● Hash/preprocessor (#): It is used for pre-processor directive. It basically denotes that we are using
the header file.
● Curly braces { }: It is used in the opening and closing of the code. It is used in the opening and
closing of the loops.
FLOWCHART
HEADER FILES
#include <stdio.h>

#include "mydecls.h"
ALGORITHMS
An algorithm is a procedure or step-by-step instruction for solving a problem. They form the foundation of writing a
program.

Algorithm to add two numbers –

1. Read A,B.

2. Set SUM := A+B.

3. Write SUM.

4. Exit.
PSEUDOCODE
Pseudocode is a simple way of writing programming code in English. Pseudocode is not actual programming language. It
uses short phrases to write code for programs before you actually create it in a specific language.

main()

integer a,b,sum;

read in a and b;

add a & b and set it to sum;

write sum;

}
Storage classes
Program Execution
Sequence logic -- that simply means the statements in the programs are
executed one after the other, from the first to the last.

Selection logic -- they will test some condition and take different courses of
action based on whether the condition is true or false.

Selection logic is implemented using the if and the if...else statements.

Repetition Logic -- The idea is to write statements once and get the computer to
execute them repeatedly as long as some condition is true.

Repetition logic using the while and for statements.


if (<condition>) if (<condition>)
{
{ ...
}
<statement1>; else {
...
}
<statement2>;

...

satament3
if(){
if(){
if(){
if (){
}else{
... }
}else {
} }
else{
else if() { }

... if(num % 2 == 0){


printf(“EVEN”);
} }
else{
else {
printf(“ODD”);
... }

}
Switch Case –
switch(expression)
{ case 1: statement 1
sequence; break;
case 2: statement 2 sequence; break;
case 3: statement 3 sequence; break;
-- -- -- case n: break;
default : default statement sequence;
}
Control Statements (Looping)
Looping is a process of repeating a single statement or a group of statements until some condition for
termination of the loop is satisfied.

There are four Parts of a loop -


1. Initialization.
2. Conditions
3. Statements
4. Incrementation or Decrementation.

Entry control loops: Those loops in which condition is checked before the execution of the statement.
Thus if the condition is false in the beginning the loop will not run even once. (for,while)
Exit control loops: Those loops in which the condition is checked after the execution of the statement.
Thus if the condition is false in the beginning the loop will run at least once. (do-while)
While loop
initialize; #include <stdio.h>
main()
while(condition) {
int i=0;
while( i<100 )
{ {
printf("\ni=%d",i);
statement; i=2i-1;
}
increment or decrement; statement3;
}
} 0
-1
Do - While Loop
initialize; int j = 5;

do printf("start\n");

{ do {

statement; printf("j = %i\n");

increment or decrement; j--;

} while(condition); }while(j > 0);

statement1 printf("stop\n");
For Loop
for (initialize; condition; increment or decrement) #include<stdio.h>
{ #include<conio.h>
main()
statement; {
int i;
for(i=1;i<100;i++ )
}
{
printf("\ni=%d",i);
}
}
continue - changes flow of a program. It does break - The break statement is used inside a loop,
to directly come out of the loop. We have already
not terminate the loop however. It just skips the
used break in switch statement. It is used to skip the
rest of current iteration of the loop and returns
execution of the loop any further and transfer the
to starting point of the loop. control of the program to the statement following the
loop.
#include <stdio.h>
int main() {
#include <stdio.h>
int i;
double number, sum = 0.0; int main() {
int i;
for (i = 1; i <= 10; ++i) { double number, sum = 0.0;
printf("Enter a n%d: ", i);
scanf("%lf", &number); for (i = 1; i <= 10; ++i) {
printf("Enter a n%d: ", i);
if (number < 0.0) { scanf("%lf", &number);
continue;
} // if the user enters a negative number, break
the loop
if (number < 0.0) {
sum += number; // sum = sum + number; break;
} }

printf("Sum = %.2lf", sum); sum += number; // sum = sum + number;


}
return 0;
} printf("Sum = %.2lf", sum);

return 0;
}
goto-It is also used to jump from one part of the program to the other or exit from
the deeply nested loop. But using goto is not a good practice because it makes the
logic complicated and long programs unreadable.

return - Used in a function to return some value and to jump from called function
definition to calling function definition.

exit() - It is not a jump statement. It is used to terminate the program or directly


exit from the program without following the intermediate statements. It is a library
function included in stdlib.h and process.h. exit is used as: exit( )
int main() #include <stdio.h>
{ #include <stdlib.h>
int sum=0;
for(int i = 0; i<=10; i++){
sum = sum+i;
int main () {
if(i==5){ printf("Start of the program....\n");
goto addition;
} printf("Exiting the program....\n");
} exit(0);
addition:
printf("End of the program....\n");
printf("%d", sum);

return 0; return(0);
} }

You might also like