Cプログラムを作成して、シリーズの最大数と最小数を見つけます
問題
ユーザーがコンソールに4つの一連の整数を入力し、一連の中で最小および最大の数を見つけてもらいます
解決策
小さい数と大きい数を計算するには、if条件を使用します。最大数と最小数を見つけるために使用するロジックは-
です。if(minno>q) //checking 1st and 2nd number minno=q; else if(maxno&l;q) maxno=q; if(minno>r) //checking 1st and 3rd number minno=r;
プログラム1
#include<stdio.h>
int main(){
int minno,maxno,p,q,r,s;
printf("enter any four numbers:");
scanf("%d%d%d%d",&p,&q,&r,&s);
minno=p;
maxno=p;
if(minno>q) //checking 1st and 2nd number
minno=q;
else if(maxno<q)
maxno=q;
if(minno>r) //checking 1st and 3rd number
minno=r;
else if(maxno<r)
maxno=r;
if(minno>s) //checking 1st and 4th number
minno=s;
else if(maxno<s)
maxno=s;
printf("Largest number from the given 4 numbers is:%d\n",maxno);
printf("Smallest numbers from the given 4 numbers is:%d",minno);
return 0;
} 出力
enter any four numbers:34 78 23 12 Largest number from the given 4 numbers is:78 Smallest numbers from the given 4 numbers is:12
プログラム2
以下のプログラムは、配列内の最小要素と最大要素を検索します-
#include<stdio.h>
int main(){
int a[50],i,num,large,small;
printf("Enter the number of elements :");
scanf("%d",&num);
printf("Input the array elements :\n");
for(i=0;i<num;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<num;++i){
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("small= %d\n",small);
printf("large= %d\n",large);
return 0;
} 出力
Enter the number of elements :8 Input the array elements : 1 2 6 4 8 9 3 9 small= 1 large= 9
-
リスト内の最小数を見つけるPythonプログラム
この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 −リストが表示されます。リストで利用可能な最小の番号を表示する必要があります ここでは、リストを並べ替えて最小の要素を取得するか、組み込みのmin()関数を使用して最小の要素を取得できます。 次に、以下の実装の概念を観察しましょう- 例 list1 = [101, 120, 104, 145, 99] # sorting using built-in function list1.sort() print("Smallest element is:", list1[0]) 出力 Smal
-
リスト内で最大の数を見つけるPythonプログラム
この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 −リストが与えられたので、リストの最大の要素を計算する必要があります。 ここでは、組み込み関数を使用して、問題ステートメントの解決策に到達します sort()関数の使用 例 # list list1 = [23,1,32,67,2,34,12] # sorting list1.sort() # printing the last element print("Largest element is:", list1[-1]) 出力 Largest in given array is 67