Question

In: Computer Science

in C please! Write a code that asks user for input N and calculates the sum...

in C please!

Write a code that asks user for input N and calculates the sum of the first N odd integers. There should be a "pure" method that calculates the sum separately that Main method should call when printing the output.

Solutions

Expert Solution

Above code is to print the sum of first n odd integers

To explain this more clearly, below are the steps:

Step 1: Declare the variable N

N is the nth value user will enter from console

int n;

  

Step 2: Take input from user in console

Input is the N number , which determine how many odd integers we have to add .

Input is taken with the help of predefined function in C, scanf()

printf("Enter Nth number: ");
scanf("%d" , &n);

Step 3: Now , call the method pure() with one argument passed which is n. It will return the calculated sum of first n odd integers and return to the main function and will print in the console.

In main function , value will be printed in console using predefined function printf()

printf("Sum of first N odd integers is: %d", pure(n));

We have called the function, in the print statement and result will be displayed directly in the console
  

Step 4: Inside function pure()

As the function is called, control moves to the function defination

It accepts the n value as argument, and store in variable num

Also, declare two variable with integer datatype; sum and oddIncrementor

sum initialized to 0, it will continously add the n odd integers and store in it

oddIncremetor initialized to 1, it will increment the value of integer by 2,

making odd digits, eg ; 1, 3 ,5 7

int pure(int num){
int sum=0;
int oddIncrementor=1;

Step 5:

A for loop is used to calculate N odd integers sum

Initialize the value of i to 0

Condition: i<num

that means, ;loop will iterate num times

Example: num is 5, according to condition, loop will executed 5 times.

i is incremented 1

If the condition is true, control moves inside the loop

It will add the oddincrementor value in the sum variable

oddincrementor value is incremeted by 2, so that next odd integer is stored in the variable

for(int i=0;i<num;i++){
sum+=oddIncrementor;
oddIncrementor+=2;
}

As the condition becomes false, control will exit out of loop and moves to next immediate statement

Step 6: It will return the calculated total sum value to function call

return sum;


Step 7: Exit the control out of program

return 0;

_____________________________________

Please find the code below in C language:

#include <stdio.h>

int pure(int num){
int sum=0;
int oddIncrementor=1;
for(int i=0;i<num;i++){
sum+=oddIncrementor;
oddIncrementor+=2;
}
return sum;
}
int main()
{
int n;
printf("Enter Nth number: ");
scanf("%d" , &n);
printf("Sum of first N odd integers is: %d", pure(n));
return 0;
}

Please find attached Code screenshot:

Please find attached output screenhot:


Related Solutions

Write a java program that asks the user for a positive integer N and then calculates...
Write a java program that asks the user for a positive integer N and then calculates a new value for N based on whether it is even or odd: if N is even, the new N is N/2. (Use integer division.) if N is odd, the new N is 3*N + 1. Repeat with each new N until you reach the value 1. For example, say the initial value is 12. Then the successive values are: 12 (even, next value...
Please write a C++ code that inputs a CSV file, asks the user if they'd like...
Please write a C++ code that inputs a CSV file, asks the user if they'd like to remove certain words from the file, and removes the words from the file. Please note that the CSV file has two columns, the first with the words, and the second with a count of how many times each word appears.
Please code C# 10. Write a program that allows a user to input names and corresponding...
Please code C# 10. Write a program that allows a user to input names and corresponding heights (assumed to be in inches). The user can enter an indefinite number of names and heights. After each entry, prompt the user whether they want to continue. If the user enters true, ask for the next name and height. If the user enters false, display the name of the tallest individual and their height. Sample run: “Name?” James “Height?” 50 “Continue?” True “Name?”...
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Create a program that asks the user to input three numbers and computes their sum. This...
Create a program that asks the user to input three numbers and computes their sum. This sounds simple, but there's a constraint. You should only use two variables and use combined statements. You can use the output below as a guide. Please enter the first number: 4 Please enter the second number: 2 Please enter the third number: 9 The sum of the three numbers is: 15.
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
Asks a user to enter n values, then calculates and returns the minimum, maximum, total, and...
Asks a user to enter n values, then calculates and returns the minimum, maximum, total, and average of those values. Use: minimum, maximum, total, average = statistics(n) ------------------------------------------------------- Parameters: n - number of values to process (int > 0) Returns: minimum - smallest of n values (float) maximum - largest of n values (float) total - total of n values (float) average - average of n values (float) ---------------------------------------
Write a C++ program using separate void which asks the user to input side of a...
Write a C++ program using separate void which asks the user to input side of a square, radius of a circle , height and base of a triangle and finds the area of squares, circles and triangles. Then using main function display the area of square, circle and triangle
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT