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

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...
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
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
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
Code in C ++ that asks the user for an integer and that prints whether or...
Code in C ++ that asks the user for an integer and that prints whether or not the number is divisible by three in the integers. If the number is not divisible by three, the program must write a message indicating what the remainder is obtained by dividing the number by three.
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in a number greater than or equal to zero and less than or equal to 100. If they do not you should alert them and end the program. Next, determine the letter grade associated with the number. For example, A is any grade between 90 and 100. Report the letter grade to the user.
Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT