Program code needed.

Closed
bLade - Sep 15, 2010 at 07:57 AM
 1-4 . :DD - Sep 16, 2010 at 06:52 PM
Please give the program code of this problems:

1. Enter a number (x) and get the average of all numbers from 1 to x.
2. Enter a number (x) and print the factorial of the number.
Program will look like this:
Enter value: 4
4 24
-Exit when the value entered is zero (0).
factorial = (n)*(n-1)*(n-2)
ex: 4!=4*3*2*1

* I dont have any clue on number 2 on how to do it.
*This two numbers need a loop (for).
* I tried number 1 but it's not right, anyway here is the program code i did:
#include <stdio.h>
#include <conio.h>
int main()
{
int x,y;
float z;
clrscr();
printf ("\nEnter a number:\t");
scanf ("%d",&x);
for (y=z;y>0;y--);
z=(y+z)/(x);
printf ("\n\nThe average of all numbers from 1 to %d is %f",x,z);
getch();
return(0);
}


*THANKS in advance.

2 responses

jack4rall Posts 6428 Registration date Sunday June 6, 2010 Status Moderator Last seen July 16, 2020
Sep 15, 2010 at 08:38 AM
Hello,

Try this 1.

1) To Find The Average

/* To Find The Average */
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
float t=0;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
t=t+i;
t=t/n;
printf("The average from 1 to %d is %.2f",n,t);
getch();
}

2) To find the factorial of a given number

/* To find the factorial of a given number */
#include<stdio.h>
#include<conio.h>
main()
{
int i,n;
long t=1;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
if(n==0)
exit();
else
for(i=1;i<=n;i++)
t=t*i;
printf("Factorial of given number is %d is %ld",n,t);
getch();
}

Good Luck.
0
nice prog though .
0