parajulirajesh.com.np

What is program?

The program is the set of  instructions that command thecomputer to perform a particular operation or a specific task.

  • Instruction is a statement.
  • A statement is an instruction to do only one task.
  • A group of statements is composed together to form a program.

Unit 1 Introduction of programming Language

A programming language is a computer language that is used by programmers (developers) to communicate with computers. It is a set of instructions written in any specific language ( C, C++, Java, Python) to perform a specific task.

  • A programming language is a language that allows people to write specific commands to be executed on a computer.
  • A programming language is mainly used to develop desktop applications, websites, and mobile applications.
  • Most commonly used Programming Language are Python, JAVA, C, C++, C#, RUBY, PHP … etc.

Types of Programming Language
1. Low-level Language
2. High Level Language

Translator:

Translator is a software that translates or converts instructions written in assembly or high level language (source code) into Machine Level Language (Object code). 

Why use translator?  

The program is developed in assembly or in high level language for the easyness, understandable (Convenience) of the programmer but computer does not understands these statements directly, so the translator is used which converts high level statements (source code) in a machine executable format(object code).

Computers understand only machine code:

  • Human languages like Python, C, or Java cannot be executed directly.
  • Translator converts them into machine code (0s and 1s).

Makes programming easier:

  • Programmers can write in a high-level language, which is easier to understand.
  • Translator handles the conversion to low-level instructions.

Detects errors:

  • Compilers/interpreters can show syntax or logical errors in the code.

Portability:

  • High-level code can run on different machines if translated properly.

Assembler: Assembler is a translator that converts the code of the assembly language (Source Code) into the machine language (Object Code). When assembler completed it converted process then only it started to execute the program.
Compiler: A Compiler is a translator which translates the complete high level program (Source Code) into the machine code
(object code) at once if the program doesn’t contain syntax errors. Programming languages like C, C++, Java use the compiler.
Interpreter: An interpreter is a language translator which translates high-level language into the machine language at one line at a
time and executes the line of the program after it has been translated. It translates statements line by line.

Differences between Compiler and interpreter?

CompilerInterpreter
Translates the entire program into machine code at once.Translates the program line-by-line.
Translation is faster once compiled.Translation is slower because it works one line at a time.
Stores the machine code on disk after compilation.Does not store machine code on disk.
Shows all errors after compiling the whole program; debugging is harder.Shows errors line-by-line; debugging is easier.
Examples: C, C++, JavaExamples: Python, PHP, BASIC, C#

Syntax

In a programming language, Syntax defines the rules that governs the structure and arrangement of keywords, symbols, and other elements. Syntax doesn’t have any relationship with the meaning of the statement; it is only associated with the grammar and structure of the programming language.

  • A line of code is syntactically valid and correct if it follows all the rules of syntax.
  • Syntax does not have to do anything with the meaning of the statement.
  • Syntax errors are easy to catch.
  • Syntax errors are encountered after the program has been executed.

Semantics

Semantics refers to the meaning of the associated line of code and how they are executed in a programming language. semantics helps interpret what function the line of code/program is performing.

  • If there is any semantic error and even when the statement has correct syntax, it wouldn’t perform the function that was
    intended for it to do. Thus, such errors are difficult to catch.
  •  Semantics are encountered at runtime.

Programming Design Tools

Program design Tools are the tools that are used to design a program before it actually developed. Program design tools are used by the developers. Some program design tools are: Algorithm, Flow Charts, Pseudo Code, Data flow Diagram(DFD), Usecase Diagram… etc.

Algorithm: An algorithm is a step-by-step procedure or set of instructions used to solve a problem.

  • An algorithm is the sequence of steps that needs to be followed in order to acheive certain task.
  • An algorithm is the fininte set of step by step set of statements that is used to solve a particular problem.
  •  It is written in simple human readable English Language.

An Algorithm should have the following properties:
1. It should have an input.
2. The steps mentioned in an algorithm can be executable by the computer
3. Each and every instruction should be in a simple language.
4. The number of steps should be finite.
5. It should not depend on a particular computer language or computer.
6. The algorithm should give an output after executing the finite numbers of steps.

Algorithm: Find the Sum of Two Numbers

Step 1: START
Step 2: Read the two numbers A and B
Step 3: Add the numbers → S = A + B
Step 4: Display S
Step 5: STOP

Algorithm: Find the Simple Interest (SI)

