Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: Basic of programming
Description: This is the programming class notes.very very help full if you learn.

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


Unit 3
Conditional Program Execution / Decision Making
3 Introduction
We have seen that a C program is a set of statements which are normally executed sequentially
in the order in which they appear
...
This involves a kind of
decision making to see whether a particular condition has occurred or not and then direct the
computer to execute certain statements accordingly
...
1 IF statement
1
...
3 Nesting of IF-ELSE statements
1
...
5 SWITCH statement
1
...
7 Conditional operator/ Ternary operator (? :)
These statements are popularly known as decision-making statements
...
1 IF Statement:
Theif statement is a powerful decision making statement and is used to control the flow of
execution of statements
...
It takes the following form:
if (test expression)
{
statement block 1;
}
statement x;
The statement block 1 may be a single statement or a group of statements
...
When the condition is true both the statement block
1 and the statement x are executed in sequence
...

3
...
If the expression is true, statement
block 1 is executed and statement block 2 is skipped
...
In any case after the statement
block 1 or 2 gets executed and finally the control will pass to statement x
...
It takes the following form:
if (test expression)
{
statement block1;
}
else
{
statement block 2;
}
statement x;

Flowchart

Program
// Program to find whether a number is even or odd
...
h>
void main()
{
int a;
printf("\n Enter the value of a : ");
scanf("%d", &a);
if(a%2==0)
printf("\n %d is even", a);
else
printf("\n %d is odd", a);
return 0;
}
Output
Enter the value of a
20
20 is even
Enter the value of a
13
13 is odd
3
...
If the condition 2 is true the statement block 1 will be evaluated otherwise
statement block 2 will be executed and then the control is transferred to statement y
...
4 ELSE-IF Ladder:
There is another way of putting ifs together when multipath decisions are involved
...
It takes the
following general form:

This construct is known as the else if ladder
...
As soon as true condition is found the statement associated with it is
executed and the control is transferred to the statement-x (skipping the rest of the ladders)
...

Flowchart

Program
// Program to classify a number as positive, negative or zero
...
h>
main()
{
int num;
printf("\n Enter any number : ");
scanf("%d", &num);
if(num==0)
printf("\n The value is equal to zero");
else if(num>0)
printf("\n The number is positive");
else
printf("\n The number is negative");
return 0;
}
3
...
However the complexity of such a program increases dramatically when
the number of alternatives increases
...
At times
it may confuse even the person who designed it
...
The switch statement tests the value of a given variable (or
expression) against the list of case values and when a match is found a block of statement
associated with that case is executed
...
6 GOTO Statement:
So far we discussed ways of controlling the flow of execution based on certain specified
conditions
...
Although it may not be essential to use the goto statement in a highly structured
language like C, there may be occasions when the use of goto might be desirable
...
A label is
any valid variable name and must be followed by colon
...
The general forms of goto and label
statements are shown below:

goto label;
…………
…………
lablel:
statement;
a) Forward jump

label:
statement;
…………
…………
goto label;
b) Backward jump

The label can be anywhere in the program either before or after the goto label; statement
...

This happens unconditionally
...


Such infinite loops should be avoided in programming
...
Enter 999 to end : ");
scanf("%d", &num);
if (num != 999)
{
if(num < 0)
goto read;
// jump to label- read
sum += num;
goto read;
// jump to label- read
}
printf("\n Sum of the numbers entered by the user is = %d", sum);
3
...
This operator is
popularly known as the conditional or ternary operator
...
If the result is nonzero, expression1 is evaluated
and is returned as the value of the conditional expression
...
For example the segment
if (x<0)
flag=0;
else
flag=1;
Can be written as
flag=(x<0)?0:1;
Program
#include ...
8
...

A program loop (or iteration) therefore consists of two segments, one is known as body of the
loop and other is known as the control statements
...

b) Exit controlled loop: test is performed at the end of the loop
...

Iterative statements are used to repeat the execution of a list of statements, depending on the
value of an integer expression
...

3
...
10 Do-while loop
3
...
Initialization of counter
2
...
Body of loop
4
...
9 While Loop
It is entry controlled loop
...
In the while loop, the condition is tested before any of the statements
in the statement block is executed
...
We must constantly update the condition of the while loop
...
h>
int main()
{
int i = 0;
while(i<=10)
{
printf(“\n %d”, i);
i = i + 1;
// condition updated
}
return 0;
}
3
...
The only difference is that in a do-while loop, the
test condition is tested at the end of the loop
...
The body of
the loop gets executed at least one time (even if the condition is false)
...
Do-while loops are widely used to print a list of options for a
menu driven program
...
h>
int main()
{
int i = 0;
do
{
printf(“\n %d”, i);
i = i + 1;
} while(i<=10);
return 0;
}
3
...
The for loop is used to repeat a task until a particular condition is
true
...

• With every iteration of the loop, the value of the loop variable is updated and the condition is
checked
...

