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. WAP to print the ASCII values of all characters.
#include <stdio.h>
int main() {
int i;
printf("ASCII values of all characters:\n");
for (i = 0; i <= 127; i++) {
printf("Character: %c, ASCII Value: %d\n", i, i);
}
return 0;
}
4. Write a program to find the multiplication table a given number using for loop
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number: ");
scanf("%d", &n);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
5. Write a program to display the multiplication table from 5-10.
#include <stdio.h>
int main() {
int i, j;
for (j = 5; j <= 10; j++) {
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d ", j, i, j * i);
}
printf("\n");
}
return 0;
}
6. Write a program to check whether a given number is Perfect or not.
#include <stdio.h>
int main() {
int n, R, i, sum = 0;
printf("Enter the number: ");
scanf("%d", &n);
for (i = 1; i < n; i++) {
R = n % i;
if (R == 0) {
sum = sum + i;
}
}
if (sum == n) {
printf("Perfect Number");
} else {
printf("Not a Perfect Number");
}
return 0;
}
7. Write a program to find the perfect numbers between entered two numbers.
#include <stdio.h>
int main() {
int n1, n2, sum, i, n;
printf("Enter the two numbers: ");
scanf("%d%d", &n1, &n2);
printf("The perfect numbers between %d and %d are:\n", n1, n2);
for (n = n1; n <= n2; n++) {
sum = 0;
for (i = 1; i < n; i++) {
if (n % i == 0) {
sum = sum + i;
}
}
if (sum == n) {
printf("%d\t", n);
}
}
return 0;
}
8. Write a program to find the Armstrong numbers between given two ranged numbers
#include <stdio.h>
int main() {
int n1, n2, result, num, R, i;
printf("Enter the two numbers: ");
scanf("%d%d", &n1, &n2);
printf("Armstrong numbers between %d and %d are:\n", n1, n2);
for (i = n1; i <= n2; i++) {
result = 0;
num = i;
while (num != 0) {
R = num % 10;
result = result + R * R * R;
num = num / 10;
}
if (result == i) {
printf("%d\t", i);
}
}
return 0;
}
9. Write a program to generate the Fibonacci series of n terms enter by user using loop
#include <stdio.h>
int main() {
int i, n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
return 0;
}
10. Write a program to read two integers and display both the even numbers and odd numbers between those numbers separately. Also display the number of frequencies of both even and odd numbers
#include <stdio.h>
int main() {
int a, b, Ocount = 0, Ecount = 0, i;
printf("Enter the two numbers: ");
scanf("%d%d", &a, &b);
printf("Even numbers between %d and %d are:\n", a, b);
for (i = a; i <= b; i++) {
if (i % 2 == 0) {
printf("%d\t", i);
Ecount = Ecount + 1;
}
}
printf("\nOdd numbers between %d and %d are:\n", a, b);
for (i = a; i <= b; i++) {
if (i % 2 != 0) {
printf("%d\t", i);
Ocount = Ocount + 1;
}
}
printf("\nThe number of odd and even numbers between %d and %d are %d and %d\n", a, b, Ocount, Ecount);
return 0;
}
11. Write a program to find the mean and standard deviation for the given set of numbers
#include <stdio.h>
#include <math.h>
int main() {
int n, i;
float sum = 0, mean, variance = 0, std;
printf("Enter the number of data: ");
scanf("%d", &n);
float num[n];
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++) {
scanf("%f", &num[i]);
sum = sum + num[i];
}
mean = sum / n;
for (i = 0; i < n; i++) {
variance += pow(num[i] - mean, 2);
}
variance /= n;
std = sqrt(variance);
printf("Mean is %.2f\n", mean);
printf("Standard Deviation is %.2f\n", std);
return 0;
}
Comments
Post a Comment