English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

the program should compute the sum of all the interger between m and n that are divisible by k

2006-10-18 05:32:12 · 4 answers · asked by help 1 in Computers & Internet Programming & Design

/*Write an interactive program that asks the user to supply three integers'
k,m,n with k being >than 1. Write a program that computes the sum of all the
intergers between m and n that is divisble by k.*/

#include
main()
{
int i, k, m, n, e,sum,total,then;

FILE *inf,*outf;
inf = fopen("data3.dat","r");
outf = fopen("data3out.txt","w");
e=fscanf(inf,"%d %d %d",&k,&n,&m);
fprintf (outf,"Output:\n\n k, m, n\n\n");
fprintf(outf," %d %d %d\n\n",k,m,n);
fprintf(outf,"\nSORRY: end of file - bye!\n\n");

while (e==1)
{
} sum=0;
for (int i=0;i<=n,i++)
then ;
if ((i%k)==0)
sum =sum +i;
e=fscanf(inf,"%d %d %d",&k,&n,&m);
}


}


when I make the changes it still dose not work . It not reading the input file at all.

2006-10-19 09:56:41 · update #1

4 answers

The things I can see are.

1.
You do not need to have the variable named 'then'. In fact, you should not have this.
Therefore, this line
int i, k, m, n, e,sum,total,then;
should now read
int i, k, m, n, e, sum, total;

2.
The lines
while (e==1)
{
}

will not help you. It is an infinite loop.
This is why it does not read the input file, as it does not get to that part of your program.
Remove the
}
and do not replace it anywhere.

3.
You do not need the statement
then ;
so remove this one as well.

Your code should now look like this. With '_' as spaces for indenting purposes. A space is at the end of the '_'s.

#include
main()
{
_ int i, k, m, n, e, sum, total;

_ FILE *inf,*outf;
_ inf = fopen("data3.dat","r");
_ outf = fopen("data3out.txt","w");
_ e=fscanf(inf,"%d %d %d",&k,&n,&m);
_ fprintf (outf,"Output:\n\n k, m, n\n\n");
_ fprintf(outf," %d %d %d\n\n",k,m,n);
_ fprintf(outf,"\nSORRY: end of file - bye!\n\n");

_ while (e==1)
_ {
___ sum=0;
___ for (int i=0;i<=n,i++)
_____ if ((i%k)==0)
_______ sum =sum +i;
___ e=fscanf(inf,"%d %d %d",&k,&n,&m);
_ }

}

Note: I have not tested this.

2006-10-24 03:08:05 · answer #1 · answered by Mark aka jack573 7 · 0 0

What's your question? As it stands, it looks like you want someone to write the entire program for you. If so, sorry, but that's not the way Yahoo! Answers works.

2016-05-21 23:41:33 · answer #2 · answered by Anonymous · 0 1

int k,m,n, total=0
enter K
enter m
enter n

for loop(int i =0; i {
m+1;
if ((m%k)==0) then
total = total + m
}
print total


I know i screwed up the syntax but you get the idea, I can never remember syntax

2006-10-18 06:09:43 · answer #3 · answered by tru_story 4 · 0 0

//Include ur header files
// l just give logic in a overview.

int sum = 0;
scanf("%d",&k)
scanf("%d",&m);
scanf("%d",&n)
for(int i=m;i<=n;i++){
if(i%k==0){
sum = sum + i;
}
}
printf(sum);

I think wat i have given is right.

2006-10-18 06:11:34 · answer #4 · answered by Sudha P 2 · 0 0

fedest.com, questions and answers