C Control Statements: While, and Do-While Loop Codes
1. WAP to read a number from the user and whether a given number is prime or composite.
#include <stdio.h>
int main() {
int n, i = 2;
printf("Enter the number: ");
scanf("%d", &n);
while (i < n) {
if (n % i == 0) {
printf("Composite Number");
break;
}
i++;
}
if (i == n)
printf("Prime Number");
return 0;
}
2. Write a program to read two numbers from the user (n1<n2) and display the natural numbers between them
#include <stdio.h>
int main() {
int n1, n2, i = 1;
printf("Enter the two numbers: ");
scanf("%d%d", &n1, &n2);
while (i <= (n2 - n1 - 1)) {
printf("The numbers in between %d and %d are %d\n", n1, n2, n1 + i);
i++;
}
return 0;
}
3. Write a program to read two numbers from the user (n1<n2) and display the numbers which are divisible by 5
#include <stdio.h>
int main() {
int n1, n2, i = 1;
printf("Enter the two numbers: ");
scanf("%d%d", &n1, &n2);
while (i <= (n2 - n1)) {
if ((n1 + i) % 5 == 0) {
printf("The numbers in between %d and %d that are divisible by 5 are %d\n", n1, n2, n1 + i);
}
i++;
}
return 0;
}
4. Write a program to display the series: 1/2 2/3 3/4 4/5 … (n-1)/n
#include <stdio.h>
int main() {
int i = 1, n;
printf("Enter the number: ");
scanf("%d", &n);
while (i <= n) {
printf("%d/%d\t", i, i + 1);
i++;
}
return 0;
}
5. WAP to evaluate the series: S = 1 + 2 x 1 + 3 x 2 + … N x N-1 up to N terms
#include <stdio.h>
int main() {
int i = 1, n, sum = 1;
printf("Enter the number of terms (n): ");
scanf("%d", &n);
while (i <= n) {
sum = sum + i * (i - 1);
i++;
}
printf("The sum of the series 1 + 2 x 1 + 3 x 2 + … N x (N-1) up to N terms is %d", sum);
return 0;
}
6. Write a program to read two numbers and find and display the sum. The program must ask next two numbers and add until user wants.
#include <stdio.h>
int main() {
int a, b, sum;
char i = 'y';
while (i == 'y' || i == 'Y') {
printf("Enter the two numbers: ");
scanf("%d%d", &a, &b);
sum = a + b;
printf("The sum is %d\n", sum);
printf("Do you want to add more? If yes, press y: ");
scanf(" %c", &i);
}
return 0;
}
7. Write a program to read a number from the user until a zero or negative number is keyed in. Finally calculate the sum and average of entered numbers.
#include <stdio.h>
int main() {
int n, count = 0, sum = 0;
printf("Enter numbers and enter zero or a negative number to stop:\n");
while (1) {
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 0) {
break;
}
sum = sum + n;
count++;
}
if (count > 0) {
float average = (float)sum / count;
printf("Sum of entered numbers: %d\n", sum);
printf("Average of entered numbers: %.2f\n", average);
} else {
printf("No valid numbers were entered.\n");
}
return 0;
}
8. Write a program to count the number of digits in a given integer number.
#include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter the number: ");
scanf("%lld", &n);
if (n == 0) {
count = 1;
} else {
while (n != 0) {
n = n / 10;
count++;
}
}
printf("Number of digits are %d\n", count);
return 0;
}
9. Write a program to find the sum of the digits of a given integer number.
#include <stdio.h>
int main() {
long n;
int sum = 0, R;
printf("Enter the number: ");
scanf("%ld", &n);
while (n != 0) {
R = n % 10;
sum = sum + R;
n = n / 10;
}
printf("The sum of digits is %d", sum);
return 0;
}
10. Write a program to find the reverse of the given positive number
#include <stdio.h>
int main() {
long n, result = 0;
int R;
printf("Enter the positive number: ");
scanf("%ld", &n);
do {
R = n % 10;
result = result * 10 + R;
n = n / 10;
} while (n != 0);
printf("The reversed number is %ld", result);
return 0;
}
11. Write a program to check whether a given number is Palindrome or not
#include <stdio.h>
int main() {
long n, num, result = 0;
int remainder;
printf("Enter the number: ");
scanf("%ld", &num);
n = num;
do {
remainder = num % 10;
result = result * 10 + remainder;
num = num / 10;
} while (num != 0);
if (n == result) {
printf("The number is a palindrome");
} else {
printf("The number is not a palindrome");
}
return 0;
}
12. Write a program to check whether the given number is Armstrong or not
#include <stdio.h>
int main() {
int n, R, num, result = 0;
printf("Enter the number: ");
scanf("%d", &n);
num = n;
while (n != 0) {
R = n % 10;
result = result + R * R * R;
n = n / 10;
}
if (result == num) {
printf("Entered number is an Armstrong number");
} else {
printf("Not an Armstrong Number");
}
return 0;
}
13. WAP to check whether a given number is strong or not.
#include <stdio.h>
int main() {
int n, sum = 0, num, R, i, fact;
printf("Enter the number: ");
scanf("%d", &n);
num = n;
while (n != 0) {
R = n % 10;
fact = 1;
for (i = 1; i <= R; i++) {
fact = fact * i;
}
sum = sum + fact;
n = n / 10;
}
if (sum == num) {
printf("Strong Number");
} else {
printf("Not a Strong Number");
}
return 0;
}
14. Write a program to find the HCF (GCD) and LCM of given two numbers.
#include <stdio.h>
int main() {
int a, b, c, d, hcf, lcm, temp;
printf("Enter the two numbers: ");
scanf("%d%d", &a, &b);
c = a;
d = b;
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
hcf = a;
lcm = (c * d) / hcf;
printf("HCF and LCM of %d and %d are %d and %d\n", c, d, hcf, lcm);
return 0;
}
15. Write a program to find the cubes and squares of first 10 natural numbers
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
printf("The square and cube of %d are %d and %d\n", i, i * i, i * i * i);
i++;
}
return 0;
}
16. Write a program to convert decimal number into binary number
#include <stdio.h>
int main() {
long int deci, bin, R, Div, i = 1, sum = 0;
printf("Enter the decimal number: ");
scanf("%ld", &deci);
do {
Div = deci / 2;
R = deci % 2;
deci = Div;
sum = sum + R * i;
i = i * 10;
} while (Div != 0);
printf("The binary number is %ld\n", sum);
return 0;
}
17. Write a program to convert binary number into decimal number
#include <stdio.h>
int main() {
long int bin, Div, R, i = 1, sum = 0;
printf("Enter the binary number: ");
scanf("%ld", &bin);
do {
Div = bin / 10;
R = bin % 10;
bin = Div;
sum = sum + R * i;
i = i * 2;
} while (Div != 0);
printf("The decimal number is %ld\n", sum);
return 0;
}

Comments
Post a Comment