I am still having trouble with this program. So far, the while function works perfectly for positive and negative integers. The for function works for all negative integer values, but not for positive ones.
Code:
#include<stdio.h>
#include<stdlib.h>
int sumFor(int daNumba);
int sumWhile(int daNumba);
/* Main function */
int main()
{
/* Declare variables */
int daNumba, startNumba, ansFor, ansWhile;
/* Display welcome message */
printf("\nWelcome to sumTimes2.\n");
/* Message indicating what the program will do */
printf("\nThis program will sum the integers between the one you enter and\n");
printf("the integer that is twice that value.\n");
/* Ask the user to enter and receive an integer */
printf("\nPlease enter your integer: ", daNumba);
scanf("%d", &daNumba);
/* sumFor program */
int sumFor(int daNumba)
{
int ansFor=0, startNumba=daNumba;
for( ; startNumba <= (daNumba*2); )
{
ansFor += startNumba++;
}
for( ; startNumba >= (daNumba*2); )
{
ansFor += startNumba--;
}
return ansFor;
}
/* sumWhile program */
int sumWhile(int daNumba)
{
int ansWhile=0, startNumba=daNumba;
while(daNumba >0 && startNumba <= (daNumba*2))
{
ansWhile += startNumba++;
}
while(daNumba <= 0 && startNumba >= (daNumba*2))
{
ansWhile += startNumba--;
}
return ansWhile;
}
/* Display test statistics */
printf("\nFor says the sum is %d\n", sumFor(daNumba));
printf("While says the sum is %d\n", sumWhile(daNumba));
/* Display ending thank you message */
printf("\nThank you for using sumTimes2. Bye!\n\n");
/* Rieturn 0 for the compiler */
return 0;
}