Example of different question in c programming
#include <stdio.h>
int main() {
int rows = 6;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int rows = 6;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int rows = 7;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
printf("%d", j);
}
for (int k = 1; k <= rows - i; ++k) {
printf("*");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main(){
int num,i;
i = 1;
while(num > 0){
printf("please enter the number");
scanf("%d", &num);
i++;
}
printf("program terminated");
}
#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);
int i = 1;
while (i <= n) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);
int i = n;
do {
printf("%d ", i);
i--;
} while (i >= 1);
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
for (int i = 2; i <= 100; i++) {
if (i % 2 == 0){
printf("%d ", i);
}
}
printf("\n");
return 0;
}
#include <stdio.h>
int main(){
int sum = 0,i,num;
printf("Enter any number");
scanf("%d",&num);
for(i = 1;i<=num;i++){
sum = sum + i;
printf("%d\n",i);
}
printf("%d\n",sum);
}
#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
int factorial = 1;
for (int i = 1; i <= n; ++i) {
factorial *= i;
}
printf("Factorial of %d = %d\n", n, factorial);
}
return 0;
}
#include <stdio.h>
int main() {
int num, largest, smallest;
printf("Enter positive integers (enter a non-positive number to exit):\n");
scanf("%d", &num);
if (num > 0) {
largest = smallest = num;
while (1) {
scanf("%d", &num);
if (num <= 0) {
break;
}
if (num > largest) {
largest = num;
} else if (num < smallest) {
smallest = num;
}
}
printf("Largest number: %d\n", largest);
printf("Smallest number: %d\n", smallest);
} else {
printf("No positive integers entered.\n");
}
return 0;
}
#include <stdio.h>
int main() {
int num, firstDigit, lastDigit, sum;
printf("Enter a number: ");
scanf("%d", &num);
// Find the last digit
lastDigit = num % 10;
// Find the first digit using a for loop
int temp = num;
while (temp >= 10) {
temp /= 10;
}
firstDigit = temp;
// Calculate the sum
sum = firstDigit + lastDigit;
printf("Sum of the first and last digit: %d\n", sum);
return 0;
}
#include <stdio.h>
int main() {
int num, reversed = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num = num / 10;
}
printf("Reversed number: %d\n", reversed);
return 0;
}
#include <stdio.h>
int main() {
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
digit = num % 10; // Extract the last digit
sum += digit; // Add the digit to the sum
num = num / 10; // Remove the last digit
}
printf("Sum of the digits: %d\n", sum);
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, n = 0, result = 0;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
n++;
}
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
#include <stdio.h>
int main() {
int num, reversedNum = 0, originalNum, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num = num / 10;
}
if (originalNum == reversedNum)
printf("%d is a palindrome.\n", originalNum);
else
printf("%d is not a palindrome.\n", originalNum);
return 0;
}
#include <stdio.h>
int main() {
double base, exponent, result = 1;
printf("Enter the base: ");
scanf("%lf", &base);
printf("Enter the exponent: ");
scanf("%lf", &exponent);
if (exponent >= 0) {
for (int i = 1; i <= exponent; i++) {
result *= base;
}
printf("%.2lf ^ %.2lf = %.2lf\n", base, exponent, result);
} else {
printf("Exponent must be a non-negative number.\n");
}
return 0;
}