Unit 1 Question Bank Answers (1) - 1-9 - Compressed
Unit 1 Question Bank Answers (1) - 1-9 - Compressed
1. What is an Algorithm?
An algorithm is a step-by-step procedure or set of rules to solve a specific problem. It consists of
a finite sequence of well-defined instructions that, when executed, produce an output and
terminate in a finite amount of time.
2. Define Flowchart
A flowchart is a graphical representation of an algorithm using symbols to depict the flow of
execution. It helps in visualizing the steps involved in solving a problem.
Compiler – Converts the entire source code into machine code at once.
Interpreter – Converts and executes code line by line.
Assembler – Converts assembly language into machine code.
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
C Program:
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}
C Program:
#include <stdio.h>
int main() {
float a, b, c, avg;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
avg = (a + b + c) / 3;
printf("Average: %.2f\n", avg);
return 0;
}
2A. Describe the steps involved in constructing a flowchart
Identify the problem and its inputs/outputs.
Determine the steps needed to solve the problem.
Use standard symbols:
Oval: Start/End
Parallelogram: Input/Output
Rectangle: Process
Diamond: Decision
Arrange steps logically.
Validate the flowchart.
#include <stdio.h>
int main() {
float length, width, area, perimeter;
printf("Enter length and width: ");
scanf("%f %f", &length, &width);
area = length * width;
perimeter = 2 * (length + width);
printf("Area: %.2f, Perimeter: %.2f\n", area, perimeter);
return 0;
}
C Program:
#include <stdio.h>
int main() {
float P, R, T, SI;
printf("Enter Principal, Rate, Time: ");
scanf("%f %f %f", &P, &R, &T);
SI = (P * R * T) / 100;
printf("Simple Interest: %.2f\n", SI);
return 0;
}
C Prog
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
printf("%d is greater\n", a);
else
printf("%d is greater\n", b);
return 0;
}
C Program:
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, s, area;
printf("Enter three sides of the triangle: ");
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("Area of Triangle: %.2f\n", area);
return 0;
}
C Program:
#include <stdio.h>
int main() {
float F, C;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &F);
C = (F - 32) * 5 / 9;
printf("Temperature in Celsius: %.2f\n", C);
return 0;
}
7B. Draw a flowchart to check whether the given year is a leap year or not
Start
Input year
If (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0), print "Leap Year"
Else print "Not a Leap Year"
Stop
C Program:
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("%d is a Leap Year\n", year);
else
printf("%d is Not a Leap Year\n", year);
return 0;
}
C Program:
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Else print c
Stop
C Program:
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
printf("%d is the maximum\n", a);
else if (b > c)
printf("%d is the maximum\n", b);
else
printf("%d is the maximum\n", c);
return 0;
}
C Program:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0)
printf("%d is Even\n", n);
else
printf("%d is Odd\n", n);
return 0;
}
10A. Discuss various notations used to measure algorithm efficiency
Big-O (O): Worst-case scenario (e.g., O(n) for linear search).
Big-Theta (Θ): Average-case scenario.
Big-Omega (Ω): Best-case scenario.
Space Complexity: Memory used.
Time Complexity: Execution time.
C Program:
#include <stdio.h>
int main() {
int base, exp, result = 1;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exp);
for (int i = 0; i < exp; i++)
result *= base;
printf("Result: %d\n", result);
return 0;
}