C Program Learning Basic Part
Computer Programming
What is Computer Programming?
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.
Types of errors in C programming
Write a C program to swap two numerical values.
#include
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;
}