Factorial of a Number
### Algorithm ###
1. Start.
2. Input the number `num`.
3. Initialize `factorial = 1` and a counter `i = 1`.
4. While `i <= num`:
- Multiply `factorial` by `i`.
- Increment `i` by 1.
5. Output the result (`factorial`).
6. Stop.
### Flowchart ###
(Visual flowcharts can be drawn externally as images.)
### Program ###
#include <stdio.h>
int main() {
int num, i, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 1; i <= num; i++) {
factorial *= i; // Multiply each counter value
}
printf("Factorial of %d is %d\n", num, factorial);
return 0;
### Program Explanation ###
1. **Input the Number**: User inputs a number `num`.
2. **Initialize Variables**: `factorial` is set to 1, and a counter `i` starts at 1.
3. **Loop Through Numbers**: Multiply `factorial` by `i` for every iteration until `i > num`.
4. **Output the Result**: Print the value of `factorial` after the loop ends.
Count Vowels and Consonants
### Algorithm ###
1. Start.
2. Input the string.
3. Initialize `vowels = 0` and `consonants = 0`.
4. Set a pointer to the start of the string.
5. While the pointer does not reach the null character:
- Convert the character to lowercase.
- Check if it's a letter:
- If it's a vowel (`a, e, i, o, u`), increment `vowels`.
- Otherwise, increment `consonants`.
- Move the pointer to the next character.
6. Output the counts of vowels and consonants.
7. Stop.
### Flowchart ###
(Visual flowcharts can be drawn externally as images.)
### Program ###
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100], *ptr;
int vowels = 0, consonants = 0;
printf("Enter a string: ");
gets(str);
ptr = str;
while (*ptr != '\0') {
char ch = tolower(*ptr); // Convert to lowercase for comparison
if (isalpha(ch)) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else {
consonants++;
ptr++;
printf("Number of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
return 0;
### Program Explanation ###
1. Input a string from the user.
2. Initialize variables `vowels` and `consonants` to 0.
3. Use a pointer to iterate through the string.
4. For each character, determine if it's a vowel or consonant using `tolower()` and `isalpha()`.
5. Increment the respective counter and move to the next character.
6. Display the counts of vowels and consonants.
Student Structure and Average
### Algorithm ###
1. Start.
2. Define a structure `Student` with fields: name, age, total_marks.
3. Input the name, age, and marks of `student1`.
4. Input the name, age, and marks of `student2`.
5. Calculate the average of `total_marks` of both students.
6. Display information for both students and the average marks.
7. Stop.
### Flowchart ###
(Visual flowcharts can be drawn externally as images.)
### Program ###
#include <stdio.h>
struct Student {
char name[50];
int age;
float total_marks;
};
int main() {
struct Student student1, student2;
float average_marks;
// Input data for student1
printf("Enter name of student 1: ");
scanf("%s", student1.name);
printf("Enter age of student 1: ");
scanf("%d", &student1.age);
printf("Enter total marks of student 1: ");
scanf("%f", &student1.total_marks);
// Input data for student2
printf("\nEnter name of student 2: ");
scanf("%s", student2.name);
printf("Enter age of student 2: ");
scanf("%d", &student2.age);
printf("Enter total marks of student 2: ");
scanf("%f", &student2.total_marks);
// Calculate average marks
average_marks = (student1.total_marks + student2.total_marks) / 2;
// Display student information
printf("\nStudent 1: Name = %s, Age = %d, Marks = %.2f\n", student1.name, student1.age,
student1.total_marks);
printf("Student 2: Name = %s, Age = %d, Marks = %.2f\n", student2.name, student2.age,
student2.total_marks);
printf("\nAverage of total marks: %.2f\n", average_marks);
return 0;
### Program Explanation ###
1. Define a structure `Student` with fields: `name`, `age`, `total_marks`.
2. Input the details for two students.
3. Calculate the average of their marks.
4. Display each student's details and the average marks.