Question

Hi, I'm trying to write C code that will print the odd abundant numbers between 1...

Hi, I'm trying to write C code that will print the odd abundant numbers between 1 and 5000 and I've written a program for it but it doesn't work. If you would be able to help me fix it or point out where I went wrong that would be awesome.

# include <stdio.h>

int main () {
  
int getSum(int n)
int i, n, j, sum = 0;
  
/* print initial statement to give context of program*/
  
printf(" The odd abundant numbers between 1 and 5000 are: \n");
scanf("%d", &n);
  
/* first declare a range for the integer i*/
  
for (i=1; 1<=n; i++); {

/* determine each factor for every integer i*/

for (j = 1; j <= i/2; j++) {
if (i % j == 0) {
sum = sum + j;
}
}
/* print abundant number alone */
  
if (sum > i)
  
printf("%d ", n);

sum = 0;
}
/* determine whether integer i is odd*/
  
if (i % 2 != 0) {
  
/* print all integers that satisfy these requirements*/
  
printf("%d", n); }
  
printf("\n");

return 0;
}

  
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your code was not properly formed, and there were alot of errors, So I recreated the code. Below is the working code

#include<stdio.h>
#include<math.h>
int checkIfNumberAbundant(int num)
{
int sum = 0;
int i;
for (i=1; i<=sqrt(num); i++)
{
if (num%i==0)
{
if (num/i == i)
sum = sum + i;
  
else
{
sum = sum + i;
sum = sum + (num / i);
}
}
}
sum = sum - num;
return (sum > num);
}
  
void main()
{
int count = 0;
int i = 0;
for(i = 0; i < 5000;i++) {
if(i%2 != 0) {
if(checkIfNumberAbundant(i) > 0) {
printf("%d ",i);
count++;
}
}
}
printf("Total number of abundant numbers less than 5000 is %d",count);
}

Add a comment
Know the answer?
Add Answer to:
Hi, I'm trying to write C code that will print the odd abundant numbers between 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

  • I'm having trouble getting my program to print shapes For example: Which shape (L-line, T-triangle, R-rectangle):...

    I'm having trouble getting my program to print shapes For example: Which shape (L-line, T-triangle, R-rectangle): L Enter an integer length between 1 and 25: 13 ************* Which shape (L-line, T-triangle, R-rectangle): t Enter an integer base length between 3 and 25: 5 * ** *** **** ***** Which shape (L-line, T-triangle, R-rectangle): r Enter an integer width and height between 2 and 25: 4 5 **** **** **** **** **** Here's my code: #include <stdio.h> #include <ctype.h> const int...

  • i need flowchart for the following c code #include <stdio.h> int main() {     int n,...

    i need flowchart for the following c code #include <stdio.h> int main() {     int n, i, sum = 0;     printf("Enter an integer: ");     scanf("%d",&n);     i = 1;     while ( i <=n )     {         sum += i;         ++i;     }     printf("Sum = %d",sum);     return 0; }

  • The program I'd like to design is supposed to read in an integer k, and print...

    The program I'd like to design is supposed to read in an integer k, and print out the number of positive integers between 1 and 100,000 (inclusive) which have exactly k divisors. As an example, the number 24 has 8 divisors: 1, 2, 3, 4, 6, 8, 12, and 24. So far this is what I got int finddivisor(int *k) {     int x, j, m;     *k = m;     printf("Enter the target number of divisors:");     scanf("%d", &k);     for (int i =...

  • I am trying to add a string command to my code. what am i doing wrong?...

    I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) {    int i, j;    int rowA, colA, rowB, colB;    int A[10][10], B[10][10];    int sum[10][10];    char str1[10];    printf("This is a matrix calculator\n");    //read in size from user MATRIX A    printf("Enter in matrix A....\n");    printf("\t#row = ");    scanf("%d", &rowA);    printf("\t#col = ");   ...

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • Hello, I need help with the following C code. This program asks the user to enter...

    Hello, I need help with the following C code. This program asks the user to enter three numbers and outputs the sum on the screen , all im trying to do is have the program reprompt the user for a number if anything other than a number is entered for each of the entries. I wanted to use do while for each of the functions but that did not work for example: Enter Number 1: 2 Enter Number 2: Hello...

  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

  • 1) (5 points) The following program is used to display numbers between two intervals include stdio.h...

    1) (5 points) The following program is used to display numbers between two intervals include stdio.h define true 1 define false 0 5 void prime (int low, int high) int i -o, flag-0 ; printf ("Prime numbers between %d and %d are: ", low, high); while (low <high) 10 flag false; 12 13 for (i 0; i <-low/2; ++i) - 15 16 17 if(low % ?--0) flagtrue: break; 19 20 21 if (flagtrue ) 23 24 25 26 27 28...

  • Hello, I am having trouble with a problem in my C language class. I am trying...

    Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct {     char name[MAX_SIZE1];     int scores[MAX_SIZE2]; } 3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT