0% found this document useful (0 votes)
2 views

All_C_Programs (1)

The document contains several C programs demonstrating the use of for loops. These programs include printing odd and even numbers, calculating the sum of the first ten natural numbers, finding the factorial of a number, printing the alphabet from A to Z, displaying a multiplication table, and showing values from 1 to 10. Each program is provided with its respective code snippet.

Uploaded by

ahmad nadeem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

All_C_Programs (1)

The document contains several C programs demonstrating the use of for loops. These programs include printing odd and even numbers, calculating the sum of the first ten natural numbers, finding the factorial of a number, printing the alphabet from A to Z, displaying a multiplication table, and showing values from 1 to 10. Each program is provided with its respective code snippet.

Uploaded by

ahmad nadeem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C Programs

Write a program to print odd numbers from 1 to 9 using a for loop.

#include <stdio.h>
int main() {
for (int i = 1; i <= 9; i += 2) {
printf("%d\n", i);
}
return 0;
}

Write a program to print even numbers from 2 to 20 using a for loop.

#include <stdio.h>
int main() {
for (int i = 2; i <= 20; i += 2) {
printf("%d\n", i);
}
return 0;
}

Write a program to print the sum of the first ten natural numbers using a for loop.

#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
printf("Sum: %d\n", sum);
return 0;
}

Write a program that prints the factorial of a number using a for loop.

#include <stdio.h>
int main() {
int num = 5, fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
printf("Factorial of %d is %d\n", num, fact);
return 0;
}

Write a program using for loop to print the alphabets from A to Z.

#include <stdio.h>
int main() {
for (char ch = 'A'; ch <= 'Z'; ch++) {
printf("%c ", ch);
}
printf("\n");
return 0;
}

Write a program to print a table of numbers using a for loop.

#include <stdio.h>
int main() {
int num = 5;
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}

Write a program that displays the values from 1 - 10 on the computer screen.

#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}

You might also like