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 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 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...
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
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.
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the 2 numbers. The difference between the 2 numbers (num1-num2) and (num2-num1) The value that is 305 more than the 1st number. The value that is 305 less than the 2nd number
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
Write a program that calculates the average of upto 100 English distances input by the user....
Write a program that calculates the average of upto 100 English distances input by the user. Create an array of objects of the Distance class, as in the ENGLARAY example in this chapter. To calculate the average, you can borrow the add_dist() member function from the ENGLCON example in Chapter 6. You’ll also need a member function that divides a Distance value by an integer. Here’s one possibility: void Distance::div_dist(Distance d2, int divisor) { float fltfeet = d2.feet + d2.inches/12.0;...
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference between the largest and the smallest. You should ask the user the number of integers s/he would like to enter (this will allow you to set the length of the array). Allow the user to input all the numbers and then store them in the array. Search the array for the largest number, then search for the smallest, and finally compute the calculation. Display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT