Introduction to condition and switch

  

example of how to use condition in c

  

Different example of condition in c programming

  

example of how to use switch in c programming

  

example of different qauestion using switch in c programming

  

Introduction to Loop in c programming

  

Different question of using loop in c programming

  

Introduction to array in c programming

  

Different question and answer in array

  

Introduction to Condition and switch


Example of different question in c programming



Write a C program that will print the following pattern using a single *:


*******
******
*****
****
***
**
*


#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;
}


Write a C program that will print the following pattern using a single *:


*
**
***
****
*****
******
*******


#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;
}



Write a C program that will print the following pattern:


1******
12*****
123****
1234***
12345**
123456*
1234567


#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;
}




Write a C program that will ask the user to input n positive numbers. The program will terminate if one of those numbers is not positive.



#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");
}





Write a C program to print all integer numbers from 1 to n. - using while loop



#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;
}



Write a C program to print all natural numbers in reverse (from n to 1). - Using do while loop



#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;
}



Write a C program to print all even numbers between 1 and 100.



#include <stdio.h>
int main() {
for (int i = 2; i <= 100; i++) {
    if (i % 2 == 0){
        printf("%d ", i);
    }
}
printf("\n");
return 0;
}



Write program to print the sum of odd number from 1 to n using for loop



#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);
        
}



Write a C program to calculate the factorial of a positive integer:



#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;
}



Write a C program to find the largest and smallest numbers among a set of positive integers entered by the user. The program should terminate when the user enters a non-positive number:



#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;
}



C program that calculates the sum of the first and last digits of a number entered by the user using a for loop:



#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;
}



C program that reverses the digits of a number entered by the user using a for loop:



#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;
}



C program that calculates the sum of the digits of a number entered by the user using a loop:



#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;
}



C program that checks whether a number is an Armstrong number using a loop:



#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;
}






C program that checks whether a number is a palindrome using a loop:



#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;
}




C program that calculates the power of a number using a loop:



#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;
}


Contact Us

Email: info@learn.co.rw
Phone: +250723709880
Whatsap: +250725527181

we will try to reply to your message as quickly as we can. please be aware that because of many messages we receive it take us same time to respond