0% found this document useful (0 votes)
37 views13 pages

Module - 1 - Q&a

Uploaded by

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

Module - 1 - Q&a

Uploaded by

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

1. a. Define an algorithm?

Algorithms
A set of finite rules or instructions to be followed in calculations or other problem-solving
operations

Or
” An algorithm is defined as a finite set of steps that provide a chain of actions for solving a problem.
Algorithm is a step-by-step method of solving a problem.”.

1. Algorithms are necessary for solving complex problems efficiently and effectively.
2. They help to automate processes and make them more reliable, faster, and easier to
perform.
3. Algorithms also enable computers to perform tasks that would be difficult or
impossible for humans to do manually.
4. They are used in various fields such as mathematics, computer science, engineering,
finance, and many others to optimize processes, analyze data, make predictions, and
provide solutions to problems.

b. Explain the various characteristics of an algorithm with advantages and


disadvantages.

Characteristics of an Algorithm:
1) Finiteness: - An algorithm terminates after a finite numbers of steps.
2) Definiteness: - Each step in algorithm is unique. This means that the action
specified by the step cannot be interpreted in multiple ways & can be performed
without any confusion.
3) Input: - An algorithm accepts zero or more inputs
4) Output: - An algorithm should produce at least one output.
5) Effectiveness: - It consists of basic instructions that are realizable. This means
that the instructions can be performed by using the given inputs in a finite
amount of time.
Qualities of a good algorithm: The following are the primary factors that are often used
to judge the quality of an algorithm.
 Time: The lesser is the time required to execute a program better is the algorithm
 Memory: Computer takes some amount of memory to storage to execute a
program. The lesser is the memory required the better is the algorithm
 Accuracy: Algorithms which provide the accurate results to a given problem
should be chosen. Sequence: The procedure of an algorithm must form in a
sequence
o Generality: The designed algorithm must solve the problem for a different
range of input data.

2. Design an algorithm to generate and print the first ‘n’ terms of Fibonacci sequence
Where n >= 1.

Step 1: Start
Step 2: Declare i, n, f, f1, f2 as integer variables
Step 3: Read the ‘n’ value
Step 4: Initialize ‘f’ as 0 and ‘f1’ as 1
Step 5: Display ‘f’ and ‘f1’ values
Step 6: Check for(i=1;i<=n-2;i++)
Step 6.1: Find ‘f2’ as f+f1
Step 6.2: Display the value of ‘f2’
Step6.3: Assign ‘f’ as ‘f1’
Step 6.4: Assign ‘f1’ as ‘f2’
Step7: Stop

#include<stdio.h>
void main()
{
int i,n,f,f1,f2;
clrscr();
printf("Enter Number of Fibonacci Values Needed : ");
scanf("%d",&n);
f=0; f1=1;
printf(“%d\n%d\n”,f,f1);
for(i=1;i<=n-2;i++)
{
f2=f+f1;
printf(“%d\n”,f2);
f=f1;
f1=f2;
}
getch();
}

3. Design an algorithm that accepts positive integer and reversing the digits of an
integer with individual digits sum.

Step 1: Start
Step 2: Declare n, rev, rem as integer variables
Step 3: Initialize rev as zero
Step 4: Read ‘n’ value
Step 5: Check while (n!=0)
Step 5.1: rem=n%10
Step 5.2: rev=rev*10+rem
Step 5.3: n=n/10
Step 6: Display ‘rev’ value as output
Step 7: Stop
}
Program:
/* Reverse the digits of an given integer */
#include <stdio.h>
#include <conio.h>
int main()
{
int n, rev = 0, rem;
clrscr();
printf("\nEnter an integer: ");
scanf("%d", &n);
while (n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n=n/10;
}
printf("\n The Reversed number of %d = %d",n,rev);
return(0);
}

4.a. Design an algorithm for finding big value among three numbers.

Step 1:Star.
Step 2:Read three numbers A,B & C.
Step 3:If A>B,then go to step 6.
Step 4:If B>C,then print B & go to step 8.
Step 5:print C is greatest & go to step 8.
Step 6:If A>C,then print A is greatest & go to step 8.
Step 7:Print C is greatest.
Step 8:end.