• Updating the loop variable may include incrementing the loop variable, decrementing the loop
variable or setting it to some other value like, i +=2, where i is the loop variable
...

Program
#include ...
12 Break statement
The break statement is used to terminate the execution of the nearest enclosing loop in which it
appears
...
Its syntax is quite simple, just type
keyword break followed with a semi-colon
...


Program
int i;
for(i=1; i<= 5; i++)
{
if (i==3)
break;
printf(“\t %d”, i);
}
Output
1
2
3
...
When the compiler encounters a
continue statement then the rest of the statements in the loop are skipped and the control is
unconditionally transferred to the loop-continuation portion of the nearest enclosing loop
...

continue;
If placed within a for loop, the continue statement causes a branch to the code that updates the
loop variable
...
14 Introduction
C enables its programmers to break up a program into segments commonly known as functions,
each of which can be written more or less independently of the others
...
Therefore, the program code of one function
is completely insulated from that of other functions
...

In the fig, main() calls another function, func1() to perform a well-defined task
...
When the compiler
encounters a function call, instead of executing the next statement in the calling function, the
control jumps to the statements that are a part of the called function
...


Any function can call any other function
...
15 Why do we need functions?
• Dividing the program into separate well defined functions facilitates each function to be
written and tested separately
...

• Understanding, coding and testing multiple separate functions are far easier than doing the
same for one huge function
...

All the libraries in C contain a set of functions that the programmers are free to use in their
programs
...
This speeds up program development
...
16 Terminology of functions
• A function, f that uses another function g, is known as the calling function and g is known as
the called function
...

• The calling function may or may not pass parameters to the called function
...

• Main () is the function that is called by the operating system and therefore, it is supposed to
return the result of its processing to the operating system
...
17 Function declaration
• Function declaration is a declaration statement that identifies a function with its name, a list
of arguments that it accepts and the type of data it returns
...
);
• No function can be declared within the body of another function
...
18 Function definition
• Function definition consists of a function header that identifies the function, followed by the
body of the function containing the executable code for that function
• When a function defined, space is allocated for that function in the memory
...
)
{
…………
...

return( variable);
}


The no
...


3
...

• When a function is invoked the compiler jumps to the called function to execute the
statements that are a part of that function
...

• Function call statement has the following syntax
...
h>
int sum(int a, int b); // FUNCTION DECLARATION
int main()
{
int num1, num2, total = 0;
printf(“\n Enter the first number : “);
scanf(“%d”, &num1);
printf(“\n Enter the second number : “);
scanf(“%d”, &num2);
total = sum(num1, num2);
// FUNCTION CALL
printf(“\n Total = %d”, total);
return 0;
}
// FUNCTION DEFNITION
int sum ( int a, int b)
// FUNCTION HEADER
{
// FUNCTION BODY
return (a + b);
}
3
...
When the return statement is encountered, the program execution resumes in
the calling function at the point immediately following the function call
...

• For functions that has no return statement, the control automatically returns to the calling
function after the last statement of the called function is executed
...
21 Passing parameters to the function
• There are two ways in which arguments or parameters can be passed to the called function
...

• Call by reference in which address of the variables are passed by the calling function to the
called function
...
21
...
Therefore, the called function uses a copy of the actual arguments to
perform its intended task
...
In the calling function no change will be
made to the value of the variables
...
h>
void add( int n);
int main()
{
int num = 2;
printf("\n The value of num before calling the function = %d", num);
add(num);
printf("\n The value of num after calling the function = %d", num);
return 0;
}
void add(int n)
{
n = n + 10;
printf("\n The value of num in the called function = %d", n);
}
The output of this program is:
The value of num before calling the function = 2
The value of num in the called function = 20
The value of num after calling the function = 2
3
...
2 Call by reference
In call by reference, we declare the function parameters as references rather than normal
variables
...
This way, changes made to that parameter in the called
function body will then be reflected in its value in the calling program
...
h>
void add( int &n);
int main()
{
int num = 2;

printf("\n The value of num before calling the function = %d", num);
add(&num);
printf("\n The value of num after calling the function = %d", num);
return 0;
}
void add( int *n)
{
*n = *n + 10;
printf("\n The value of num in the called function = %d", n);
}
The output of this program is:
The value of num before calling the function = 2
The value of num in the called function = 20
The value of num after calling the function = 20
3
...

Every recursive solution has two major cases, they are base case, in which the problem is simple
enough to be solved directly without making any further calls to the same function recursive
case, in which first the problem at hand is divided into simpler sub parts
...
Third,
the result is obtained by combining the solutions of simpler sub-parts
...
h>
int Fact(int)
{
if(n==1)
retrun 1;
return (n * Fact(n-1));
}
main()
{
int num;

scanf(“%d”, &num);
printf(“\n Factorial of %d = %d”, num, Fact(num));
return 0;
}
The Fibonacci series can be given as:
0 1 1 2 3 5
8
13
21 34
55……
That is, the third term of the series is the sum of the first and second terms
...
Now we will design a
recursive solution to find the nth term of the Fibonacci series
...
23 Types of recursion
Any recursive function can be characterized based on:
 whether the function calls itself directly or indirectly (direct or indirect recursion)
