printf("\nNatural Numbers from %d to %d are:\n", min, max);
for(count = min; count <= max; count++)
{
printf("%d\n", count);
sum = sum + count;
}
printf("Sum of Natural Numbers from %d to %d is %d\n", min, max, sum);
return 0;
}
#include<stdio.h>
int main()
{
int min, max, temp, count, sum = 0;
printf("Enter 2 positive numbers\n");
scanf("%d%d", &min, &max);
if(min > max)
{
temp = min;
min = max;
max = temp;
}
printf("\nNatural Numbers from %d to %d are:\n", min, max);
for(count = min; count <= max; count++)
{
printf("%d\n", count);
sum = sum + count;
}
printf("Sum of Natural Numbers from %d to %d is %d\n", min, max, sum);
return 0;
}
Output 1: Enter 2 positive numbers 5 10
Natural Numbers from 5 to 10 are: 5 6 7 8 9 10 Sum of Natural Numbers from 5 to 10 is 45
Output 2: Enter 2 positive numbers 14 10
Natural Numbers from 10 to 14 are: 10 11 12 13 14 Sum of Natural Numbers from 10 to 14 is 60
Logic To Calculate Sum of Natural Numbers Between Range using For Loop
We ask the user to enter minimum and maximum number(i.e., the range) and we store it inside variable min and max. If value of min is greater than value of max, then we swap the values of min and max.
We initialize the variable count to min and iterate through the for loop until count value is less than or equal to value of max. We keep incrementing the value of count by 1 for each iteration of for loop.
Inside for loop we add the value of count to previous value of sum and once the control exits the for loop we display the value present in variable sum.
printf("Natural numbers from %d to %d are:\n", min, max);
for(count = min; count <= max; count++)
{
printf("%d\n", count);
}
return 0;
}
#include<stdio.h>
int main()
{
int min, max, temp, count;
printf("Enter 2 positive numbers\n");
scanf("%d%d", &min, &max);
if(min > max)
{
temp = min;
min = max;
max = temp;
}
printf("Natural numbers from %d to %d are:\n", min, max);
for(count = min; count <= max; count++)
{
printf("%d\n", count);
}
return 0;
}
Output 1: Enter 2 positive numbers 10 15 Natural numbers from 10 to 15 are: 10 11 12 13 14 15
Output 2: Enter 2 positive numbers 15 10 Natural numbers from 10 to 15 are: 10 11 12 13 14 15
Logic To Print Natural Numbers Between Two Numbers using for loop
We ask the user to enter 2 numbers, and store it inside variables min and max. For loop keeps iterating till min is less than or equal to max. We assign the value of min to count and keep incrementing the value of count by 1 for each iteration of the for loop. Once value of count is greater than value of max, control exits for loop.
Today lets learn some very important input types of HTML5:
datetime datetime-local date time week month
datetime input type is supported by Opera browser, but as of now(2013), Google Chrome doesn’t support it. All other input date time related input types are supported by Google Chrome.
Note: We can specify value, min, max and step attributes for all these input types. And the format for it is as shown below:
dateYYYY-MM-DD default step 60 seconds
timeHH:MM:SS.FF default step 1 Day (SS.FF means seconds and fractional seconds)
datetimeYYYY-MM-DDTHH:MM:SS.FFZ default step 60 seconds (T and Z means the TimeZone, these alphabets/characters must be literally present in the format)
datetime-localYYYY-MM-DDTHH:MM:SS.FF default step 60 seconds (albhabet/character/letter T must be literally present in the format)
weekYYYY-WNN default step of 1 week (letter W means the week, this alphabet/characters must be literally present in the format)
Browsers which support range input type know that the field is used for entering a number with in a given range.
User Interface(UI) enhancement wise, you may get a slider control to select a range of number. You can even use min, max and step attributes to provide additional control for the field – for setting the range.
Here we have 2 input fields of type range and number respectively. Both having minimum allowed value as 2 and maximum allowed value as 10, and a step of 0.2 (fractional value). The number input field is taken for the sake of illustrating the value selected by the user using the range input field – by using(sliding) the slider.
JavaScript file myScript.js
1
2
3
4
5
function fetch()
{
var get = document.getElementById("get").value;
document.getElementById("put").value = get;
}
function fetch()
{
var get = document.getElementById("get").value;
document.getElementById("put").value = get;
}
Here we fetch the value selected by the user using the slider and assign the value to the number input field.
Note: Browsers which do not support range input type, simply fall back to text input type. However, you need to validate the user entered data at server-side for more security. Client side validation is done mainly for the sake of creating the illustration of speed of validating the data and for a better user experience.
number input type indicates that the input field is used for entering numbers.
Advantages of using number input type: 1. Browsers which support HTML5, provides useful user interface enhancement by providing up and down arrow/tickers to increment and decrement the number. 2. You get a customized keyboard which is optimized for numeric inputs on mobile devices which supports number input type. 3. Using attributes like min, max and step, you can have more control. You can specify the minimum, maximum values the field takes and also you can specify the step for incrementing and decrementing the number using arrow marks. 4. Basic validation to check if the user entered numeric value or not.
Note: Browsers which do not support number input type, simply fall back to text input type, so you can go ahead and implement number input type in your projects without worrying. It’s a good practice to even validate the code at server-side, since browsers can be easily tricked and wrong entries can be made using form elements.