b. Given a number ‘n’, design an algorithm to compute ‘n’ factorial. Where n >= 0

Step1: Start
Step 2: Declare ‘n’,’i’ and ‘f’ as integer variables
Step 3: Assign ‘f’ as 1
Step 4: Read the ‘n’ value
Step 5: Check for(i=1;i<=n,i++) Step5.1: Assign ‘f’ as f*i
Step 6: Display the factorial value ‘f’
Step 7: Stop
Program:
/* To find factorial of the given number */
#include <stdio.h>
#include <conio.h> int main()
{
int n,i,f=1; clrscr();
printf("\n Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n The factorial of %d is %d",n,f);
return(0);
}

5. What is Flowchart? Explain various symbols used in the flowchart with an example.

A flowchart is a type of diagram that represents a workflow or process. A flowchart can


also be defined as a diagrammatic representation of an algorithm, a step-by-step
approach to solving a task.
1. Terminal: The terminal (oval)symbol as the name implies is used
to indicate the beginning(start), ending(stop) and pause in the
logic flow. It is the first and last symbol in the programming
logic.
2.Input/output:The input/output (parallelogram) symbol is used to
denote any function of an input/output device in the program. All
input/output instructions in the program are indicated with this symbol.
3. Processing: A processing(rectangle) symbol is used in a flowchart
to represent arithmetic and data movement instructions. Thus all
arithmetic processes of addition, subtraction, multiplication etc
4. Flow lines: Flow lines with arrow heads are used to indicate the flow
of operations i.e. the exact sequence in which the instructions are to
be executed. The normal flow of flowchart is from top to bottom and
left to right.
5. Decision: The decision (diamond) symbol is used in a flowchart to
indicate a point at which a decision has to be made and a branch to
one of two or more alternate points is possible.
6. Connectors: If a flowchart becomes very long the flow chart starts criss
crossing at many places that causes confusion and reduces
understandability of the flowchart. Moreover there are instances when a
flowchart becomes too long to fit in a single page and the use of flow
lines become impossible. A connector symbol is represented by a circle. A
pair of identically labeled connector symbols is commonly used to indicate
a continued flow when the use of a line is confusing. So two connectors
with identical labels can be used.
7. Predefined process: The predefined process(double sided rectangle)
symbol is used in flowcharts to indicate that modules or subroutines are
specified elsewhere.
8. Annotation: The annotation (bracket with broken line) symbol is
used in flowcharts to indicate the descriptive comments or
explanation of the instruction.

6. Explain about Programming and discuss how to develop a program?

Programming refers to a technological process for telling a computer which tasks to perform
in order to solve problems. You can think of programming as a collaboration between
humans and computers, in which humans create instructions for a computer to follow
(code) in a language computer can understand.

How to develop a program:


When we want to develop a program by using any programming language, we have to
follow a sequence of steps. These steps are called phases in program development.
The program development life cycle is a set of steps or phases which are used to develop a
program in any programming language.
Phases of program development
Program development life cycle contains 6 phases, which are as follows −
• Problem Definition.
• Problem Analysis.
• Algorithm Development.
• Coding & Documentation.
• Testing & Debugging.
• Maintenance.
Problem Definition: In this phase, we need to understand what the problem statement is,
what our requirement is and what the output of the problem solution is.
Problem Analysis: Here, we determine the requirements like variables, functions, etc. to
solve the problem. It means that we gather the required resources to solve the problem,
which are defined in the problem definition phase.
Algorithm Development: Here, we develop a step-by-step procedure that is used to solve
the problem by using the specification given in the previous phase.
Coding & Documentation: Here, we use a programming language to write or implement the
actual programming instructions for the steps defined in the previous phase. We write the
program to solve the given problem by using the programming languages like C, C++, Java,
etc.
Testing & Debugging: Here we try to test the program whether it is solving the problem for
various input data values or not. We also test if it is providing the desired output or not.
Maintenance: In this phase, we make the enhancements. If the user gets any problem or
wants any enhancement, then we need to repeat all these phases from the starting, so that
the encountered problem is solved or enhancement is added.
These six phases are depicted in the diagram given below –

