Lets write a C program to enter number till the user wants. At the end it should display the count of positive number, negative number and zeros entered, using Ternary Operator or Conditional Operator and without using arrays.
Lets write a C program to enter number till the user wants. At the end it should display the count of positive number, negative number and zeros entered, without using arrays.
Output 1: Enter the limit 8 Enter 8 numbers 1 -5 0 6 -2 0 5 4
Positive Numbers: 4 Negative Numbers: 2 Number of zero: 2
Output 2: Enter the limit 10 Enter 10 numbers 2 6 9 3 -5 -7 0 9 -10 -50
Positive Numbers: 5 Negative Numbers: 4 Number of zero: 1
Logic To Count Positive, Negative and Zero without using Array
We ask the user to enter the maximum numbers he or she wants to enter, and store it inside variable limit. Now we iterate the while loop limit number of times. Inside the while loop, for each iteration, until limit is equal to zero, we keep asking the user to enter a number. Once the user enters the number we check if its greater than 0, if its true we’ll increment the value of variable positive by one. If the user entered number is less than 0, then we increment the value of variable negative by one. If the user entered number is neither greater than 0 nor less than 0, then its a zero – so we increment the value of variable zero by one.
For each iteration of the while loop, we decrement the value of variable limit by one. Once the value of variable limit is 0, control exits the while loop and the result will be printed. That is, the number of positives, negatives and the zeros entered by the user.
Logic To Draw Pyramid of Alphabets, using While Loop
We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).
In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the alphabets).
Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the alphabets needed for each row.
At the end the result will be a pyramid with the number of rows as input by the user.
Note: Inside second nested while loop we are checking for the condition – if alphabet is equal to 90(ASCII value of Z), and then reinitializing it to 64(but ASCII value of A is 65), that is because after the if condition is executed we have alphabet++; statement which increments the value of variable alphabet from 64 to 65(which is the ASCII value of A).
Alphabets and ASCII Value We can even write the code in if else block as shown below:
We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).
In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the stars).
Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the stars needed for each row.
At the end the result will be a pyramid with the number of rows as input by the user.
Source Code: C Program To Draw Pyramid of Stars, using While Loop
#include<stdio.h>
int main()
{
int num, count = 1, i;
printf("Enter no of rows of Pyramid\n");
scanf("%d", &num);
while(count <= num)
{
i = 0;
while( i <= (num - count) )
{
printf(" ");
i++;
}
i = 0;
while(i < (2*count-1))
{
printf("*");
i++;
}
printf("\n");
count++;
}
return 0;
}
Logic To Find Sum of Squares of Numbers from 1 to N, using While Loop
We ask the user to enter the limit. We initialize variable count to 1. Now inside while loop we square the value of variable count and add it to the previous value of variable sum – we repeat this for each iteration of while loop until value of count is less than or equal to value entered by the user(which is stored in variable N). For each iteration of the while loop value of variable count is incremented by 1.
Video Tutorial: C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop
Source Code: C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop
#include < stdio.h >
int main()
{
int N, count = 1, sum = 0;
printf("Enter the limit\n");
scanf("%d", &N);
while(count <= N)
{
sum = sum + (count * count);
count++;
}
printf("Sum of squares of numbers from 1 to %d is %d.\n", N, sum);
return 0;
}
Output: Enter the limit 5 Sum of squares of numbers from 1 to 5 is 55.