luc031.c¶
Problem Statement
Write a program to enter numbers till the user wants. At the end it should display the count of positive, negative and zeros entered.
Metadata¶
| Property | Detail |
|---|---|
| Author | Amit Dutta (amitdutta4255@gmail.com) |
| License | MIT |
| Difficulty | Beginner (index: 2 / 10) |
Actions¶
You can print or save this file by opening Raw and using your browser.
Source Code¶
#include <stdio.h>
int main()
{
int choice = 1, num, positive_count = 0, negative_count = 0, zero_count = 0;
while (choice == 1)
{
printf("\nEnter the number (Type any character and press Enter to finish.) : ");
choice = scanf("%d", &num); // Checking whether the user has input any characters
if (choice == 1)
{
printf("Number recorded : %d", num);
if (num < 0)
negative_count++;
else if (num > 0)
positive_count++;
else if (num == 0)
zero_count++;
}
else
{
// If the user inputs any characters, then choice = 0, it means he doesn't want to give any more input;
choice = 0;
printf("\nCharacter received. Stopping input...\n");
}
}
// Display the final results
printf("\n====================================\n");
printf(" Analysis Complete\n");
printf("====================================\n");
printf("Positive numbers entered: %d\n", positive_count);
printf("Negative numbers entered: %d\n", negative_count);
printf("Zeroes entered: %d\n", zero_count);
printf("Total numbers recorded: %d\n", positive_count + negative_count + zero_count);
printf("====================================\n");
}
Explanation¶
Explain with AI
Copy the prompt below and paste it into any AI assistant.
You are explaining a C programming code to a beginner.
STRICT RULES:
- Only use the given code. Do NOT assume anything not present.
- Do NOT add extra examples.
- Keep explanation clear and short.
- If something is unclear, say "Not clear from code".
- Follow the exact format below. Do NOT change headings.
FORMAT:
[START]
## What it does
(Explain the overall purpose in 1-2 sentences)
## Step-by-step
(Explain how the code works in steps, simple language)
## Key Concepts
(List concepts like loop, condition, function, etc.)
## Notes
(Mention any limitations, errors, or assumptions)
[END]
CODE (luc031.c):
#include <stdio.h>
int main()
{
int choice = 1, num, positive_count = 0, negative_count = 0, zero_count = 0;
while (choice == 1)
{
printf("\nEnter the number (Type any character and press Enter to finish.) : ");
choice = scanf("%d", &num); // Checking whether the user has input any characters
if (choice == 1)
{
printf("Number recorded : %d", num);
if (num < 0)
negative_count++;
else if (num > 0)
positive_count++;
else if (num == 0)
zero_count++;
}
else
{
// If the user inputs any characters, then choice = 0, it means he doesn't want to give any more input;
choice = 0;
printf("\nCharacter received. Stopping input...\n");
}
}
// Display the final results
printf("\n====================================\n");
printf(" Analysis Complete\n");
printf("====================================\n");
printf("Positive numbers entered: %d\n", positive_count);
printf("Negative numbers entered: %d\n", negative_count);
printf("Zeroes entered: %d\n", zero_count);
printf("Total numbers recorded: %d\n", positive_count + negative_count + zero_count);
printf("====================================\n");
}