Write and compile the C++ programs in lenux (g++) Comment. equation for the 1st
ID: 3788379 • Letter: W
Question
Write and compile the C++ programs in lenux (g++) Comment.
equation for the 1st one is n(n+1)/2
Write a C program to find the sum and average of first n natural numbers. The user inputs the value of n. Comment your C code. Write a C program to find the largest and smallest of n non-zero positive integers. The user will input the value of n followed by each of the n values. Comment your C code. Write a C program to find factorial of an integer n. The user inputs the value of n. Do not use recursion. Comment your C code.Explanation / Answer
1)
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
double avg = (n+1)/2.0;
printf("The average of first %d numbers is %lf ",n,avg);
return 0;
}
2)
#include <stdio.h>
int main()
{
int n,min,max,temp,i;
scanf("%d",&n);
if(n>0)
{
scanf("%d",&min);
}
max = min;
for(i=1;i<n;i++)
{
scanf("%d",temp);
if(temp>max)
{
max=temp;
}
if(temp<min)
{
min=temp;
}
}
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.