Posts

Showing posts with the label for loops in C

C Control Statements: For Loop Codes

1. Write a program to find the factorial of a given number #include <stdio.h> int main() {     int n, i;     long fact = 1;          printf("Enter the number: ");     scanf("%d", &n);          for (i = 1; i <= n; i++) {         fact = fact * i;     }          printf("The factorial of %d is %ld", n, fact);          return 0; } 2. Write a program to find the sum of first n natural number #include <stdio.h> int main() {     int n, sum = 0, i;          printf("Enter the number: ");     scanf("%d", &n);          for (i = 1; i <= n; i++) {         sum = sum + i;     }          printf("The sum of natural numbers up to %d = %d", n, sum);          return 0; } 3...