Question

In: Computer Science

#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...

#include <stdio.h>

int sum(int n); //prototype

int main() {

    int number, result;

    printf("Enter a positive integer: ");

    scanf("%d", &number);

    result = sum(number);

    printf("sum = %d", result);

    return 0;

}

int sum(int n) {

    if (n != 0)

        return n + sum(n-1);

    else

        return n;

}

What does the code above do?

Solutions

Expert Solution

Implementation of given program;

Screenshots

Output:

The given code calculates the sum of all natural numbers from 1 to given number where number is given by the user. The given code uses recursive function to calculate the sum of all natural numbers.

i.e., For a given value of number, the program calculates sum as   

sum=(number*(number+1))/2

For example when number = 10

then sum = (10*11)/2

= 110/2

= 55

Iterations in program for number = 10

sum(10)

= 10+sum(9) [ sum(9) is a function call again ]

= 10+9+sum(8) [ sum(8) is a function call again ]

= 10+9+8+sun(7) [ sum(7) is a function call again ]

= 10+9+8+7+sum(6) [ sum(6) is a function call again ]

=  10+9+8+7+6+sum(5) [ sum(5) is a function call again ]

= 10+9+8+7+6+5+sum(4) [ sum(4) is a function call again ]

= 10+9+8+7+6+5+4+sum(3) [ sum(3) is a function call again ]

= 10+9+8+7+6+5+4+3+sum(2) [ sum(2) is a function call again ]

= 10+9+8+7+6+5+4+3+2+sum(1) [ sum(1) is a function call again ]

= 10+9+8+7+6+5+4+3+2+1+sum(0) [ sum((0) returns 0 ]

= 10+9+8+7+6+5+4+3+2+1+0

= 55

   


Related Solutions

#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j);...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j); // need to validate both being positive while (i != j) { i > j ? (i -= j) : (j -= i); } printf("GCD: %d\n", i); return 0; } The above code returns a GCD from two numbers entered. Use the above program to design a C program to compute of integer coefficients a and b such that gcd(i, j) = a xi...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print down from 2 to 0 by 0.1 increments\n"); float f = 2.0; while (f != 0) { printf("%0.1f\n",f); f = f - 0.1; } printf("============== Problem #2 =================\n"); printf("This should find that the average is 5.5\n"); int total_score = 55; int total_grades = 10; double avg = total_score/total_grades; printf("Average: %0.2f\n",avg); printf("============== Problem #3 =================\n"); printf("If the population increases by 2.5 people per second, how...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to...
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to my Command-Line Calculator (CLC)\n");    printf("Developer: Your name will come here\n");    printf("Version: 1\n");    printf("Date: Development data Will come here\n");    printf("----------------------------------------------------------\n\n");    //choice stores users input    char choice;    //to store numbers    int val1,val2;    //to store operator    char operation;    //flag which leths the loop iterate    //the loop will break once the flag is set to 0...
#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements...
#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements one by one for (i = 0; i < c; i++) { // Checking if the picked element is already printed for (j = 0; j <= i; j++) { // If current element is already there in the array, break from j loop if (arr[i] == arr[j]) { break; } } // If it is not printed earlier and is within 10-100, then...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        i++;    }...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number;...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number; Scanf (“%d”, & number); if (number % 2 ==0) { printf (“Even\n”); } else { printf(“Odd\n”); } Return 0; }
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT