Engineer Simple https://www.engineersimple.com/2022/07/C%20Program%20Learning%20Basic%20Part.html

C Program Learning Basic Part

 Computer Programming 

What is Computer Programming?

Computer programming is defined as a process of developing and implementing various set of instructions given to the computer to perform a certain predefined task. Computer Programming is easy if it is appropriately managed. There are many computer programming languages available so finalizing the right language is not an easy task.

C Program Exercise Basic Part


What is algorithm?

An algorithm is a procedure or step-by-step instruction for solving a problem. They form the foundation of writing a program. An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values as output. For writing any programs, the following has to be known:

•Input 

•Tasks to be preformed 

•Output expected

Importance of algorithm in computer programming: 

• Easy to understand the steps of solving the problems. 

• It does not depend on any programming languages. 

• Find out the errors in a program. 

• Helps to change and update a program. 

• It helps to write a program.

 

BASIS FOR COMPARISON

COMPILER

INTERPRETER

Input

It takes an entire program at a time

It takes a single line of code or instruction at a time

Output

It generates intermediate object code

It does not produce any intermediate object code

Working mechanism

The compilation is done before execution

Compilation and execution take place simultaneously

Speed

Comparatively faster

Slower

Memory

Memory requirement is more due to the creation of object code

It requires less memory as it does not create intermediate object code

Errors

Display all errors after compilation, all at the same time

Displays error of each line one by one

Error detection

Difficult

Easier comparatively

Pertaining Programming languages

C, C++, C#, Scala, typescript uses compiler

PHP, Perl, Python, Ruby uses an interpreter

 

What are the characteristics necessary for a sequence of instructions to qualify as an algorithm-

The characteristics of a good algorithm are:

Precision – the steps are precisely stated(defined).

Uniqueness – results of each step are uniquely defined and only depend on the input and the result of the preceding steps.

Finiteness – the algorithm stops after a finite number of instructions are executed. Input – the algorithm receives input.

Output – the algorithm produces output.

Generality – the algorithm applies to a set of inputs.

Machine language

Sometimes referred to as machine code or object code, machine language is a collection of binary digits or bits that the computer reads and interprets. Machine language is the only language a computer is capable of understanding. Computer programs are written in one or more programming languages, like C++, Java, or Visual Basic. A computer cannot directly understand the programming languages used to create computer programs, so the program code must be compiled. Once a program's code is compiled, the computer can understand it because the program's code is turned into machine language.

 

What is a Flowchart? 

Flowchart is a graphical representation of an algorithm. Programmers often use it as a program planning tool to solve a problem. It makes use of symbols which are connected among them to indicate the flow of information and processing. The process of drawing a flowchart for an algorithm is known as “flowcharting”.

Basic Symbols used in Flowchart Designs 

Terminal: 

The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A pause/halt is generally used in a program logic under some error conditions. Terminal is the first and last symbols in the flowchart. 

Input/Output: 
A parallelogram denotes any function of input/output type. Program instructions that take input from input devices and display output on output devices are indicated with parallelogram in a flowchart.

Processing: 
A box represents arithmetic instructions. All arithmetic processes such as adding, subtracting, multiplication and division are indicated by action or process symbol.
Decision: 
Diamond symbol represents a decision point. Decision based operations such as yes/no question or true/false are indicated by diamond in flowchart.
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. 


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. 

*Draw a flowchart to input two numbers from user and display the largest of two numbers
There are two operators which are known as Equality Operators:
Equal To Operator (= =) 
Not Equal To Operator (!=) 
1) "Equal To" Operator (= =)
It’s a binary operator and works on two operands, it returns 1 if value of both operands are equal else it returns 0.
Syntax: 
Operand1 = = Operand2 
Consider the example: 
#include<stdio.h> 
int main() 
int a=10; 
int b=10; 
int result; 
result = (a==b); 
printf("result: %d\n",result); 
return 0; 
}
What is return data type. 
#include<stdio.h>  
#include<stdlib.h>  
#include<math.h>  
int sum();
int main() 
int num; 
num=sum(); 
printf("\nsum of two integer number = %d",num); 
return 0; 
int sum() 
int a=50, b=80, sum; 
sum=sqrt(a)+sqrt(b); 
return sum; 
}