Step 1: START
Step 2: Read the principal (P), rate (R), and time (T)
Step 3: Multiply P, R, and T → Product = P × R × T
Step 4: Divide the product by 100 to get SI → SI = (P × R × T) / 100
Step 5: Display SI
Step 6: END

Algorithm: Print numbers from 1 to 20

Step 1: START
Step 2: Initialize X = 0
Step 3: Increment X by 1
Step 4: Print X
Step 5: If X < 20, go back to Step 3
Step 6: STOP

PRACTICE QUESTIONS 

  1. Find the area of a rectangle

  2. Find the square of a number

  3. Print the first 10 natural numbers

  4. Find the average of three numbers

  5. Check if a number is positive or negative

  6. Check if a number is even or odd.

  7. Swap Two Numbers

Flowchart

A flowchart is a pictorial representation of an algorithm. Flowchart is a diagrammatic representation of sequence of logical steps of a program. Flowcharts use simple geometric shapes to depict processes and arrows to show relationships and process/data flow.

Difference between Algorithm and Flowchart?

AlgorithmFlowchart
It is a procedure for solving problems.It is a graphic representation of a process.
The process is shown in step-by-step instructions.The process is shown in block-by-block diagram.
It is complex and difficult to understand.It is intuitive and easy to understand.
It is convenient to debug errors.It is hard to debug errors.
The solution is showcased in natural language.The solution is showcased in pictorial format.
It is somewhat easier to solve complex problems.It is harder to solve complex problems.
It takes more time to create an algorithm.It takes less time to create a flowchart.

Features of good programme

 

  1. Correctness
    A good program gives the right output for the given input. It does what it is supposed to do.

  2. Readability
    The program should be easy to read and understand. This is done by using proper names, spacing, and comments.

  3. Simplicity
    A good program is simple, not unnecessarily complicated. Simple code is easier to write, understand, and fix.

  4. Efficiency
    The program should use less time and memory. It should work fast and not waste computer resources.

  5. Maintainability
    The program should be easy to update or fix in the future if changes are needed.

  6. Modularity
    The program should be divided into small parts (functions or modules). This makes it easier to test and understand.

  7. Portability
    A good program can run on different computers or systems with little or no change.

  8. Reliability
    The program should work properly every time and handle errors smoothly without crashing.

Unit-2 Introduction to C

C programming is a general-purpose, procedural programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. e The UNIX OS was totally written in C.

History of C

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. C was developed to overcome the problems of previous languages such as B, BCPL, etc.

Basic Structure of C program

  1. Documentation Section
  2. Link Section
  3. Definition Section
  4. Global Declaration Section
  5. main() Function
  6. User-Defined Functions

In C language, all these six sections together make up the Basic Structure of C Program

1. Documentation Section

Programmers write comments in the Documentation section to describe the program. The compiler ignores the comments and does not print
them on the screen. Comments are used only to describe that program.

/* File Name -: Hello.c
Author Name -: Rajesh parajuli
Founder of parajulirajesh.com.np
Date -: 12/09/2023
Description -: Basic Structure of C program */
//This is a single line comment

2. Link Section or Preprocessor Section

Includes header files needed by the program. Header files contain predefined functions. Within the Link Section, we declare all the Header Files that are used in our program. From the link section, we instruct the compiler to link those
header files from the system libraries, which we have declared in the link section in our program.

#include <stdio.h>

3. Definition Section

The Definition Section of a C program is used to define constants and macros using the #define directive. The values defined in this section do not change during program execution. It helps make the program easy to understand, avoids repeating fixed values, and allows changes to be made in one place. This section is written before the main() function and improves the readability and maintainability of the program.

#define PI 3.14
#define SQUARE(x) (x * x)

4. Global Declaration Section

The Global Declaration Section of a C program is where global variables and function prototypes are declared. Variables declared here, called global variables, can be used in any part of the program, including main() and other functions. Function prototypes tell the compiler about functions that will be defined later. This section is written before the main() function and helps make the program organized, easy to understand, and maintain.

int total; // global variable
void display(); // function prototype

5.main() Function

The main() function is the starting point of a C program where execution begins. All the program’s instructions are written inside it, and it can call other functions. It usually returns 0 to indicate that the program ran successfully.

int main() {
// statements
return 0;
}

6.User-Defined Functions

