In: Computer Science
Write code in C please.
#1 Write a function multiples() which will take an integer input and it will print out all the multiples of this number starting from 2 but not including itself. For example, multiples(10) will print 2, 5 and multiples(100) will print 2, 4, 5, 10, 20, 25, 50
#2 Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers.
Source Code:
Output:
Code in text format (See above images of code for indentation):
/*include library files*/
#include <stdio.h>
/*main function*/
int main(void)
{
/*function prototypes*/
void multiples();
void Fibonacci();
/*variables*/
int n,num;
/*read a number from user*/
printf("Enter a number to print multiples: ");
scanf("%d",&n);
/*function call*/
printf("The multiples of %d are\n",n);
multiples(n);
/*read a number from user*/
printf("\nEnter number of terms for fibonacci:
");
scanf("%d",&num);
/*function call*/
Fibonacci(num);
return 0;
}
/*function definition*/
void multiples(int n)
{
int i;
/*using loop print multiples*/
for(i=2;i<n/2;i++)
{
/*check and print*/
if(n%i==0)
printf("%d,
",i);
}
printf("%d",i);
}
/*function definition*/
void Fibonacci(int num)
{
int i,f=0,s=1,n;
/*using for loop print fibonacci series*/
for(i=0;i<num;i++)
{
if(i<=1)
n=i;
else
{
/*swap for next
iteration*/
n=f+s;
f=s;
s=n;
}
/*print number*/
printf("%d ",n);
}
}