#include <stdio.h>
int main() {
int i;
printf("The first 10 natural numbers are:\n");
for(i = 1; i <= 10; i++) {
printf("%d ", i);
}
return 0;
}
Computing the sum of the first 10 natural numbers:
#include <stdio.h>
int main() {
int i, sum = 0;
printf("The first 10 natural numbers are:\n");
for(i = 1; i <= 10; i++) {
printf("%d ", i);
sum += i;
}
printf("\nThe Sum is : %d", sum);
return 0;
}
Displaying n terms of natural numbers and their sum:
#include <stdio.h>
int main() {
int i, n, sum = 0;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("The first %d natural numbers are:\n", n);
for(i = 1; i <= n; i++) {
printf("%d ", i);
sum += i;
}
printf("\nThe Sum of Natural Number up to %d terms : %d", n, sum);
return 0;
}
Reading 10 numbers from the keyboard and finding their sum and average:
#include <stdio.h>
int main() {
int i, num;
double sum = 0;
printf("Input the 10 numbers:\n");
for(i = 1; i <= 10; i++) {
printf("Number-%d: ", i);
scanf("%d", &num);
sum += num;
}
printf("The sum of 10 numbers is: %.2lf\n", sum);
printf("The Average is: %.6lf", sum / 10);
return 0;
}
Displaying the cube of numbers up to an integer:
#include <stdio.h>
int main() {
int i, n;
printf("Input number of terms: ");
scanf("%d", &n);
printf("Expected Output:\n");
for(i = 1; i <= n; i++) {
printf("Number is: %d and cube of %d is: %d\n", i, i, i * i * i);
}
return 0;
}
Displaying the multiplication table for a given integer:
#include <stdio.h>
int main() {
int i, num;
printf("Input the number (Table to be calculated): ");
scanf("%d", &num);
printf("Expected Output:\n");
for(i = 1; i <= 10; i++) {
printf("%d X %d = %d\n", num, i, num * i);
}
return 0;
}
Displaying the multiplication table vertically from 1 to n:
#include <stdio.h>
int main() {
int i, j, n;
printf("Input upto the table number starting from 1: ");
scanf("%d", &n);
printf("Expected Output:\n");
for(i = 1; i <= 10; i++) {
for(j = 1; j <= n; j++) {
printf("%dx%d = %d, ", j, i, j * i);
}
printf("\n");
}
return 0;
}
Displaying n terms of odd natural numbers and their sum:
#include <stdio.h>
int main() {
int i, n, sum = 0;
printf("Input number of terms: ");
scanf("%d", &n);
printf("Expected Output:\n");
printf("The odd numbers are: ");
for(i = 1; i <= n; i++) {
printf("%d ", 2 * i - 1);
sum += 2 * i - 1;
}
printf("\nThe Sum of odd Natural Number up to %d terms: %d", n, sum);
return 0;
}
Displaying a right angle triangle pattern using asterisks:
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Displaying a right angle triangle pattern with numbers:
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
return 0;
}
Pattern like a right-angle triangle with a number repeating in a row:
#include <stdio.h>
int main() {
int n = 4; // Number of rows
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}
return 0;
}
Pattern like a right-angle triangle with numbers increased by 1:
#include <stdio.h>
int main() {
int n = 4; // Number of rows
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}
Pyramid pattern with numbers increased by 1:
#include <stdio.h>
int main() {
int n = 4; // Number of rows
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}
Pyramid pattern with an asterisk:
#include <stdio.h>
int main() {
int n = 4; // Number of rows
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Calculate the factorial of a given number:
#include <stdio.h>
int main() {
int num;
unsigned long long factorial = 1;
printf("Input the number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf("The Factorial of %d is: %llu\n", num, factorial);
return 0;
}
Sum of n terms of even natural numbers:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Input number of terms: ");
scanf("%d", &n);
printf("The even numbers are: ");
for (int i = 1; i <= n; i++) {
printf("%d ", 2 * i);
sum += 2 * i;
}
printf("\nThe Sum of even Natural Number up to %d terms: %d\n", n, sum);
return 0;
}
Pyramid pattern with numbers repeating in the same row:
#include <stdio.h>
int main() {
int n = 4; // Number of rows
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf("%d ", i);
}
printf("\n");
}
return 0;
}
Sum of the series [1 - X^2/2! + X^4/4! - ...]:
#include <stdio.h>
#include <math.h>
int main() {
int x, terms;
double sum = 1.0; // First term is 1
printf("Input the value of x: ");
scanf("%d", &x);
printf("Input the number of terms: ");
scanf("%d", &terms);
for (int i = 1; i < terms; i++) {
int power = 2 * i;
double term = pow(x, power) / tgamma(power + 1);
if (i % 2 != 0) {
term = -term;
}
sum += term;
}
printf("The sum = %lf\n", sum);
printf("Number of terms = %d\n", terms);
printf("Value of x = %d\n", x);
return 0;
}
Display n terms of a harmonic series and their sum:
#include <stdio.h>
int main() {
int n;
double sum = 0.0;
printf("Input the number of terms: ");
scanf("%d", &n);
printf("The harmonic series is: ");
for (int i = 1; i <= n; i++) {
printf("1/%d ", i);
if (i < n) {
printf("+ ");
}
sum += 1.0 / i;
}
printf("\nSum of Series up to %d terms: %lf\n", n, sum);
return 0;
}
Pyramid pattern with asterisks with each row containing an odd number of asterisks:
#include <stdio.h>
int main() {
int n = 3; // Number of rows
int stars = 1;
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
printf(" ");
}
for (int k = 1; k <= stars; k++) {
printf("*");
}
stars += 2;
printf("\n");
}
return 0;
}