Subjective Assignment 05 Forloop Ans
Subjective Assignment 05 Forloop Ans
Example
Input
Solution -
#include <stdio.h>
int main()
{
int i, n;
/*
* Start loop counter from 1 (i=1) and go till n (i<=n)
* increment the loop count by 1 to get the next value.
* For each repetition print the value of i.
*/
for(i=1; i<=n; i++)
{
printf("%d\n", i);
}
return 0;
}
2. Write a C program to print all natural numbers in reverse from n to 1 using for
loop.
Example
Input
Input N: 10
Output
Solution -
#include <stdio.h>
int main()
{
int i, start;
/*
* Run loop from 'start' to 1 and
* decrement 1 in each iteration
*/
for(i=start; i>=1; i--)
{
printf("%d\n", i);
}
return 0;
}
3. Write a C program to print alphabets from a to z using for loop.
Example
Input
Output
Alphabets: a, b, c, ... , x, y, z
Solution -
#include <stdio.h>
int main()
{
char ch;
return 0;
}
4. Write a C program to print all even numbers from 1 to n using for loop.
Example
Input
#include <stdio.h>
int main()
{
int i, n;
/*
* Start loop counter from 1, increment it by 1,
* will iterate till n
*/
for(i=1; i<=n; i++)
{
/* Check even condition before printing */
if(i%2 == 0)
{
printf("%d\n", i);
}
}
return 0;
}
5. Write a C program to print all odd numbers from 1 to n using for loop.
Example
Input
Solution -
#include <stdio.h>
int main()
{
int i, n;
return 0;
}
6. Write a C program to find the sum of all natural numbers between 1 to n using
for loop.
Example
Input
Solution -
#include <stdio.h>
int main()
{
int i, n, sum=0;
return 0;
}
7. Write a C program to input number from user and find sum of all even numbers
between 1 to n.
Example
Input
Solution -
#include <stdio.h>
int main()
{
int i, n, sum=0;
return 0;
}
8. Write a C program to find sum of all odd numbers from 1 to n using for loop.
Example
Input
Solution -
#include <stdio.h>
int main()
{
int i, n, sum=0;
return 0;
}
9. Write a C program to input a number from user and print multiplication table of
the given number using for loop.
Example
Input
Input num: 5
Output
5*1 =5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Solution -
#include <stdio.h>
int main()
{
int i, num;
return 0;
}
10. Write a C program to input a number from user and count number of digits in
the given integer using loop.
Example
Input
Number of digits: 5
Solution -
#include <stdio.h>
int main()
{
long long num;
int count = 0;
return 0;
}