Question

(use only variables, expression, conditional statement and loop of c programming)

***C programming**

int sum_of_cubes(int n) {

}

int quadrant(int x, int y) {

}

3.1 int sum_of_cubes(int n) This function should return the sum of the cubes of the first n integers. For example, if n is 4,

int num_occurrences_of_digit(long num, int digit) {
return 0;
}

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

// C program to create and test functions

#include <stdio.h>

#include <stdlib.h>

// function to calculate the sum of cubes of the first n integers

int sum_of_cubes(int n)

{

       if(n < 0) // if n is negative return -1(error)

             return -1;

       int sum = 0;

       int i;

       // loop to calculate the sum of cubes of first n integers

       for(i=1;i<=n;i++)

             sum += (i*i*i);

       return sum; // return the sum

}

// function to return the quadrant to which the point (x,y) belongs

int quadrant(int x, int y)

{

       if((x == 0) || (y == 0)) // if x or y is 0 return -1 (error)

             return -1;

       else if((x > 0) && (y > 0)) // both positive first quadrant

             return 1;

       else if((x > 0) && (y < 0)) // x positive y negative second quadrant

             return 4;

       else if((x < 0) && (y < 0)) // both negative third quadrant

             return 3;

       else // x negative y positive fourth quadrant

             return 2;

}

// function to return the number of occurrence of digit in num

int num_occurrence_of_digit(long num, int digit)

{

       if(digit < 0) //if digit < 0 return -1 (error)

             return -1;

       int count = 0;

       num = abs(num); // get the absolute value of num

       // loop to calculate the occurrence of digit in num

       while(num != 0)

       {

             int d = num%10; // get the last digit

             if(d == digit) // check if last digit is digit we want to search

                    count++; // increment the count

             num = num/10; // remove the last digit from num

       }

       return count; // return number of occurrence

}

int main(void) {

       // test the functions

       printf("\nsum_of_cubes(4) : %d ",sum_of_cubes(4));

       printf("\nsum_of_cubes(0) : %d ",sum_of_cubes(0));

       printf("\nsum_of_cubes(-2) : %d ",sum_of_cubes(-2));

       printf("\nquadrant(0,2) : %d ",quadrant(0,2));

       printf("\nquadrant(2,2) : %d ",quadrant(2,2));

       printf("\nquadrant(2,-2) : %d ",quadrant(2,-2));

       printf("\nquadrant(-2,2) : %d ",quadrant(-2,2));

       printf("\nquadrant(-2,-2) : %d ",quadrant(-2,-2));

       printf("\nnum_occurrence_digit(20162019,2) : %d ",num_occurrence_of_digit(20162019,2));

       printf("\nnum_occurrence_digit(-20162019,2) : %d ",num_occurrence_of_digit(-20162019,2));

       printf("\nnum_occurrence_digit(-20162019,-1) : %d ",num_occurrence_of_digit(-20162019,-1));

               return 0;

}

//end of program

Output:

sum_of_cubes (4) : 100 sum_of_cubes (0) : 0 sum_of_cubes (-2): -1 quadrant(0, 2) : -1 quadrant(2,2) : 1 quadrant(2,-2) : 4 qu

Add a comment
Know the answer?
Add Answer to:
(use only variables, expression, conditional statement and loop of c programming) ***C programming** int sum_of_cubes(int n)...
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
  • Part 1 – Data Encryption Data Encryption is the process of transforming data, using a cipher,...

    Part 1 – Data Encryption Data Encryption is the process of transforming data, using a cipher, to make it unreadable to anyone except those possessing special knowledge. In this problem you will implement a simple encryption technique on data that comprises of non-negative integers that have at least six digits with no leading zeroes. In order to encrypt the number the following functions are to be implemented: void input(int *num); int add4(int num); int shift(int num); void printOutput(int encryptNum, int...

  • c++ problem Write a recursive method int mul(int a, int b) that computes the product of...

    c++ problem Write a recursive method int mul(int a, int b) that computes the product of two nonnegative integers a and b. You are not allowed to use multiplication () Hint: use addition (+) and recursion. Write a method evenDigits that accepts an integer parameter n and that returns the integer formed by removing the odd digits from n. The following table shows several calls and their expected return values: 1- 2- Call Valued Returned evenDigits (8342116); 8426 evenDigits(4109); 4...

  • java programming. One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter...

    java programming. One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter n contains only odd digits (1, 3, 5, 7, or 9), and returns false otherwise. Zero is counted as an even digit whenever it appears inside the number. Remember that for a positive integer n, the expression (n % 10) gives its last digit, and the expression (n / 10) gives its other digits. Correct answer true false false false 357199 7540573 97531000

  • Write the definitions of the member functions of the class integerManipulation not given in Example 10-11....

    Write the definitions of the member functions of the class integerManipulation not given in Example 10-11. Also, add the following operations to this class: Split the number into blocks of n-digit numbers starting from right to left and find the sum of these n-digit numbers. (Note that the last block may not have ndigits. If needed add additional instance variables.) Determine the number of zeroes. Determine the number of even digits. Determine the number of odd digits Also, write a...

  • Write a program in C++ that implements a function // Precondition: num >= 0 int rotateUp(int...

    Write a program in C++ that implements a function // Precondition: num >= 0 int rotateUp(int num); which produces a new integer with each digit replaced with (digit+1), and 9 replaced with 0, because we don’t have digit 10: rotateUp(5) == 6 rotateUp(17) == 28 rotateUp(126) == 237 rotateUp(5091) == 6102 rotateUp(9911) == 22 // technically, more like 0022 The program should have a main function that tests the function. Example Output: Enter a number: 901 12

  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

  • C programming KAM5 Write a function unsigned long long int factorial(int num) to compute the factorial...

    C programming KAM5 Write a function unsigned long long int factorial(int num) to compute the factorial of a number. The factorial of a number is given by n! = 1*2* ... *n So 1! = 1, 2! = 1*2, 3! = 1*2*3 ... For example: Test Result printf("%llu\n", factorial(5)); 120 printf("%lu\n", factorial(20)); 2432902008176640000

  • Hello, I am having trouble with this C++ programming assignment. If someone could write the code...

    Hello, I am having trouble with this C++ programming assignment. If someone could write the code or at least part of it that would help me, because every code I try has errors. Using if Statements, Loops and Nested Loops • Scanning Characters in a String Using a Loop • Performing Character Arithmetic In project, you will write an interactive program that, counts the number of digits in a non-negative integer, factorizes the integer into powers of ten and its...

  • Need help with these array problems. int countAllPunctuation( const string array[ ], int n ); Return...

    Need help with these array problems. int countAllPunctuation( const string array[ ], int n ); Return the total number of punctuation symbols found in all the elements of the passed array argument. For the purpose of this function, the characters '.' , ',', '!', ';', ''', '-', '/', ':', '?', '"' count as punctuation symbols (that is, period, comma, exclamation mark, semicolon, apostrophe, dash, slash, colon, question mark, and double quote). Return -1 if n <= 0. For example, for...

  • Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int)

    please do in c++Q4. Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int) that when passed a non-negative integer, returns the number of occurrences of the digit 7 in the number. So, for example: count7s(717) → 2 count75(7) →1 count7s(123) → 0 Q5. Write a C++ function of the form string mirrorEnds(string)that when given a string, looks for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or...

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
ADVERTISEMENT