User-defined functions are the functions written by the programmer to perform specific tasks in a C program. They help make the program organized, reusable, and easy to understand.

#include <stdio.h>
void greet() { // user-defined function
printf(“Hello!”);
}
int main() {
greet(); // calling the function
return 0;
}

Character Set

As every language contains a set of characters used to construct words, statements, etc., C language also has a set of characters which
include alphabets, digits, and special symbols. C language supports a total of 256 characters. Every character in C language has its equivalent
ASCII (American Standard Code for Information Interchange) value.

Every C program contains statements. These statements are constructed using words and these words are constructed using characters from C
character set. C language character set contains the following set of characters:
1. Alphabets
2. Digits
3. Special Symbols
Alphabets:
C language supports all the alphabets from the English language. Lower and upper case letters together support 52 alphabets.
lower case letters — a to z
UPPER CASE LETTERS — A to Z
Digits: C language supports 10 digits which are used to construct numerical values in C language.

Digits — 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols:
C language supports a rich set of special symbols that include symbols to perform mathematical operations, to check conditions, white spaces,
backspaces, and other special symbols.
Special Symbols -~ @#$%*&*()_-+={}[]5:5″/?.>,<\]

A token is a smallest individual element of a program which is meaningful to the compiler. The compiler that breaks a program into the smallest
units is called tokens and these tokens proceed to the different stages of the compilation. Tokens in C are building blocks which means a program can’t be created without tokens.

Classification of Tokens are:  

  1. Keywords
  2. Identifiers
  3. Constants
  4. Strings
  5. Special Symbols
  6. Operators

Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and
they cannot be used as an identifier.
 C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all Keywords allowed in ANSI C.

C Predefined 32 Keywords

Identifier

Identifiers in C are used for naming variables, functions, arrays, structures, etc. Identifiers in C are the user-defined words. It can be composed of
uppercase letters, lowercase letters, underscore, or digits, but the starting letter should be either an underscore or an alphabet. Identifiers cannot
be used as keywords. Rules for constructing identifiers in C are given below:

  • An identifier can only have alphanumeric characters (a-z , A-Z , 0-9) (i.e. letters and digits) and underscore( _ ) symbol.
    Identifier names must be unique
  • The first character must be an alphabet or underscore.
  • You cannot use a keyword as an identifier.
  • Only the first thirty-one (31) characters are significant.
    It must not contain white spaces.
  • Identifiers are case-sensitive.

For Example:
int cprogram;
Char Bicte_firstsemester;
here, int and Char is keywords and cprogram and Bicte_firstsemester is identifier.

Constants:

The constants in C are the read-only variables whose values cannot be modified once they are declared in the C program. The type of constant
can be an integer constant, a floating pointer constant, a string constant, or a character constant. In C language, the const keyword is used to
define the constants.

What is a constant in C?
As the name suggests, a constant in C is a variable that cannot be modified once it is declared in the program. We can not make any
change in the value of the constant variables after they are defined.

Syntax to Define Constant
const data_type var_name = value;

Strings:

Sequence of Characters is known as Strings.
e Every String is terminated by \0
¢ String Constant is a sequenced os 0 or more characters enclosed between double quotes ”” is known as string constant e.g. “S”, “XYZ”,
“123”, “hello world”
¢ All characters are converted into their corresponding ASCII value and then stored in memory as contiguous allocation.
¢ String Variable is the array of character type. For e.g. char[10];
char greeting[6] = {H’, ‘e’, ‘I’, I’, ‘o’, ‘\O’};
If you follow the rule of array initialization then you can write the above statement as follows —
char greeting[] = “Hello’;

#include<stdio.h>
struct student
{
int id;
char name[50];
};
int main()
{
FILE *ptr;
struct student s[3];
ptr=fopen(“rajesh.txt”,”w”);
int i;
 
for(i=0;i<3;i++)
{printf(“enter id and name: “);
  scanf(“%d%s”,&s[i].id,s[i].name);
}
for(i=0;i<3;i++)
{
fprintf(ptr,”%d student record: “,i+1);
fprintf(ptr,”student id =%d and name=%s\n”,s[i].id,s[i].name);
}
return 0;
}
Scroll to Top

Rajesh Parajuli

SERVICES

BICTE GMC

LEC.RAJESH PARAJULI

Address: Ghodaghodi Municipality-1 Sukhad kailali

Contact: 9847546279

Ghodaghodi Multiple Campus BICTE