Write a C program to read an integer and display the message as
“Positive Number”, “Negative Number” or “Zero Value” opening
on the value of the given number. Use ternary operator.
Answers
Answer:
C program to accept read numbers until -1
#include <stdio.h>
void main ()
{
int num, size;
while(1)
{
printf("Enter number: ");
scanf("%d", &num);
if(num == -1)
{
break;
}
}
}
Answer:
C program to count the number of positive, negative, zeroes encountered by users using a while loop
#include <stdio.h>
void main ()
{
int size = 0, i = 0, positive = 0, negative = 0, zero = 0;
printf("Enter the size of array: ");
scanf("%d", &size);
int arr[size];
while(i < size){
printf("Enter number: ");
scanf("%d", &arr[i]);
if( arr[i] > 0){
positive++ ;
}
if(arr[i] == 0){
zero++ ;
}
if(arr[i] < 0){
negative++ ;
}
i += 1;
}
printf("Positive numbers: %d", positive);
printf("\nNegative numbers: %d", negative);
printf("\nZero valued numbers: %d", zero);
}
int x,p,n,z;
n=p=z=0
do{
printf(“Enter an integer: “);
scanf(“%d",&x);
if(x==-1) break;
if(x<0)
n++;
else if (x>0) p++;
else z++;
}
while(1);