Skip to content

Commit 26992a6

Browse files
committed
Add code snippet to count positive, negative, and zero numbers
1 parent 0d4aac1 commit 26992a6

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

c.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<stdio.h>
2+
3+
int main()
4+
{
5+
int num, temp, rem=0, sum=0, i;
6+
float cube;
7+
8+
num = 1;
9+
temp = 1;
10+
for(i=1;i<=500;i++)
11+
{
12+
while(num!=0)
13+
{
14+
rem = num%10;
15+
cube = pow(rem,3); //or we can write (rem*rem*rem)
16+
sum = sum + cube;
17+
num = num/10;
18+
} //HERE THE VALUE OF num = 0
19+
if(sum == temp)
20+
printf("%d\n",temp);
21+
22+
//set default values to the variables
23+
rem = 0;
24+
sum = 0;
25+
cube = 0;
26+
//increment temp and num value according to i
27+
//for first loop i = 1
28+
temp = i+1;
29+
num = i+1;
30+
}
31+
32+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
### Code Snippet: Count Positive, Negative, and Zero Numbers
2+
3+
#### Inputs
4+
The code snippet takes input from the user in the form of integer numbers.
5+
6+
#### Flow
7+
1. The code snippet initializes three variables to keep track of the counts of positive, negative, and zero numbers.
8+
2. It enters a while loop that continues until the user enters 0.
9+
3. Inside the loop, it prompts the user to enter a number.
10+
- If the number is greater than 0, it increments the positive count variable.
11+
- If the number is less than 0, it increments the negative count variable.
12+
- If the number is equal to 0, it increments the zero count variable and breaks out of the loop.
13+
4. After the loop ends, it prints the counts of positive, negative, and zero numbers entered by the user.
14+
15+
#### Outputs
16+
The code snippet outputs the counts of positive, negative, and zero numbers entered by the user.
17+
18+
```python
19+
def count_numbers():
20+
positive_count = 0
21+
negative_count = 0
22+
zero_count = 0
23+
24+
while True:
25+
num = int(input("Enter a number (enter 0 to stop): "))
26+
if num > 0:
27+
positive_count += 1
28+
elif num < 0:
29+
negative_count += 1
30+
else:
31+
zero_count += 1
32+
break
33+
34+
print(f"Positive numbers entered: {positive_count}")
35+
print(f"Negative numbers entered: {negative_count}")
36+
print(f"Zeroes entered: {zero_count}")
37+
38+
count_numbers()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
### Code Snippet: Armstrong Numbers Finder
2+
3+
#### Inputs
4+
The code snippet doesn't take any direct input from the user.
5+
6+
#### Flow
7+
1. The code snippet iterates through numbers from 1 to 500 (inclusive).
8+
2. For each number, it calculates the sum of cubes of its digits.
9+
3. If the sum of cubes of digits is equal to the original number, it prints that number.
10+
11+
#### Outputs
12+
The code snippet outputs Armstrong numbers found within the range of 1 to 500 (inclusive).
13+
14+
```python
15+
def find_armstrong_numbers():
16+
for i in range(1, 501):
17+
num = i
18+
sum_of_cubes = 0
19+
while num > 0:
20+
digit = num % 10
21+
sum_of_cubes += digit ** 3
22+
num //= 10
23+
if sum_of_cubes == i:
24+
print(i)
25+
26+
find_armstrong_numbers()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
for i in range(1, 501):
2+
num = i
3+
sum_of_cubes = 0
4+
while num > 0:
5+
digit = num % 10
6+
sum_of_cubes += digit ** 3
7+
num //= 10
8+
if sum_of_cubes == i:
9+
print(i)

0 commit comments

Comments
 (0)