Part 2
Part 2
i)
#include <stdio.h>
int main() {
int n, i;
int a = 0, b = 1, c;
printf("Enter the number of Fibonacci terms: ");
scanf("%d", &n);
if (n >= 1) {
printf("%d ", a);
}
if (n >= 2) {
printf("%d ", b);
}
2. ii)
#include <stdio.h>
#include<math.h>
int main() {
int n, i, a[100];
float avg, sd, s=0;
printf("How many numbers\n");
scanf("%d", &n);
printf("Enter the numbers\n");
for(i=0; i<n; i++){
scanf("%d", &a[i]);
s=s+a[i];
}
avg = s/n;
printf("The sum is: \n");
printf("The average is \n");
s=0;
for(i=0; i<n; i++){
s=s+(a[i]-avg)*(a[i]-avg);
}
sd = sqrt(s/n);
printf("Standard deviation is: \n");
return 0;
}
Input:
Enter the number of elements: 4
Enter number 1: 5
Enter number 2: 10
Enter number 3: 15
Enter number 4: 20
Output:
Sum = 50.00
Average = 12.50
Standard Deviation = 5.59
2. iii)
#include <stdio.h>
int main() {
int n, i, a[100], key;
printf("Enter the number of characters\n");
scanf("%d",&n);
printf("Enter the sorted elements\n")
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
printf("Enter the key to be found\n");
scanf("%d", &key);
l=0; u=n-1; flag=1;
while(l<=u){
mid=(l+n)/2;
if(a[mid]==key){
printf("No. found at %d", mid);
flag=0;
break;
}
if(a[mid]>key){
u=mid-1;
}
if(a[mid]<key){
l=mid+1;
}
}
if(flag==1){
printf("Not found");
}
return 0;
}
Input:
Enter the number of characters
5
Enter the sorted elements
1
3
5
7
9
Enter the key to be found
7
Output:
No. found at 3
2. iv)
#include <stdio.h>
int main() {
int n, i, a[100], key;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n")
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
printf("Enter the key to be found\n");
scanf("%d", &key);
flag=1;
for(i=0; i<n; i++){
if(a[i] == key){
printf("Found at the %d location", i);
flag=0;
break;
}
if(flag == 1){
printf("Not found")
}
return 0;
}
Input:
Enter the number of elements
5
Enter the elements:
10
20
30
40
50
Enter the key to be found
30
Output:
Found at 2 location