如何使用 For 循环、While 循环和函数以及示例编写 C 语言程序来计算数组中的正数和负数。
C 语言计算数组中正数和负数的程序
此程序允许用户输入一维数组的大小和元素。接下来,我们使用 For 循环迭代数组值并检查正数和负数。
/* C Program to Count Positive and Negative Numbers in an Array */
#include<stdio.h>
int main()
{
int Size, i, a[10];
int Positive_Count = 0, Negative_Count = 0;
printf("\n Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements\n");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < Size; i ++)
{
if(a[i] >= 0)
{
Positive_Count++;
}
else
{
Negative_Count++;
}
}
printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
return 0;
}

在此,For 循环将确保数字在 0 和最大一维数组大小值之间。在此示例中,它将是 0 到 4。
for(i = 0; i < Size; i ++)
在下一行,我们声明了If语句
if(a[i] >= 0)
任何大于或等于 0 的数字都是正数。If 条件将检查这一点。
使用 While 循环计算数组中正数和负数的程序
此程序与上述程序相同,但这次我们使用了While 循环来计算数组中的正数和负数。
/* C Program to Count Positive and Negative Numbers in an Array */
#include<stdio.h>
int main()
{
int Size, i, j = 0, a[10];
int Positive_Count = 0, Negative_Count = 0;
printf("\n Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements\n");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
while(j < Size)
{
if(a[j] >= 0)
{
Positive_Count++;
}
else
{
Negative_Count++;
}
j++;
}
printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
return 0;
}
Please Enter the Size of an Array : 7
Please Enter the Array Elements
10 -56 -98 -78 0 -14 10
Total Number of Positive Numbers in this Array = 3
Total Number of Negative Numbers in this Array = 4
使用函数计算数组中正数和负数的程序
此程序与第一个示例相同。但我们使用函数将计算正数和负数的逻辑分开。
/* C Program to Count Positive and Negative Numbers in an Array */
#include<stdio.h>
int CountPositiveNumbers(int a[], int Size);
int CountNegativeNumbers(int a[], int Size);
int main()
{
int Size, i, a[10];
int Positive_Count = 0, Negative_Count = 0;
printf("\n Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements : ");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
Positive_Count = CountPositiveNumbers(a, Size);
Negative_Count = CountNegativeNumbers(a, Size);
printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
return 0;
}
int CountPositiveNumbers(int a[], int Size)
{
int i, Positive_Count = 0;
printf("\n List of Positive Numbers in this Array: ");
for(i = 0; i < Size; i ++)
{
if(a[i] >= 0)
{
printf("%d ", a[i]);
Positive_Count++;
}
}
return Positive_Count;
}
int CountNegativeNumbers(int a[], int Size)
{
int i, Negative_Count = 0;
printf("\n List of Negative Numbers in this Array: ");
for(i = 0; i < Size; i ++)
{
if(a[i] < 0)
{
printf("%d ", a[i]);
Negative_Count++;
}
}
return Negative_Count;
}
Please Enter the Size of an Array : 10
Please Enter the Array Elements : -12 0 -18 -16 106 125 -98 12 145 -12
List of Positive Numbers in this Array: 0 106 125 12 145
List of Negative Numbers in this Array: -12 -18 -16 -98 -12
Total Number of Positive Numbers in this Array = 5
Total Number of Negative Numbers in this Array = 5
评论已关闭。