Welcome to our website! We are adding new data every day, so please check back often for updates.
Posts

How to Write a C Program that Sums Odd Values

write a simple C program that reads 5 numbers and sums all odd values between them. basic concepts of C programming, such as variables, arrays, loops

In this program, we will show you how to write a simple C program that reads 5 numbers and sums all odd values between them. This program can help you practice some basic concepts of C programming, such as variables, arrays, loops, and conditional statements.

C Program

The program consists of three main steps:

  • Declare and initialize the variables and the array that will store the 5 numbers and the sum of odd values.
  • Use a for loop to read the 5 numbers from the user input and store them in the array. Inside the loop, use an if statement to check if each number is odd and add it to the sum if it is.
  • Print the sum of odd values to the output.

Here is the code for each step:

#include <stdio.h>

int main()

{

    int numbers[5]; // an array to store the 5 numbers

    int sum = 0; // a variable to store the sum of odd values

    printf("Enter 5 numbers:\n");

    for (int i = 0; i < 5; i++) 

    {

        scanf("%d", &numbers[i]); 

        if (numbers[i] % 2 != 0) 

        {

            sum += numbers[i]; 

        }

    }

    printf("The sum of odd values is %d\n", sum); 

    return 0;

}

Output

This will prompt you to enter 5 numbers. For example, if you enter '1', '2', '3', '4', and '5', you will get this output:

Enter 5 numbers:

1

2

3

4

5

The sum of odd values is 9

You can try different inputs and see how the program works.

Explanation

  1. The program starts with including the 'stdio.h' header file, which provides input and output functions such as 'printf' and 'scanf'.
  2. Then, it defines the 'main' function, which is the entry point of the program.
  3. Inside the 'main' function, it declares and initializes two variables: 'numbers' and 'sum'. The variable 'numbers' is an array of 5 integers, which will store the 5 numbers entered by the user. The variable 'sum' is an integer, which will store the sum of odd values among the 5 numbers. It is initialized to 0.
  4. Next, it prints a message to the user to enter 5 numbers, using the 'printf' function.
  5. Then, it uses a for loop to read the 5 numbers from the user input and store them in the array. The loop variable 'i' goes from 0 to 4, which are the indices of the array. Inside the loop, it uses the 'scanf' function to read a number from the user input and store it in the array at the corresponding index. For example, if the user enters 1 as the first number, it will be stored in 'numbers[0]'.
  6. After storing a number in the array, it checks if the number is odd or not. To do this, it uses the modulo operator '%', which returns the remainder of dividing two numbers. For example, '1 % 2' returns 1, which means 1 is odd. If the number is odd, it adds it to the sum using the '+=' operator, which is equivalent to 'sum = sum + number'.
  7. After reading and processing all 5 numbers, it prints the sum of odd values using the 'printf' function.
  8. Finally, it returns 0 from the main function, which indicates that the program has executed successfully.

Conclusion

In this program, we have shown you how to write a C program that reads 5 numbers and sums all odd values between them. This program can help you learn some basic concepts of C programming, such as variables, arrays, loops, and conditional statements. We hope you enjoyed this tutorial and found it useful. If you have any questions or feedback, please leave a comment below.



We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!

Post a Comment

For more Tech content follow us on Social media
© Ruturaj Khansole. All rights reserved. Distributed by ASThemesWorld