7. Discuss about Structured Programming and differentiate between compiler and


Interpreter.

Structured Programming Concept: In structured programming approach, the problem is


viewed as a sequence of things to be done such as reading, calculating and printing. The
number of functions or procedures is written to perform these tasks. So it is also called
POP language. The primary focus is on constructing procedures, and a little attention is
given to the data.
The following are the characteristics of this approach:
1. Emphasis (Importance) is on preparing functions (algorithms).
2. Large programs are divided in to small programs known as functions.
3. Most of the functions share global data.
4. Data moves openly around the system from one function to another function.
5. Functions transform the data from one form to another.
6. It follows top-down approach in program design.
The high level languages such as COBOL, FORTRAN, PASCAL and C use the concepts of
POP.
Differences between compiler and interpreter

SI. Compiler Interpreter


No

1 Performs the translation of a Performs statement by statement


program as a whole. translation.

2 Execution is faster. Execution is slower.

3 Requires more memory as linking is Memory usage is efficient as no


needed for the generated intermediate object code is generated.
intermediate object code.

4 Debugging is hard as the error It stops translation when the first error is
messages are generated after met. Hence, debugging is easy.
scanning the entire program only.

5 Programming languages like C, C++ Programming languages like Python, BASIC,


uses compilers. and Ruby uses interpreters.

8.A. Write a C program for counting of number of digits in a given ‘n’ value.

Step 1 : Start
Step 2 : Initialize ‘n’ as integer variable and ‘count’ as 0
Step 3 : READ ‘n’ value
Step 4 : check the condition (n!=0) Step 4.1 : update n=n/10
Step 4.2 : increment count by 1(count++)
Step 5 : PRINT ‘count’ value
Step 6 : Stop
Program:
/* counting the number of digits */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,count=0;
clrscr();
printf("Enter the number: ");
scanf("%d", &n);
while(n!=0)
{
n=n/10;
count=count+1;
}
printf("\n Number of digits = %d ", count);
return(0);
}

b. Develop an algorithm and flow chart for summation of a set of numbers.

Step 1 : Start

Step 2 : Initialize ‘sum’ as 0

Step 3 : Declare ‘n’ and ‘i’ as integer type


variables

Step 4 : Read the ‘n’ value

Step 5 : check for(i=1;i<=n;++i)

Step 5.1: sum=sum+i

Step 6 : Print ‘sum’ value

Step 7 : Stop

9. a. Design an algorithm to exchanging


the values of Two Variables by using third variable.

Step 1 : Start
Step 2 : READ num1, num2
Step 3 : temp = num1
Step 4 : num1 = num2
Step 5 : num2 = temp
Step 6 : PRINT num1, num2
Step 7 : Stop

b. Design an algorithm to exchanging the values of Two Variables without using third
variable.

Step 1 : Start
Start 2 : READ num1, num2
Start 3 : num1 = num1 + num2
Start 4 : num2 = num1 - num2
Start 5 : num1 = num1 - num2
Start 6 : PRINT num1, num2
Start 7 : Stop
Program:
/* Exchange the values of two variables */
#include<stdio.h>
#include<conio.h>
int main()
{
int first, second, temp;
clrscr();
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, firstNumber = %d\n", first);
printf("\nAfter swapping, secondNumber = %d\n", second);
return(0);
}

10. a. Design an algorithm to check given number is Armstrong or not.

1.Start
2.read number
3.set sum=0 and duplicate=number
4.reminder=number%10
5.sum=sum+(reminder*reminder*reminder)
6.number=number/10
7.repeat steps 4 to 6 until number > 0
8.if sum = duplicate
9.display number is armstrong
10. else
11. display number is not armstrong
12. stop

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,s,r,dup;
s=0;
printf("\n Enter a number please:\t");
scanf("%d",&n);
dup=n;
while(n>0)
{
r=n%10;
s=s+(r*r*r); //or you can use pow(r,3) and use the header file math.h
n=n/10;
}
if(dup=s)
printf("\n %d is an armstrong number",dup);
else
printf("\n %d is not an armstrong
number",dup);
getch();
}

