In above c program, we ask the user to input 2 integer value and store it in variables start and end. If value of start is greater than the value of end, then we swap the values.
For loop counter is initialized to start, and for loop executes until value of count is less than or equal to end. For each iteration of the for loop, count value increments by 1.
Inside for loop, for every value of count, we check if its not perfectly divisible by 2. If true, it’s a Odd number and we output that number to the console window.
Source Code: C Program To Find Odd Numbers Between Range using For Loop
#include
int main()
{
int start, end, temp, count;
printf("Enter start and end value, to find odd numbers\n");
scanf("%d%d", &start, &end);
if(start > end)
{
temp = start;
start = end;
end = temp;
}
printf("Odd numbers between %d and %d are\n", start, end);
for(count = start; count <= end; count++)
{
if(count % 2 != 0)
printf("%d\n", count);
}
return 0;
}
Output 1 Enter start and end value, to find odd numbers 40 60 Odd numbers between 40 and 60 are 41 43 45 47 49 51 53 55 57 59
Output 2 Enter start and end value, to find odd numbers 60 40 Odd numbers between 40 and 60 are 41 43 45 47 49 51 53 55 57 59
In above c program, we ask the user to input 2 integer value and store it in variables start and end. For loop counter is initialized to start and for loop executes until loop counter is less than or equal to value of end. For each iteration of the for loop, loop counter value increments by 1.
Inside for loop, for every value of count, we check if its perfectly divisible by 2. If true, it’s a Even number and we output that number to the console window.
Source Code: C Program to Find Even Numbers Between Range using For Loop
#include
int main()
{
int start, end, count, temp;
printf("Enter start value and end value to generate Even no's\n");
scanf("%d%d", &start, &end);
if(start > end)
{
temp = start;
start = end;
end = temp;
}
printf("Even numbers between %d and %d are:\n", start, end);
for(count = start; count <= end; count++)
{
if(count % 2 == 0)
{
printf("%d\n", count);
}
}
return 0;
}
Output 1 Enter start value and end value to generate Even no’s 10 20 Even numbers between 10 and 20 are: 10 12 14 16 18 20
Output 2 Enter start value and end value to generate Even no’s 50 40 Even numbers between 40 and 50 are: 40 42 44 46 48 50
According to a study, the approximate level of intelligence of a person can be calculated using the following formula:
i = 2 + ( y + 0.5x )
Write a C program that will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps 0.5.
1. Problem statement snippet: “..y varies from 1 to 6”. That means, we can accomplish it using a looping statement. So we can take a for loop and initialize variable y to 1 and iterate the for loop until y is less than or equal to 6.
2. Problem statement snippet: “..for each value of y, x varies from”. That means we should write a nested for loop here.
3. Problem statement snippet: “..for each value of y, x varies from 5.5 to 12.5 in steps 0.5”. That means, we need to write a nested for loop and initialize x value to 5.5 and iterate through the for loop until x value is less than or equal to 12.5 And for each iteration of the inner for loop we need to increment the value of x by 0.5
Video Tutorial: C Program To Calculate Approximate Level of Intelligence of a Person
Source Code: C Program To Calculate Approximate Level of Intelligence of a Person
#include
int main()
{
int y, count = 1;
float i, x;
for(y = 1; y <= 6; y++)
{
for(x = 5.5; x <= 12.5; x += 0.5)
{
i = 2 + (y + 0.5 * x);
printf("%d. i = %0.2f,\t x = %0.2f,\t y = %d\n", count++,i, x, y);
}
}
return 0;
}
Output:
1. i = 5.75, x = 5.50, y = 1
2. i = 6.00, x = 6.00, y = 1
3. i = 6.25, x = 6.50, y = 1
4. i = 6.50, x = 7.00, y = 1
5. i = 6.75, x = 7.50, y = 1
6. i = 7.00, x = 8.00, y = 1
7. i = 7.25, x = 8.50, y = 1
8. i = 7.50, x = 9.00, y = 1
9. i = 7.75, x = 9.50, y = 1
10. i = 8.00, x = 10.00, y = 1
11. i = 8.25, x = 10.50, y = 1
12. i = 8.50, x = 11.00, y = 1
13. i = 8.75, x = 11.50, y = 1
14. i = 9.00, x = 12.00, y = 1
15. i = 9.25, x = 12.50, y = 1
16. i = 6.75, x = 5.50, y = 2
17. i = 7.00, x = 6.00, y = 2
18. i = 7.25, x = 6.50, y = 2
19. i = 7.50, x = 7.00, y = 2
20. i = 7.75, x = 7.50, y = 2
21. i = 8.00, x = 8.00, y = 2
22. i = 8.25, x = 8.50, y = 2
23. i = 8.50, x = 9.00, y = 2
24. i = 8.75, x = 9.50, y = 2
25. i = 9.00, x = 10.00, y = 2
26. i = 9.25, x = 10.50, y = 2
27. i = 9.50, x = 11.00, y = 2
28. i = 9.75, x = 11.50, y = 2
29. i = 10.00, x = 12.00, y = 2
30. i = 10.25, x = 12.50, y = 2
31. i = 7.75, x = 5.50, y = 3
32. i = 8.00, x = 6.00, y = 3
33. i = 8.25, x = 6.50, y = 3
34. i = 8.50, x = 7.00, y = 3
35. i = 8.75, x = 7.50, y = 3
36. i = 9.00, x = 8.00, y = 3
37. i = 9.25, x = 8.50, y = 3
38. i = 9.50, x = 9.00, y = 3
39. i = 9.75, x = 9.50, y = 3
40. i = 10.00, x = 10.00, y = 3
41. i = 10.25, x = 10.50, y = 3
42. i = 10.50, x = 11.00, y = 3
43. i = 10.75, x = 11.50, y = 3
44. i = 11.00, x = 12.00, y = 3
45. i = 11.25, x = 12.50, y = 3
46. i = 8.75, x = 5.50, y = 4
47. i = 9.00, x = 6.00, y = 4
48. i = 9.25, x = 6.50, y = 4
49. i = 9.50, x = 7.00, y = 4
50. i = 9.75, x = 7.50, y = 4
51. i = 10.00, x = 8.00, y = 4
52. i = 10.25, x = 8.50, y = 4
53. i = 10.50, x = 9.00, y = 4
54. i = 10.75, x = 9.50, y = 4
55. i = 11.00, x = 10.00, y = 4
56. i = 11.25, x = 10.50, y = 4
57. i = 11.50, x = 11.00, y = 4
58. i = 11.75, x = 11.50, y = 4
59. i = 12.00, x = 12.00, y = 4
60. i = 12.25, x = 12.50, y = 4
61. i = 9.75, x = 5.50, y = 5
62. i = 10.00, x = 6.00, y = 5
63. i = 10.25, x = 6.50, y = 5
64. i = 10.50, x = 7.00, y = 5
65. i = 10.75, x = 7.50, y = 5
66. i = 11.00, x = 8.00, y = 5
67. i = 11.25, x = 8.50, y = 5
68. i = 11.50, x = 9.00, y = 5
69. i = 11.75, x = 9.50, y = 5
70. i = 12.00, x = 10.00, y = 5
71. i = 12.25, x = 10.50, y = 5
72. i = 12.50, x = 11.00, y = 5
73. i = 12.75, x = 11.50, y = 5
74. i = 13.00, x = 12.00, y = 5
75. i = 13.25, x = 12.50, y = 5
76. i = 10.75, x = 5.50, y = 6
77. i = 11.00, x = 6.00, y = 6
78. i = 11.25, x = 6.50, y = 6
79. i = 11.50, x = 7.00, y = 6
80. i = 11.75, x = 7.50, y = 6
81. i = 12.00, x = 8.00, y = 6
82. i = 12.25, x = 8.50, y = 6
83. i = 12.50, x = 9.00, y = 6
84. i = 12.75, x = 9.50, y = 6
85. i = 13.00, x = 10.00, y = 6
86. i = 13.25, x = 10.50, y = 6
87. i = 13.50, x = 11.00, y = 6
88. i = 13.75, x = 11.50, y = 6
89. i = 14.00, x = 12.00, y = 6
90. i = 14.25, x = 12.50, y = 6
Note: Inner for loop executes from 5.5 to 12.5 with a 0.5 step increment for each loop. So its 15 iterations. Outer for loop executes 6 times. So total iterations equals 15 x 6 = 90 times. So 90 values will be printed to the console window.
First Thing First: What Is Fibonacci Series ? Fibonacci Series is a series of numbers where the first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. Its recurrence relation is given by Fn = Fn-1 + Fn-2.
Below are a series of Fibonacci numbers(10 numbers): 0 1 1 2 3 5 8 13 21 34
Source Code: C Program To Generate Fibonacci Series using For Loop
#include
int main()
{
int n1 = 0, n2 = 1, n3, count, num;
printf("Enter the number of terms to be printed\n");
scanf("%d", &num);
printf("\n%d\n%d\n", n1, n2);
for(count = 3; count <= num; count++)
{
n3 = n1 + n2;
printf("%d\n", n3);
n1 = n2;
n2 = n3;
}
return 0;
}
Output: Enter the number of terms to be printed 10
0 1 1 2 3 5 8 13 21 34
Logic To Generate Fibonacci Series using For Loop
We know that the first 2 digits in Fibonacci series are 0 and 1. So we directly initialize n1 and n2 to 0 and 1 respectively and print that out before getting into For loop logic. Since we’ve already printed two Fibonacci numbers, we assign 3 to loop control variable count and iterate through the for loop till count is less than or equal to the user entered number.
Inside For loop As per definition of Fibonacci series: “..each subsequent number is the sum of the previous two.” So we add n1 and n2 and assign the result to n3 and display the value of n3 to the console.
Next, we step forward to get next Fibonacci number in the series, so we step forward by assigning n2 value to n1 and n3 value to n2.
For loop keeps repeating above steps until the count is less than or equal to the user entered number. If the user entered limit/count is 10, then the loop executes 8 times(as we already printed the first two numbers in the Fibonacci series, that is, 0 and 1).
Lets write a C program to print the multiplication table of the number entered by the user. The table should get displayed in the following form:
Example: Multiplication table of 29 29 x 1 = 29 29 x 2 = 58 29 x 3 = 87 29 x 4 = 116 29 x 5 = 145 29 x 6 = 174 29 x 7 = 203 29 x 8 = 232 29 x 9 = 261 29 x 10 = 290
Source Code: C Program To Print Multiplication Table Using For Loop
#include
int main()
{
int num, count;
printf("Enter a number\n");
scanf("%d", &num);
printf("Multiplication table for %d is:\n", num);
for(count = 1; count <= 10; count++)
{
printf("%d x %d = %d\n", num, count, (num*count));
}
return 0;
}
Output 1: Enter a number 5 Multiplication table for 5 is: 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Output 2: Enter a number 10 Multiplication table for 10 is: 10 x 1 = 10 10 x 2 = 20 10 x 3 = 30 10 x 4 = 40 10 x 5 = 50 10 x 6 = 60 10 x 7 = 70 10 x 8 = 80 10 x 9 = 90 10 x 10 = 100
Output 3: Enter a number 7 Multiplication table for 7 is: 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
Output 4: Enter a number 6 Multiplication table for 6 is: 6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54 6 x 10 = 60
Logic: Multiplication Table
We ask the user to enter a number. We initialize for loop counter to 1 and iterate through the loop until loop counter is less than or equal to 10. Inside for loop we multiply the user entered number with the loop counter variable to get the result of multiplication table.