Module - 1 - Q&a
Module - 1 - Q&a
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.
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.
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.
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.
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);
}
Step 1 : Start
Step 7 : Stop
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);
}
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();
}
What is a program?
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.
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.
i. Machine 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.
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.
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.
Sept 3 : End