a. Discuss about program, Programming and program Languages.

What is a program?

A computer program is a sequence of instructions written using a Computer Programming


Language to perform a specified task by the computer.

Computer Programming

the act of writing computer programs is called computer programming.

As we mentioned earlier, there are hundreds of programming languages, which can be used
to write computer programs and following are a few of them −
Java, C, C++, Python, PHP, Perl, Ruby
Computer Programmer

Someone who can write computer programs or in other words, someone who can do
computer programming is called a Computer Programmer.

Based on computer programming language expertise, we can name a computer


programmers as follows −

C Programmer
C++ Programmer
Java Programmer
Python Programmer
PHP Programmer
What is Language?

Language is a mode of communication that is used to share ideas, opinions with each
other. For example, if we want to teach someone, we need a language that is
understandable by both communicators.

What is a 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.

Types of programming language


1. Low-level programming language

Low-level language is machine-dependent (0s and 1s) programming language. The


processor runs low- level programs directly without the need of a compiler or interpreter, so
the programs written in low-level language can be run very fast.

Low-level language is further divided into two parts -

i. Machine Language

Machine language is a type of low-level programming language. It is also called as machine


code or object code. Machine language is easier to read because it is normally displayed in
binary or hexadecimal form (base 16) form. It does not require a translator to convert the
programs because computers directly understand the machine language programs.

ii. Assembly Language

Assembly language (ASM) is also a type of low-level programming language that is designed
for specific processors. It represents the set of instructions in a symbolic and human-
understandable form. It uses an assembler to convert the assembly language to machine
language.

2. High-level programming language

High-level programming language (HLL) is designed for developing user-friendly software


programs and websites. This programming language requires a compiler or interpreter to
translate the program into machine language (execute the program).

The main advantage of a high-level language is that it is easy to read, write, and maintain.

High-level programming language includes Python, Java, JavaScript, PHP, C#, C++, Objective
C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language.

A high-level language is further divided into three parts -

i. Procedural Oriented programming language

Procedural Oriented Programming (POP) language is derived from structured programming


and based upon the procedure call concept. It divides a program into small procedures
called routines or functions.

Procedural Oriented programming language is used by a software programmer to create a


program that can be accomplished by using a programming editor like IDE, Adobe
Dreamweaver, or Microsoft Visual Studio.

Example: C, FORTRAN, Basic, Pascal, etc.

ii. Object-Oriented Programming language

Object-Oriented Programming (OOP) language is based upon the objects. In


this programming language, programs are divided into small parts called objects. It is used
to implement real-world entities like inheritance, polymorphism, abstraction, etc in the
program to makes the program resusable, efficient, and easy-to-use.

Example: C++, Java, Python, C#, etc.

iii. Natural language

Natural language is a part of human languages such as English, Russian, German, and
Japanese. It is used by machines to understand, manipulate, and interpret human's
language. It is used by developers to perform tasks such as translation, automatic
summarization, Named Entity Recognition (NER), relationship extraction, and topic
segmentation.

The main advantage of natural language is that it helps users to ask questions in any subject
and directly respond within seconds.
3. Middle-level programming language

Middle-level programming language lies between the low-level programming language and
high-level programming language. It is also known as the intermediate programming
language and pseudo-language.

A middle-level programming language's advantages are that it supports the features of high-
level programming, it is a user-friendly language, and closely related to machine language
and human language.

Example: C, C++

11. a. Design an algorithm to find maximum number among three numbers using
nested conditional operator.

Step 1 : Start the program .


Step 2 : Declare variable a, b, c, largestValue.
Step 3 : f a > b go to step 4 Otherwise go to step 5
Step 4. If a > c SET largestValue = a Otherwise largestValue = c
Step 5 : If b > c SET largestValue = b Otherwise largestValue = c
Step 6 : End.

b. Design an algorithm to display prime numbers up to given range.

Sept 3 : End

You might also like