Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Design at end of the loop. a while loop that sum up all odd numbers starting

ID: 3890881 • Letter: 1

Question

1. Design at end of the loop. a while loop that sum up all odd numbers starting from 17 to 2017. Display the value 2. Design a for loop that will allow you to enter integer) and determine the largest and smallest of the numbers. You need only five variables irn this task: N, a series of N numbers (when N is an user input number, maximum and minimum and count which will count how many times the is performed. Before beginning the loop, you need to initialize minimum to INT_MAX and maximum to INT_MIN. Then write the loop by starting from count 1 and countN, within the loop get the value for number, and check if number is greater than maximum, then update maxi mum with this value. Similarly for finding minimum, check if the number is less than the umber. You may like to use additional header file climits minimum then update minimum to the n to use INT MAX and INT MIN (Insert your code here) Alt Ctrl

Explanation / Answer

1) Design while loop that sums up all odd numbers from 17 to 2017. Display the value at the end of the loop.

To Add all odd numbers, run a loop starting from 17 to 2017 and add the number to sum(initialized to 0) variable only if the number is odd else ignore. To check the odd number do a modulo operator %2 and check if reminder is 1, then it is odd else even.

Code -

#include<stdio.h>

#include<conio.h>

int addOddNumbers(){

int i = 17, end = 2017;

int sum = 0;

while(i <= end){

if(i % 2 == 1){

sum = sum + i;

}

i++;

}

return sum;

}

int main(){

printf("sum of odd numbers from 17 to 2017 is : ");

printf("%d ", addOddNumbers());

getchar();

return 0;

}

2) For loop to get min and max

#include<stdio.h>

#include<conio.h>

#include<limits.h>

int main(){

int N = 0, count = 0, number = 0, max = INT_MIN, min = INT_MAX;

printf("Enter the value of N : ");

scanf("%d", &N);

for(count = 1; count <= N; count++){

printf(" Enter the %dth number : ", count);

scanf("%d", &number);

if(number > max){

max = number;

}

if(number < min){

min = number;

}

}

printf("Max = %d, Min = %d", max, min);

getchar();

return 0;

}