// 5. Check if a given number is prime or not using a for loop:
#include <stdio.h>
int main() {
int num, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
for(int i = 2; i <= num / 2; i++) {
if(num % i == 0) {
isPrime = 0;
break;
}
}
}
if(isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
👍3
// 6. Print the Fibonacci series up to a given number using a for loop:
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d", t1, t2);
for(int i = 3; i <= n; i++) {
nextTerm = t1 + t2;
printf(", %d", nextTerm);
t1 = t2;
t2 = nextTerm;
}
printf("\n");
return 0;
}
👍1
// 7. Find the sum of all elements in an array using a for loop:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(int i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum of all elements in the array is: %d\n", sum);
return 0;
}
👍1
// 8. Find the largest element in an array using a for loop:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int largest = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > largest) {
largest = arr[i];
}
}
printf("The largest element in the array is: %d\n", largest);
return 0;
}
// 9. Find the smallest element in an array using a for loop:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int smallest = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] < smallest) {
smallest = arr[i];
}
}
printf("The smallest element in the array is: %d\n", smallest);
return 0;
}
// 10. Reverse an array using a for loop:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Reversed array:\n");
for(int i = n-1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
👍2
// 11. Sort an array in ascending order using a for loop:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n; j++) {
if(arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Array in ascending order:\n");
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
❤1
// 12. Sort an array in descending order using a for loop:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n; j++) {
if(arr[i] < arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Array in descending order:\n");
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
❤1
// 13. Find the sum of the digits of a given number using a while loop:
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0) {
sum += num % 10;
num /= 10;
}
printf("Sum of the digits is: %d\n", sum);
return 0;
}
// 14. Check if a given number is a palindrome or not using a while loop:
#include <stdio.h>
int main() {
int num, originalNum, reversedNum = 0;
printf("Enter a number: ");
scanf("%d", &num);
originalNum = num;
while(num != 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
if(originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
return 0;
}
// 15. Convert a decimal number to binary using a while loop:
#include <stdio.h>
int main() {
int num, binary[32], i = 0;
printf("Enter a decimal number: ");
scanf("%d", &num);
while(num > 0) {
binary[i] = num % 2;
num = num / 2;
i++;
}
printf("Binary representation: ");
for(int j = i-1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
return 0;
}
👍1
// 16. Convert a binary number to decimal using a while loop:
#include <stdio.h>
#include <math.h>
int main() {
int binary, decimal = 0, base = 1, rem;
printf("Enter a binary number: ");
scanf("%d", &binary);
while(binary > 0) {
rem = binary % 10;
decimal = decimal + rem * base;
binary = binary / 10;
base = base * 2;
}
printf("Decimal representation: %d\n", decimal);
return 0;
}
👍1
// 17. Find the GCD of two numbers using a while loop:
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
while(a != b) {
if(a > b)
a = a - b;
else
b = b - a;
}
printf("GCD is: %d\n", a);
return 0;
}
// 18. Find the LCM of two numbers using a while loop:
#include <stdio.h>
int main() {
int a, b, max;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
max = (a > b) ? a : b;
while(1) {
if(max % a == 0 && max % b == 0) {
printf("LCM is: %d\n", max);
break;
}
max++;
}
return 0;
}
👍2
// 19. Print all Armstrong numbers between 1 and 1000 using a for loop:
#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, result, n;
printf("Armstrong numbers between 1 and 1000 are:\n");
for(num = 1; num <= 1000; num++) {
originalNum = num;
result = 0;
n = log10(num) + 1;
while(originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
if(result == num) {
printf("%d\n", num);
}
}
return 0;
}
👍6
// 20. Print all perfect numbers between 1 and 1000 using a for loop:
#include <stdio.h>
int main() {
int num, i, sum;
printf("Perfect numbers between 1 and 1000 are:\n");
for(num = 1; num <= 1000; num++) {
sum = 0;
for(i = 1; i <= num / 2; i++) {
if(num % i == 0) {
sum += i;
}
}
if(sum == num && num != 0) {
printf("%d\n", num);
}
}
return 0;
}
👍1
All 20 questions and answers related to c loops 👆👇
https://t.me/C_Codes_pro/184
https://t.me/C_Codes_pro/184
1. भारतीय संविधान को लिखने का कार्य किसने किया था?
- भारतीय संविधान को प्रेम बिहारी नारायण रायज़ादा ने अंग्रेजी में और वसंत कृष्ण वैद्य ने हिंदी में हाथ से लिखा था।
2. संविधान सभा का अध्यक्ष कौन था?
- .....
More details join: https://t.me/SidsAnalysis/38
Join for daily intrusting knowledge
- भारतीय संविधान को प्रेम बिहारी नारायण रायज़ादा ने अंग्रेजी में और वसंत कृष्ण वैद्य ने हिंदी में हाथ से लिखा था।
2. संविधान सभा का अध्यक्ष कौन था?
- .....
More details join: https://t.me/SidsAnalysis/38
Join for daily intrusting knowledge
Become Termux Expert in Hindi Series Video No.: 7 | Change Termux Permission
https://youtu.be/53b40lkce3s
https://youtu.be/53b40lkce3s
YouTube
Become Termux Expert in Hindi Series #7 | Change termux permissions | Termux storage permission
In this video, I exaplained You about how to change permissions of termux. How to change storage, video, photos permissions and also notifications permission.
Playlist:
https://youtube.com/playlist?list=PLjEYzWkdEvxsirWewr-tsGoVLrY7ABa8f&si=c8ugTtthZ67ud_zX…
Playlist:
https://youtube.com/playlist?list=PLjEYzWkdEvxsirWewr-tsGoVLrY7ABa8f&si=c8ugTtthZ67ud_zX…
👍3
आप सभी को स्वतंत्रता दिवस की हार्दिक बधाई 🇮🇳🇮🇳
❤5👍3
Forwarded from BCA MCA Btech CS IT Channel (Siddharth Sharma)
Is There any Way to run Any language code other than javascript in frontend (browser) ?
Anonymous Quiz
80%
Yes
20%
No