[go: up one dir, main page]

0% found this document useful (0 votes)
18 views2 pages

Chapter 11 Example Programs

The document presents solved programming exercises in C, including examples for calculating the square root of a positive number, finding the largest of three numbers, and determining if a character is a vowel. Each example includes code snippets and explanations of the logic used. The exercises demonstrate basic input handling and conditional statements in C programming.

Uploaded by

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

Chapter 11 Example Programs

The document presents solved programming exercises in C, including examples for calculating the square root of a positive number, finding the largest of three numbers, and determining if a character is a vowel. Each example includes code snippets and explanations of the logic used. The exercises demonstrate basic input handling and conditional statements in C programming.

Uploaded by

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

Chapter 11 Exercise Solved Programs

Example 1: Calculate Square root of a positive number


#include <stdio.h>
#include <math.h>

int main() {
int number;

// Input positive number


printf("Enter a positive number: ");
scanf("%d", &number);

// Check if the number is positive


if (number < 0) {
printf("Please enter a positive number.\n");
return 1; // Exit with an error code
}

// Calculate and display the square root


float squareRoot = sqrt(number);
printf("Square root of %d: %.2f \n", number, squareRoot);

Example 2: Accepts three numbers and display largest number.

#include <stdio.h>
int main() {
// Input three numbers
float num1, num2, num3;
printf("Enter three numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);

// Check and display the largest number


if (num1 >= num2 && num1 >= num3) {
printf("The largest number is %.2f\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The largest number is %.2f\n", num2);
} else {
printf("The largest number is %.2f\n", num3);
}

}
Example 3: Accepts a number from user and determine whether positive, negative or zero.

#include <stdio.h>
int main() {
// Input three numbers
float num1, num2, num3;
printf("Enter three numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);

// Check and display the largest number


if (num1 >= num2 && num1 >= num3) {
printf("The largest number is %.2f\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The largest number is %.2f\n", num2);
} else {
printf("The largest number is %.2f\n", num3);
}

Example 4: Accepts a character from user and determine whether it is vowel or not.
int main() {
// Input a character
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
// Check and display whether the character is a vowel or not
switch (ch) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("The character '%c' is a vowel.\n", ch);
break;
default:
printf("The character '%c' is not a vowel.\n", ch);
}
}

You might also like