Module 4 respository for lab
1.### Program: Find the Minimum of Three Numbers
Aim:
To find and display the minimum of three numbers entered by the user using the conditional (ternary) operator.
Algorithm:
- Start the program.
- Declare three integer variables
n1,n2,n3and a variableminto store the minimum. - Read three numbers from the user.
- Use the ternary operator to compare the numbers:
- First, compare
n1andn2. - Then, compare the smaller of
n1andn2withn3.
- First, compare
- Store the smallest number in
min. - Print the minimum number along with the input numbers.
- End the program.
Source Code:
#include <stdio.h>
int main()
{
int n1, n2, n3, min;
scanf("%d %d %d", &n1, &n2, &n3);
min = (n1 < n2) ? ((n1 < n3) ? n1 : n3) : ((n2 < n3) ? n2 : n3);
printf("Minimum between %d, %d and %d = %d", n1, n2, n3, min);
return 0;
}output:
Result: The program successfully determines and displays the smallest of the three numbers entered by the user.
2.### Program: Temperature Classification
Aim:
To classify the weather based on the temperature entered by the user and display an appropriate message.
Algorithm:
- Start the program.
- Declare a variable
tempto store the temperature. - Read the temperature value from the user.
- Use conditional statements to check the temperature range and print the corresponding message:
- If
temp < 0→ "Freezing weather" - If
0 ≤ temp ≤ 10→ "Cold weather" - If
10 < temp < 20→ "Very Cold weather" - If
20 ≤ temp ≤ 30→ "Normal in temp" - If
30 < temp < 40→ "It's Hot" - If
temp ≥ 40→ "It's very hot"
- If
- End the program.
Source Code:
#include <stdio.h>
int main()
{
float temp;
scanf("%f", &temp);
if (temp < 0)
printf("Freezing weather.");
else if (temp >= 0 && temp <= 10)
printf("Cold weather.");
else if (temp > 10 && temp < 20)
printf("Very Cold weather.");
else if (temp >= 20 && temp <= 30)
printf("Normal in temp.");
else if (temp > 30 && temp < 40)
printf("It's Hot");
else if (temp >= 40)
printf("It's very hot.");
return 0;
}output
Result: The program evaluates the entered temperature and displays a message describing the type of weather accordingly.
3.### Program: Compare Two Strings
Aim:
To compare two strings entered by the user and determine whether they are the same or different.
Algorithm:
- Start the program.
- Declare two character arrays
str1andstr2to store the input strings. - Read both strings from the user.
- Call the function
compare(char a[], char b[])to compare the strings:- Initialize
flag = 0andi = 0. - Use a while loop to traverse both strings simultaneously until the null character
\0is encountered. - If any corresponding characters differ, set
flag = 1and break the loop.
- Initialize
- The function returns
0if strings are the same, otherwise1. - In
main, check the return value and display:- "strings are same" if return value is
0. - "strings are not same" if return value is
1.
- "strings are same" if return value is
- End the program.
Source Code:
#include <stdio.h>
int compare(char[], char[]);
int main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
scanf("%s", str1);
scanf("%s", str2);
int c = compare(str1, str2); // calling compare() function
if (c == 0)
printf("strings are same");
else
printf("strings are not same");
return 0;
}
// Function to compare both the strings
int compare(char a[], char b[])
{
int flag = 0, i = 0;
while (a[i] != '\0' && b[i] != '\0')
{
if (a[i] != b[i])
{
flag = 1;
break;
}
i++;
}
if (flag == 0)
return 0;
else
return 1;
} output
Result: The program successfully compares the two input strings and displays whether they are the same or not.
4.### Program: Count Spaces in a String
Aim:
To count the number of words in a string entered by the user.
Algorithm:
- Start the program.
- Declare a character array
str[100]to store the input string. - Initialize a counter variable
count = 0. - Read the input string using
fgets(). - Traverse the string using a loop until the null character
\0is encountered. - For each character, check:
- If it is a space
' 'or newline'\n', incrementcount.
- If it is a space
- Optionally, handle cases where the string starts with a space or newline.
- Print the total count of spaces.
- End the program.
Source Code:
#include <stdio.h>
int main()
{
char str[100];
int ind, count = 0;
fgets(str, sizeof(str), stdin);
for (ind = 0; str[ind] != '\0'; ind++) {
if (str[ind] == ' ' || str[ind] == '\n') {
count++;
}
}
if (ind > 0 && (str[0] == ' ' || str[0] == '\n')) {
count++;
}
printf("%d", count);
return 0;
}output
5.### Program: Convert a String to Uppercase
Aim:
To convert all lowercase characters in a string to uppercase and display the result.
Algorithm:
- Start the program.
- Declare a character array
str[100]to store the input string. - Read the string from the user using
fgets(). - Traverse the string using a loop until the null character
\0is encountered. - For each character, use the
toupper()function to convert it to uppercase. - Replace the original character with the uppercase character in the string.
- Print the converted uppercase string.
- End the program.
Source Code:
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i;
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
printf("%s", str);
return 0;
}output