232

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

#include <stdio.

h> -----1--------

int main() {
// Loop from 1 to 10
for (int i = 1; i <= 10; i++) {
printf("%d\n", i); // Print the current number
}

return 0;
}
---------------------------------------------------------------------------------

#include <stdio.h> ------2-----------

int main() {
int num = 2; // Starting number

// Loop until num reaches 20


while (num <= 20) {
printf("%d\n", num); // Print the current number
num += 2; // Increment num by 2 to get the next even number
}

return 0;
}
-----------------------------------------------------------------------------------
#include <stdio.h> ---------3-------------

int main() {
int num = 1; // Starting number
int sum = 0; // Variable to store the sum

// Loop until num reaches 50


do {
sum += num; // Add num to sum
num++; // Increment num
} while (num <= 50);

printf("The sum of numbers from 1 to 50 is: %d\n", sum); // Print the sum

return 0;
}
The sum of numbers from 1 to 50 is: 1275

-----------------------------------------------------------------------------------
-
#include <stdio.h> -------4-----------

int main() {
int number;
unsigned long long factorial = 1; // Factorial can grow large, so we use
unsigned long long to accommodate large numbers

// Input the number from the user


printf("Enter a positive integer: ");
scanf("%d", &number);

// Check if the number is negative


if (number < 0) {
printf("Error! Factorial of a negative number doesn't exist.");
}
else {
// Calculate factorial using a for loop
for (int i = 1; i <= number; ++i) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", number, factorial);
}
return 0;
}
-----------------------------------------------------------------------------------
-
#include <stdio.h> ---------5--------

int main() {
int number, sum = 0;

// Infinite loop
while (1) {
// Prompt the user to enter a number
printf("Enter a number (enter 0 to exit): ");
scanf("%d", &number);

// Check if the entered number is 0


if (number == 0) {
break; // Break out of the loop if the number is 0
}

// Add the entered number to the sum


sum += number;
}

// Display the sum


printf("The sum of the entered numbers is: %d\n", sum);

return 0;
}
---------------------------------------------------------------------------------
#include <stdio.h> --------6-----------

int main() {
int num, count = 0;
double sum = 0.0, average;

// Loop to get positive numbers from the user


do {
// Prompt the user to enter a number
printf("Enter a positive number (negative to terminate): ");
scanf("%d", &num);

// Check if the entered number is positive


if (num >= 0) {
sum += num; // Add the number to the sum
count++; // Increment count
}
} while (num >= 0);

// Check if at least one positive number was entered


if (count > 0) {
// Calculate the average
average = sum / count;
printf("Average of the entered positive numbers: %.2f\n", average);
} else {
printf("No positive numbers entered.\n");
}

return 0;
}
-----------------------------------------------------------------------------------
---
#include <stdio.h> -------------7-------------------

int main() {
int number;

// Input the number from the user


printf("Enter a number to print its multiplication table: ");
scanf("%d", &number);

// Print the multiplication table using a for loop


printf("Multiplication table of %d:\n", number);
for (int i = 1; i <= 10; ++i) {
printf("%d x %d = %d\n", number, i, number * i);
}

return 0;
}
-----------------------------------------------------------------------------------
-------
#include <stdio.h> -----8--------

// Function prototype
int max(int a, int b);

int main() {
int num1, num2;

// Input two numbers from the user


printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Call the max function and display the result


printf("The maximum of %d and %d is: %d\n", num1, num2, max(num1, num2));

return 0;
}

// Function definition for finding maximum of two numbers


int max(int a, int b) {
return (a > b) ? a : b; // Return the larger of the two numbers
}
-----------------------------------------------------------------------------------
---
#include <stdio.h> --------------9-------------

// Function prototype
int sumOfSquares(int a, int b);

int main() {
int num1, num2;

// Input two numbers from the user


printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Call the sumOfSquares function and display the result


printf("The sum of squares of %d and %d is: %d\n", num1, num2,
sumOfSquares(num1, num2));

return 0;
}

// Function definition for calculating sum of squares of two numbers


int sumOfSquares(int a, int b) {
return (a * a) + (b * b); // Return the sum of squares of the two numbers
}
-----------------------------------------------------------------------------------
-------
#include <stdio.h> -------10----------

// Function prototype
void printPattern();

int main() {
// Call the printPattern function
printPattern();

return 0;
}

// Function definition for printing a rectangle of stars


void printPattern() {
int rows = 5; // Number of rows
int cols = 3; // Number of columns

// Nested loop to print the rectangle of stars


for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("* "); // Print a star followed by a space
}
printf("\n"); // Move to the next line after printing each row
}
}
-----------------------------------------------------------------------------------
#include <stdio.h> -------------11-----------------

// Function prototype
int isPrime(int number);

int main() {
int num;

// Input a number from the user


printf("Enter a number: ");
scanf("%d", &num);
// Call the isPrime function and display the result
if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}

// Function definition for checking if a number is prime


int isPrime(int number) {
// Special case: 1 is not a prime number
if (number <= 1) {
return 0; // False
}

// Check for divisibility by numbers from 2 to sqrt(number)


for (int i = 2; i * i <= number; ++i) {
if (number % i == 0) {
return 0; // False
}
}

return 1; // True
}
-----------------------------------------------------------------------------------
-------
#include <stdio.h> ----------------------12---------------------

// Function prototype
void printMenu();

int main() {
// Call the printMenu function to display the menu
printMenu();

return 0;
}

// Function definition for printing the calculator menu


void printMenu() {
printf("Calculator Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
}
-----------------------------------------------------------------------------------
----
#include <stdio.h> -----------13--------------

// Function prototype
void printTable(int number);

int main() {
int num;
// Input the number from the user
printf("Enter a number to print its multiplication table: ");
scanf("%d", &num);

// Call the printTable function with the entered number


printTable(num);

return 0;
}

// Function definition for printing the multiplication table of a given number


void printTable(int number) {
printf("Multiplication table of %d:\n", number);
for (int i = 1; i <= 10; ++i) {
printf("%d x %d = %d\n", number, i, number * i);
}
}

You might also like