0% found this document useful (0 votes)
7 views

Assignment 8

C program
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Assignment 8

C program
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment :- 8

Ques 1. In mathematics, the Fibonacci numbers, commonly denoted Fn form a


sequence, called the Fibonacci sequence, such that each number is the sum of
the two preceding ones, starting from 0 and 1. That is, and for n > 1. By
starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
...
Write a C program to find the sum of the even-valued terms from the terms in
the Fibonacci sequence (up to n terms entered by the user)
Code :
#include<stdio.h>
int main(){
int a[100]={1,2},n, sum = 0;

printf("Enter number of terms of fibonacci series\n");


scanf("%d",&n);

for(int i = 2; i<n; i++){


a[i]=a[i-1] + a[i-2];
}
for(int j = 0; j < n ; j++){
if(a[j]%2==0){
sum = sum + a[j];
}
}
printf("The sum of even valued numbers in Fibonacci Series upto %d terms is %d",n,
sum);
return 0;
}
OUTPUT:

Oues 2. Write a program to find the frequency of each element in an array of


integers
Code:
#include<stdio.h>
int main(){
int a[100],n,i, count;

printf("Enter the size of array\n");


scanf("%d",&n);

printf("Enter array elements\n");


for(i = 0; i < n; i++){
printf("Element a[%d] : ",i);
scanf("%d",&a[i]);
}

for(i = 0; i<n;i++){
count = 1;
for(int j = i+1; j < n ; j++){
if(a[i]==a[j]){
count+=1;
for(int m = j;m<n;m++){
a[m]=a[m+1];
}
n-=1;
}
}
printf("%d, frequency is %d\n",a[i],count);
}
return 0;
}
OUTPUT :
Oues 3. Write a program to find the median of two sorted arrays
Code :
#include<stdio.h>
int main(){
int a[]={5,10,9,7,25,6,8,12};
int b[]={2,9,4,63,1,26,4,5,8};
int n1,n2,m1,m2;

n1 = sizeof(a)/sizeof(a[0]);
n2 = sizeof(b)/sizeof(b[0]);

if(n1%2==0){
m1 = (a[(n1-1)/2]+a[((n1-1)/2)+1])/2;
printf("The median of array a[] is %d\n",m1);
}
else{
m1 = a[(n1-1)/2];
printf("The median of array a[] is %d\n",m1);
}
if(n2%2==0){
m2 = (b[(n2-1)/2]+b[((n2-1)/2)+1])/2;
printf("The median of array b[] is %d\n",m2);
}
else{
m2 = b[((n2-1)/2)];
printf("The median of array b[] is %d\n",m2);
}
return 0;
}
OUTPUT :

Oues 4. Write a program to take a 2D array as input from user and find the
sum of elements.
Code :
#include<stdio.h>
int main(){
int a[50][100], n1,n2,i,j,sum = 0;

printf("Enter the Rows numbers in array\n");


scanf("%d",&n1);
printf("Enter the column numbers in array\n");
scanf("%d",&n2);

for(i = 0;i<n1;i++){
for(j=0;j<n2;j++){
printf("Enter a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}
for(i = 0;i<n1;i++){
for(j=0;j<n2;j++){
sum = sum + a[i][j];
}
}
printf("The sum of elements of 2D array is %d",sum);
return 0;
}
OUTPUT :

You might also like