Assignment Algorithms
Assignment Algorithms
Algorithm
Step 1: START
Step 2: Declare variables a, b, sum
Step 3: INPUT a and b from user
Step 4: Compute sum = a + b
Step 5: PRINT sum
Step 6: STOP
Flowchart
──────────────────────────────────────────────────
╔═══════════════════╗
║ START ║
╚═══════════════════╝
|
V
╱─────────────────────╲
╱ Input a, b ╲
╱─────────────────────────╲
|
V
┌─────────────────────────┐
│ sum = a + b │
└─────────────────────────┘
|
V
╱─────────────────────╲
╱ Print sum ╲
╱─────────────────────────╲
|
V
╔═══════════════════╗
║ STOP ║
╚═══════════════════╝
──────────────────────────────────────────────────
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;
}
Q2. Larger of Two Numbers
Algorithm
Step 1: START
Step 2: Declare variables a, b
Step 3: INPUT a and b from user
Step 4: IF a > b THEN print a is larger
Step 5: ELSE IF b > a THEN print b is larger
Step 6: ELSE print both are equal
Step 7: STOP
Flowchart
──────────────────────────────────────────────────
╔═══════════════════╗
║ START ║
╚═══════════════════╝
|
V
╱─────────────────────╲
╱ Input a, b ╲
╱─────────────────────────╲
|
V
/───────────────\
< a > b ? >
\───────────────/
|
V
┌─────────────────────────┐
│ (YES) Print 'a is larger' | (NO) b > a ?│
└─────────────────────────┘
|
V
┌─────────────────────────┐
│ (YES) Print 'b is larger' | (NO) Print 'Equal'│
└─────────────────────────┘
|
V
╔═══════════════════╗
║ STOP ║
╚═══════════════════╝
──────────────────────────────────────────────────
C Program
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b) printf("%d is larger\n", a);
else if (b > a) printf("%d is larger\n", b);
else printf("Both are equal\n");
return 0;
}
Q3. Even or Odd
Algorithm
Step 1: START
Step 2: Declare variable n
Step 3: INPUT n from user
Step 4: IF n % 2 == 0 THEN print 'Even'
Step 5: ELSE print 'Odd'
Step 6: STOP
Flowchart
──────────────────────────────────────────────────
╔═══════════════════╗
║ START ║
╚═══════════════════╝
|
V
╱─────────────────────╲
╱ Input n ╲
╱─────────────────────────╲
|
V
/───────────────\
< n % 2 == 0 ? >
\───────────────/
|
V
┌─────────────────────────┐
│ (YES) Print 'Even' | (NO) Print 'Odd'│
└─────────────────────────┘
|
V
╔═══════════════════╗
║ STOP ║
╚═══════════════════╝
──────────────────────────────────────────────────
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;
}
Q4. Factorial of a Number
Algorithm
Step 1: START
Step 2: Declare variables n, i, fact = 1
Step 3: INPUT n from user
Step 4: Initialize i = 1
Step 5: WHILE i <= n: fact = fact * i, increment i
Step 6: PRINT fact
Step 7: STOP
Flowchart
──────────────────────────────────────────────────
╔═══════════════════╗
║ START ║
╚═══════════════════╝
|
V
╱─────────────────────╲
╱ Input n ╲
╱─────────────────────────╲
|
V
┌─────────────────────────┐
│ fact = 1, i = 1 │
└─────────────────────────┘
|
V
/───────────────\
< i <= n ? >
\───────────────/
|
V
┌─────────────────────────┐
│ (YES) fact = fact * i, i = i + 1 => loop back│
└─────────────────────────┘
|
V
╱─────────────────────╲
╱ (NO) Print fact ╲
╱─────────────────────────╲
|
V
╔═══════════════════╗
║ STOP ║
╚═══════════════════╝
──────────────────────────────────────────────────
C Program
#include <stdio.h>
int main() {
int n, i;
long long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fact *= i;
printf("%d! = %lld\n", n, fact);
return 0;
}
Q5. Simple Interest
Algorithm
Step 1: START
Step 2: Declare variables P, R, T, SI
Step 3: INPUT P (Principal), R (Rate), T (Time) from user
Step 4: Compute SI = (P * R * T) / 100
Step 5: PRINT SI
Step 6: STOP
Flowchart
──────────────────────────────────────────────────
╔═══════════════════╗
║ START ║
╚═══════════════════╝
|
V
╱─────────────────────╲
╱ Input P, R, T ╲
╱─────────────────────────╲
|
V
┌─────────────────────────┐
│ SI = (P * R * T) / 100 │
└─────────────────────────┘
|
V
╱─────────────────────╲
╱ Print SI ╲
╱─────────────────────────╲
|
V
╔═══════════════════╗
║ STOP ║
╚═══════════════════╝
──────────────────────────────────────────────────
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;
}
Q6. Positive, Negative, or Zero
Algorithm
Step 1: START
Step 2: Declare variable n
Step 3: INPUT n from user
Step 4: IF n > 0 THEN print 'Positive'
Step 5: ELSE IF n < 0 THEN print 'Negative'
Step 6: ELSE print 'Zero'
Step 7: STOP
Flowchart
──────────────────────────────────────────────────
╔═══════════════════╗
║ START ║
╚═══════════════════╝
|
V
╱─────────────────────╲
╱ Input n ╲
╱─────────────────────────╲
|
V
/───────────────\
< n > 0 ? >
\───────────────/
|
V
┌─────────────────────────┐
│ (YES) Print 'Positive' | (NO) n < 0 ?│
└─────────────────────────┘
|
V
┌─────────────────────────┐
│ (YES) Print 'Negative' | (NO) Print 'Zero'│
└─────────────────────────┘
|
V
╔═══════════════════╗
║ STOP ║
╚═══════════════════╝
──────────────────────────────────────────────────
C Program
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n > 0) printf("Positive\n");
else if (n < 0) printf("Negative\n");
else printf("Zero\n");
return 0;
}
Q7. Sum of First N Natural Numbers
Algorithm
Step 1: START
Step 2: Declare variables n, i, sum = 0
Step 3: INPUT n from user
Step 4: Initialize i = 1
Step 5: WHILE i <= n: sum = sum + i, increment i
Step 6: PRINT sum
Step 7: STOP
Flowchart
──────────────────────────────────────────────────
╔═══════════════════╗
║ START ║
╚═══════════════════╝
|
V
╱─────────────────────╲
╱ Input n ╲
╱─────────────────────────╲
|
V
┌─────────────────────────┐
│ sum = 0, i = 1 │
└─────────────────────────┘
|
V
/───────────────\
< i <= n ? >
\───────────────/
|
V
┌─────────────────────────┐
│ (YES) sum = sum + i, i = i + 1 => loop back│
└─────────────────────────┘
|
V
╱─────────────────────╲
╱ (NO) Print sum ╲
╱─────────────────────────╲
|
V
╔═══════════════════╗
║ STOP ║
╚═══════════════════╝
──────────────────────────────────────────────────
C Program
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
sum += i;
printf("Sum = %d\n", sum);
return 0;
}
Q8. Celsius to Fahrenheit
Algorithm
Step 1: START
Step 2: Declare variables C, F
Step 3: INPUT C (Celsius temperature) from user
Step 4: Compute F = (C * 9 / 5) + 32
Step 5: PRINT F
Step 6: STOP
Flowchart
──────────────────────────────────────────────────
╔═══════════════════╗
║ START ║
╚═══════════════════╝
|
V
╱─────────────────────╲
╱ Input C ╲
╱─────────────────────────╲
|
V
┌─────────────────────────┐
│ F = (C * 9/5) + 32 │
└─────────────────────────┘
|
V
╱─────────────────────╲
╱ Print F ╲
╱─────────────────────────╲
|
V
╔═══════════════════╗
║ STOP ║
╚═══════════════════╝
──────────────────────────────────────────────────
C Program
#include <stdio.h>
int main() {
float C, F;
printf("Enter Celsius: ");
scanf("%f", &C);
F = (C * 9.0 / 5.0) + 32;
printf("Fahrenheit = %.2f\n", F);
return 0;
}
Q9. Smallest of Three Numbers
Algorithm
Step 1: START
Step 2: Declare variables a, b, c
Step 3: INPUT a, b, c from user
Step 4: IF a <= b AND a <= c THEN print a is smallest
Step 5: ELSE IF b <= a AND b <= c THEN print b is smallest
Step 6: ELSE print c is smallest
Step 7: STOP
Flowchart
──────────────────────────────────────────────────
╔═══════════════════╗
║ START ║
╚═══════════════════╝
|
V
╱─────────────────────╲
╱ Input a, b, c ╲
╱─────────────────────────╲
|
V
/───────────────\
< a<=b AND a<=c ? >
\───────────────/
|
V
┌─────────────────────────┐
│ (YES) Print 'a is smallest' | (NO) b<=a AND b<=c ?│
└─────────────────────────┘
|
V
┌─────────────────────────┐
│ (YES) Print 'b is smallest' | (NO) Print 'c is smallest'│
└─────────────────────────┘
|
V
╔═══════════════════╗
║ 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 smallest\n", a);
else if (b <= a && b <= c)
printf("%d is smallest\n", b);
else
printf("%d is smallest\n", c);
return 0;
}
Q10. Leap Year Check
Algorithm
Step 1: START
Step 2: Declare variable year
Step 3: INPUT year from user
Step 4: IF year % 400 == 0 THEN it is a leap year
Step 5: ELSE IF year % 100 == 0 THEN it is NOT a leap year
Step 6: ELSE IF year % 4 == 0 THEN it is a leap year
Step 7: ELSE it is NOT a leap year
Step 8: PRINT result
Step 9: STOP
Flowchart
──────────────────────────────────────────────────
╔═══════════════════╗
║ START ║
╚═══════════════════╝
|
V
╱─────────────────────╲
╱ Input year ╲
╱─────────────────────────╲
|
V
/───────────────\
< year % 400 == 0 ? >
\───────────────/
|
V
┌─────────────────────────┐
│ (YES) Leap Year | (NO) year % 100 == 0 ?│
└─────────────────────────┘
|
V
┌─────────────────────────┐
│ (YES) NOT Leap | (NO) year % 4 == 0 ?│
└─────────────────────────┘
|
V
┌─────────────────────────┐
│ (YES) Leap Year | (NO) NOT Leap Year│
└─────────────────────────┘
|
V
╱─────────────────────╲
╱ Print Result ╲
╱─────────────────────────╲
|
V
╔═══════════════════╗
║ STOP ║
╚═══════════════════╝
──────────────────────────────────────────────────
C Program
#include <stdio.h>
int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
if (year % 400 == 0)
printf("Leap Year\n");
else if (year % 100 == 0)
printf("Not a Leap Year\n");
else if (year % 4 == 0)
printf("Leap Year\n");
else
printf("Not a Leap Year\n");
return 0;
}