Professional Documents
Culture Documents
Write a program that finds all of the Fibonacci numbers up to some number n,
that we input from the keyboard.
Fibonacci numbers are number where the next element is sum of the previous two
numbers. By definition a1=1, a2=1
#include<stdio.h>
int main()
{
int a[100],i,n;
printf("Enter the final number: "); scanf("%d",&n);
a[0]=a[1]=1;
for(i=1;i<n-1;i++)
a[i+1]=a[i]+a[i-1];
printf("The fibonacci numbers are: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
2. Write a program that counts the number of appearances of the minimal and the
maximal number of some integer array that is inputted from the keyboard
#include<stdio.h>
int main()
{
int a[100],cMin=0,cMax=0,min,max,i,n;
printf("How many elements? "); scanf("%d",&n);
printf("Enter the array:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i); scanf("%d",&a[i]);
}
max=min=a[0];
for(i=1;i<n;i++)
{
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
for(i=0;i<n;i++)
{
if(a[i]==min)
cMin++;
if(a[i]==max)
cMax++;
}
printf("The maximum element is counted %d times.\n",cMax);
printf("The minimum element is counted %d times.\n",cMin);
return 0;
}
3. Write a program that finds, counts and displays the appearance of some number
in one integer array. The desired number is input from the keyboard
#include<stdio.h>
int main()
{
int a[100],count=0,i,n,m;
printf("How many elements? "); scanf("%d",&n);
printf("Enter the array:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i); scanf("%d",&a[i]);
}
printf("Enter the number: "); scanf("%d",&m);
for(i=0;i<n;i++)
if(a[i]==m)
{
printf("The number %d is found in position %d.\n",m,i+1);
count++;
}
printf("The number %d appeared %d times.\n",m,count);
return 0;
}
4. Write a program that swaps the biggest and the smallest element in some
array, that is input from the keyboard
#include<stdio.h>
int main()
{
int a[100],i, iMin, iMax, n,min,max,temp;
printf("How many elements? "); scanf("%d",&n);
printf("Enter the array:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i); scanf("%d",&a[i]);
}
max=min=a[0];
iMin=iMax=0;
for(i=0;i<n;i++)
{
if(a[i]<min)
{
min=a[i];
iMin=i;
}
if(a[i]>max)
{
max=a[i];
iMax=i;
}
}
temp=a[iMin];
a[iMin]=a[iMax];
a[iMax]=temp;
printf("The new array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
5. Write a program that finds the biggest incrementing subarray is some integer
array that is inputted from the keyboard
#include<stdio.h>
int main()
{
int a[100],i,j,n,length,temp,pos1;
printf("How many elements? "); scanf("%d",&n);
printf("Enter the array:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i); scanf("%d",&a[i]);
}
length=0;
for(i=0;i<n;i++)
{
temp=1;
j=i;
while(a[j]<a[j+1])
{
temp++;
j++;
}
if(temp>length)
{
pos1=i;
length=temp;
}
}
printf("The biggest increasing subarray has length %d, and it is: ",length);
for(i=pos1;i<=pos1+length-1;i++)
printf("%d ",a[i]);
return 0;
}