Using C Programming: Write a program that implements a function which can take a
ID: 3598761 • Letter: U
Question
Using C Programming:
Write a program that implements a function which can take a positive integer, N, and computes the sum of all the non-negative integers smaller or equal to N. For example, if you call the function with N=5, it will output 1+2+3+4+5=15. Note that the main part of your program will just have a function call. No other components need to be there in the main part.
Q.2 Write a program that uses a function to take 5 integers from the user, store them in an array and display their average on the screen. Note that the main part of your program will just have a function call. No other components need to be there in the main part.
Q.3 Write a program that will take 7 integers from the user, store them in an array. Write three functions which takes the aforementioned array as input and display them on the screen 1) in the order as they were entered, 2) in increasing order and 3) in the decreasing order. For instance if the input is: 13 -23 1 45 -2 16 -5 then the call to the first function will display:
13 -23 1 45 -2 16 -5,
call to the second function will display:
-23 -5 -2 1 13 16 45
Finally, call to the 3rd function will display:
45 16 13 1 -2 -5 -23.
Explanation / Answer
1)
#include <stdio.h>
int add(int x)
{
int c,sum;
for (c = 1; c <= x; c++)
{
sum = sum + c;
}
return sum;
}
int main()
{
int c,n,sum=0;
printf("enter the n value");
scanf("%d",&n);
printf("The sum value is:%d",add(n));
}
Input : 5
Output :
enter the n value:5
The sum value is:15
----------------------------------------------------------------------------------------------------------------
2)
#include <stdio.h>
float avg()
{
int c,sum;
int num[5];
printf("Enter the values: ");
for(c=1;c<=5;c++)
{
scanf("%d",&num[c]);
}
for (c = 1; c <= 5; c++)
{
sum = sum + num[c];
}
return sum/5;
}
int main()
{
int c,n,sum=0;
int num[5];
printf("The Average value of give numbers:%f",avg());
}
Input : 1 2 3 4 5
Output : 1 2 3 4 5
The Average value of give numbers:3.000000
-------------------------------------------------------------------------------------------------------
3)
#include <stdio.h>
void printOrder(int[] num)
{
int i,j,temp,c;
printf("The Normal Display :");
for (c = 1; c <= 5; c++)
{
printf("%d ",num[c]);
}
}
void printDesOrder(int[] num)
{
int i,j,temp,c;
for(i=0;i< 5;i++)
{
for(j=i+1;j< 5;j++)
{
if(num[i]< num[j])
{
temp =num[i];
num[i] =num[j];
num[j] =temp;
}
}
}
printf(" The Descending order Display :");
for (c = 1; c <= 5; c++)
{
printf("%d ",num[c]);
}
}
void printAscOrder(int[] num)
{
int i,j,temp,c;
for (i = 0; i < 5; ++i)
{
for (j = i + 1; j < 5; ++j)
{
if (num[i] > num[j])
{
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf(" The Ascending order Display :");
for (c = 1; c <= 5; c++)
{
printf("%d ",num[c]);
}
}
int main()
{
int c,n,sum=0;
int num[5];
printf("Enter the values: ");
for(c=1;c<=5;c++)
{
scanf("%d",&num[c]);
}
printOrder(num);
printDesOrder(num);
printAscOrder(num);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.