C Programs - For Loop and While Loop
Q1. Print numbers from 1 to 10
Using for loop:
for(int i=1; i<=10; i++) printf("%d ", i);
Using while loop:
int i=1; while(i<=10) { printf("%d ", i); i++; }
Q2. Print even numbers from 1 to 20
Using for loop:
for(int i=2; i<=20; i+=2) printf("%d ", i);
Using while loop:
int i=2; while(i<=20) { printf("%d ", i); i+=2; }
Q3. Print odd numbers from 1 to 20
Using for loop:
for(int i=1; i<=20; i+=2) printf("%d ", i);
Using while loop:
int i=1; while(i<=20) { printf("%d ", i); i+=2; }
Q4. Sum of first 10 natural numbers
Using for loop:
int sum=0; for(int i=1;i<=10;i++) sum+=i; printf("%d",sum);
Using while loop:
int i=1,sum=0; while(i<=10){sum+=i;i++;} printf("%d",sum);
Q5. Print multiplication table of n
Using for loop:
for(int i=1;i<=10;i++) printf("%d x %d = %d\n",n,i,n*i);
Using while loop:
int i=1; while(i<=10){printf("%d x %d = %d\n",n,i,n*i);i++;}
Q6. Print numbers from 10 to 1
Code provided earlier in chat.
Q7. Print squares of numbers from 1 to 10
Code provided earlier in chat.
Q8. Find factorial of a number
Code provided earlier in chat.
Q9. Count digits of a number
Code provided earlier in chat.
Q10. Reverse a number
Code provided earlier in chat.
Q11. Check if a number is palindrome
Code provided earlier in chat.
Q12. Find sum of digits of a number
Code provided earlier in chat.
Q13. Print Fibonacci series up to n terms
Code provided earlier in chat.
Q14. Find largest digit of a number
Code provided earlier in chat.
Q15. Print sum of odd numbers from 1 to 50
Code provided earlier in chat.
Q16. Print ASCII values of characters A to Z
Code provided earlier in chat.
Q17. Check if a number is Armstrong
Code provided earlier in chat.
Q18. Print product of digits of a number
Code provided earlier in chat.
Q19. Print first 10 natural numbers in reverse
Code provided earlier in chat.
Q20. Extra: Print cube of numbers 1 to 10 (added to make 20)
Code provided earlier in chat.