-
for loop problem
I need to write a program that takes a positive integer that is input and then output an integer. the ouput is the integer input multiplied by each integer below i. e.g. if 5 was input, i need to ouput 5*4*3*2*1. I can get the number input and store it in a variable, but im not sure about the loop i need. i think it would be a 'for' loop which decreases the value of the input and then multiply that witht he value input
-
> i think it would be a 'for' loop which decreases the value of the input and then multiply that witht he value input
Sounds right to me
-
Post what you have, even if it's not much.
-
Here is all i have at the moment
Code:
#include <stdio.h>
void main(void) {
printf("Enter a positive integer");
scanf("%d", &num);
fflush(stdin);
while (num>=num && num>0) {
printf("%d", num*num--);
}
}
-
Don't use "fflush(stdin)", search the board as to why not.
-
So having correctly determined that a for loop was necessary, you go ahead and use a while loop.
> void main(void)
int main ( void )
-
Here is a similar program i have developed unfourtunatly it still isnt working properly i seem to get 35 or somewhere there abouts by multiplyig say 5*4*3*2*1. Perhaps some one could tell me why?
[code]
#include <stdio.h>
#include <conio.h>
void main (void)
{
int num;
int x;
int mult = 0;
clrscr();
printf("please enter a number: ");
scanf("%d", &num);
fflush(stdin);
for(x=1;x<=num;x++)
{
printf("%d", x);
printf("*");
mult++;
}
x=mult*x;
printf("%d", x);
}
[\code]
-
Heh - Colin and David are at the same college...
> x=mult*x;
Try
1. putting this inside the loop
2. making it mult=mult*x;
printing mult (not x) when you're done as well
-
I have changed the code slightly but it is still not outputting the correct value:
Code:
#include <stdio.h>
#include <conio.h>
void main(void) {
int num;
int x;
int div;
clrscr();
printf("Enter a positive integer : ");
scanf("%d", &num);
fflush(stdin);
for (x=num; x>0; x--) {
printf("%d", x);
printf("*");
div++;
div=div*x;
}
printf(": %d", div);
}
-
Code:
#include <stdio.h>
int main(int argc, char *argv[]) {
int num=0;
int x=0;
int div=1;
printf("Enter a positive integer : ");
scanf("%d", &num);
/* fflush(stdin); */
for (x=num; x>0; x--) {
printf("%d", x);
if(x>1) printf("*");
/* div++ */
div=div*x;
}
printf(": %d\n", div);
return 0;
}
-
Well, obviously, you don't pick up hints very well:
Change:
1) void main(void) -> int main(void)
2) remove "fflush (stdin);"
Add a return statement.
What is the purpose of "div++;"?? You never initialize div... or x or num for that matter.
edit: beaten. :(
-
thanks salem, but now im geting 325. i ned to write a C program that accepts an int value and returns it s factroal if thats any help
-
one other small problem im having, i also need to make a program that subtracts one time from another, it is supposed to display in 24 h format, thing is i cant seem to get 00:00 hours to appear by subtracting two times that are the same ie 10:00 - 10:00 I have managed to get a display of 22:00 hoursso what am i doing wrong?
here is the code
Code:
#include <stdio.h>
#include <conio.h>
void main (void)
{
int hours,hours2=0;
int mins, mins2=0;
clrscr();
printf("Enter the time you reqire (hours) 24-h format: ");
scanf("%d", &hours);
fflush(stdin);
printf("\n");
printf("now enter the minutes: ");
scanf("%d", &mins);
fflush(stdin);
if (hours >23)
hours=0;
if (mins >=60)
{
mins=mins-60;
hours++;
}
printf("the time entered is %2d:%.2d\n", hours, mins);
printf("\n");
printf("Enter the time you wish to be subtracted (hours): ");
scanf("%d", &hours2);
fflush(stdin);
printf("\n");
printf("now enter the minutes: ");
scanf("%d", &mins2);
fflush(stdin);
if ((hours2<hours) && (mins2<mins))
{
hours=hours2-hours;
mins=mins2-mins;
}
else
{
hours=hours-hours2;
mins=mins2-mins;
hours--;
}
if (hours<0)
hours=hours+23;
printf("the time is now %.2d:%.2d", hours, mins);
}
-
Yeah, we're well aware of what you're trying to do, so how about posting your latest effort?
And HOW many times are you going to ignore the don't use void main and fflush(stdin) ?
-
Quote:
Originally posted by davidmccabe
I have changed the code slightly but it is still not outputting the correct value:
Code:
#include <stdio.h>
#include <conio.h>
void main(void) {
int num;
int x;
int div;
clrscr();
printf("Enter a positive integer : ");
scanf("%d", &num);
fflush(stdin);
for (x=num; x>0; x--) {
printf("%d", x);
printf("*");
div++;
div=div*x;
}
printf(": %d", div);
}
How did this process get messed up? :D
Code:
fact = 1;
for(i=input; i > 0; i--)
{
printf("%d%s", i, (i > 1 ? "*" : ""));
fact *= i;
}
printf(" = %d", fact);
Are you sure that you want ints? This value can grow rather rapidly.