...

 the structure of the calling pattern (linear or tree-recursive)
...
23
...
For example, consider the
function given below
...
23
...
Look at the functions given below
...

int Func1(int n)
{
if(n==0)
return n;
return Func2(n);
}

int Func2(int x)
{
return Func1(x1);
}

3
...
3 Tail Recursion
A recursive function is said to be tail recursive if no operations are pending to be performed
when the recursive function returns to its caller
...
Tail recursive functions are
highly desirable because they are much more efficient to use as in their case, the amount of
information that has to be stored on the system stack is independent of the number of recursive
calls
...
23
...

• In simple words, a recursive function is said to be linearly recursive when no pending
operation involves another recursive call to the function
...

• On the contrary, a recursive function is said to be tree recursive (or non-linearly recursive) if
the pending operation makes another recursive call to the function
...


3
...

• Code is clearer and easier to use










Recursion represents like the original formula to solve a problem
...

Recursion is implemented using system stack
...

Aborting a recursive process in midstream is slow and sometimes nasty
...

It is difficult to find bugs, particularly when using global variables

Some Important Programs
Program: To calculate factorial of a number
...
h>
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
Program: To calculate sum of digits of a number
...
h>
int main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum
= sum + remainder;
t
= t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}

Program: To reverse a number
...
h>
int main()
{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d", &n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n
= n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;
}
Program: To check whether a given number is armstrong or not
...
h>
int main()
{
int number, sum = 0, temp, remainder;
printf("Enter an integer\n");
scanf("%d",&number);
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
printf("Entered number is an armstrong number
...
\n");
return 0;
}
Program: To generate Fibonacci Series
...
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;
}
Program: To calculate LCM and GCD
...
h>
int main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
return 0;
}

Question

2016-17
1
...
Distinguish between int main() and void main()?
3
...

4
...
Actual and formal arguments
b
...
Write a program to print all prime numbers between 1 to 300
6
...

7
...
Write a program to check whether a given number is Armstrong or not Like 153=13+53+33
9
...
Write a C function to calculate
sum of digits of a 5 digit number:
a
...
Using recursion
2015-16
1
...

2
...
What are the principles of recursion? Explain in detail
...
Write a program in ‘C’ that will read a positive number from the keyboard and print it in
reverse order
...
g
...
What do you mean by parameter passing mechanism?
6
...
What are different types of functions? Write a program in C to short list of names of students
in an ascending order
...
Write difference between call by value and call by reference with suitable example
...
Give the loop statement to print the following sequence of integer
-6 -4 -2 0 2 4 6
2
...
What is the role of SWITCH statement in C programming language? Explain with example
...
Distinguish between actual and formal arguments
...
Describe call by value and call by reference with example
...
Write a program in C language to generate the Fibonacci series
...
Describe about the types of looping statements in ‘C’ with necessary syntax
...
Write a C program to find the multiplication of two matrices
...
What are the types of function? Write a C program to find the factorial of a given number
using recursion
...
What is the difference between break and continue? Describe the structure of switch-case
with neat example
...
What are the different types of functions? Write a program in C to short list of names of
students in an ascending order
...
Write a program to print following pattern
1
23
456
7 8 9 10
3
...
Write a program in C to generate Fibnocii series (0 1 1 2 3 5 8
13…) using recursive function
...
Write a C program to find the sum of individual digits in a five digit number
...
Write the difference between call by value and call by reference with suitable example
...
Write a program to find greatest among three numbers using conditional operator
...
Differentiate between nested-if and switch statements in ‘C’ with example
...
Write a program in ‘C’ to sort list of 10 integers in an ascending order
...
Write a program to multiply the two matrices of MxN
...
Write the purpose and syntax of at least two iterative statements in C
...
WAP to generate fabonacci series up to the last term less than 100
...

3
...

4
...
Write the functions sum_matrix() and multiply_matix() to add
and multiply two matices
...
Differentiate between call by value and call by reference
...
Write a program to calculate GCD
...
WAP to calculate the multiplication of all the digits of a 5 digit number
...
Write a program which stores the marks of N students in integer array
...

9
...

10
...


11
...

12
...
Give its advantage
...

13
...
If the sum of factor is
equal to number itself then it is a perfect number
...
g Factor of 6 are 1, 2, 3 whose sum
1+2+3=6
...
Write a program to find the prime numbers between the given range
...
Write a program to generate following pattern
...
Write a program o read five digit number if it is even then add the digits otherwise multiply
them
...
Write a program to generate the given series upto less than 200
...

4
...
Use for and continue statements
...
Write a program to check whether a number is even or odd without else option
Title: Basic of programming
Description: This is the programming class notes.very very help full if you learn.