or the following questions you are only allowed to use programming constructs an
ID: 3722980 • Letter: O
Question
or the following questions you are only allowed to use programming constructs and unctions we have talked about in class. 1. Write a C program that asks the user to input two numbers a,n and computes and returns the following sunm rn i=0 2. Now modify your progra m to also create an array of the correct size where each entry in the y is one of the terms in the sum. For example, if a is 1 and n is 5. Your array should have the following entries (0,1, V2, V3, v4, v5). Write code to print each entry in the array. arra 3. Write a C program that asks the user to enter a positive integer n and computes and returns the produce of the numbers from 1 to n.Explanation / Answer
Q1.
#include<stdio.h>
#include<math.h>
// function to find summation a*sqrt(i) of given number
double number(int a,int n)
{
double res = a;
int i;
for (i=2; i<=n; i++)
res *=a* sqrt(i);
return res;
}
int main()
{
int a,n;
printf("Enter number a ");
scanf("%d",&a);
printf("Enter number n ");
scanf("%d",&n);
printf("a= %d ,n= %d is %lf",a, n, number(a,n));
return 0;
}
2.
#include<stdio.h>
#include<math.h>
#include<malloc.h>
// function to find summation a*sqrt(i) of given number
double number(int a,int n, double arr[])
{
double res = a;
int i;
arr[0]=0;
arr[1]=a;
for (i=2; i<=n; i++){
arr[i]=a* sqrt(i);
res +=arr[i];
}
return res;
}
int main()
{
int a=1,n=5;
double *arr;
printf("Enter number a ");
// scanf("%d",&a);
printf("Enter number n ");
// scanf("%d",&n);
arr = (double *)malloc((n+1)* sizeof(double));
printf("a= %d ,n= %d is %lf ",a, n, number(a,n,arr));
for (int i =0; i<=n; i++)
printf("%lf,",arr[i]);
return 0;
}
Q.3
#include<stdio.h>
// function to find 1.2....n of given number
unsigned int number(unsigned int n)
{
int res = 1, i;
for (i=2; i<=n; i++)
res *= i;
return res;
}
int main()
{
int num;
printf("Enter a positive integer ");
scanf("%d",&num);
printf("1.2.3..... of %d is %d", num, number(num));
return 0;
}
Output :-
Enter a positive integer 1.2.3..... of 5 is 120
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.