3 Looping Statements
a. To find the sum of first N natural numbers using While loop.
b. Check whether the number is palindrome or not using For loop
3. [Link] find the sum of first N natural numbers using
While loop.
#include <stdio.h>
int main() {
int N, sum = 0, i = 1;
printf("Enter the value of N: ");
scanf("%d", &N);
while (i <= N) {
sum = sum + count;
i++;
}
printf("The sum of the first %d natural numbers is: %d\n", N, sum);
return 0;
}
3. [Link] whether the number is palindrome or not
using For loop.
#include <stdio.h>
int main() {
int num, temp, rev, rem;
printf("Enter a number: ");
scanf("%d", &num);
temp = number;
for (rev=0; num != 0; num /= 10) {
rem = num % 10;
rev = rev * 10 + rem;
}
if (temp == rev) {
printf("%d is a palindrome number.\n", temp);
} else {
printf("%d is not a palindrome number.\n", temp);
}
return 0;
}