Types of errors in C programming

Basically there are three types of errors in c programming
1. Run time Errors 
2. Compile Errors 
3. Logical Errors

1. Run time Errors: 
C runtime errors are those errors that occurs during the execution of the program and generally occurs due to some illegal operation performed in the program.

Examples of some illegal operations that may produce runtime errors are:
 
1. Dividing a number by zero 
2. Trying to open a file which is not created. 
3. Lack of free space.

2. Compile Errors: 
Compile errors are those errors that occur at the time of compilation of the program. C compile errors can be classified as: 
i. Syntax Error 
ii. Semantic Error 

3. Logical Error:
On compilation and execution of a program, desired output is not obtained when certain input values are given. These types of errors which provide incorrect output but appears to be error free are called logical errors. These are one of the most common errors done by beginners of programming. These errors solely depend on the logical thinking of the programmer and are easy to detect if we follow the line of execution and determine why the program takes that path of execution.
int main() 
int i = 0; 
// logical error : a semicolon after loop 
for(i = 0; i < 3; i++); 
{
 printf("loop "); 
continue;

Write a C program to swap two numerical values.

#include<stdio.h>

int main()

{

double first, second, temp;

printf("Enter first number: ");

scanf("%lf", &first);

printf("Enter second number: ");

scanf("%lf", &second);

// Value of first is assigned to temp

temp = first;

// Value of second is assigned to first

first = second;

 // Value of temp (initial value of first) is assigned to second

second = temp;

printf("\nAfterswapping, firstNumber = %.2lf\n", first);

printf("After swapping, secondNumber = %.2lf", second);

return 0;

}

sizeof operator in C

Sizeof is a much used operator in the C or C++. It is a compile time unary operator which can be used to compute the size of its operand. When sizeof() is used with the data types such as int, float, char… etc it simply returns the amount of memory is allocated to that data types.

#include<stdio.h>

int main()

{

printf("%d\n", sizeof(char));

printf("%d\n", sizeof(int));

printf("%d\n", sizeof(float));

printf("%d", sizeof(double));

return 0;

}

Comma in C Comma as a separator:
Comma acts as a separator when used with function calls and definitions, function like macros, variable declarations, enum declarations, and similar constructs.

/* comma as a separator */

int a = 1, b = 2;

void fun(x, y);

 

1. Write a program that will generate and print the first n Fibonacci number-

#include<stdio.h>

int main()

{

int n, first = 0, second = 1, next, c;

printf("Enter the number of terms\n");

scanf("%d", &n);

printf("First %d terms of Fibonacci series are:\n",n);

for (c = 0; c < n;c++)

{

if (c <= 1) next = c;

else

{

next = first + second;

first = second;

second = next;

}

printf("%d\n",next);

}

return 0;

}


2. Prime Numbers Between Two Integers

#include<stdio.h>

Int checkPrimeNumber(int n);

int main()

{

Int n1, n2, i, flag;

printf("Enter two positive integers: ");

scanf("%d %d", &n1, &n2);

printf("Prime numbers between %d and %d are: ", n1, n2);

for (i = n1 + 1; i < n2; ++i)

{

flag = checkPrimeNumber(i);

if

(flag == 1)

printf("%d ", i);

}

return 0;

}

Int checkPrimeNumber(int n)

{

int j, flag = 1;

for (j = 2; j <= n / 2; ++j)

{

if

(n % j == 0)

{

flag = 0;

break;

}

}

return flag;

}




Share with others

0 Comments

Publish comments by following these Rules.??

অর্ডিনারি আইটি কী?