Explore the list of my C programs below:
Conditional Statements
-
Vowel or Consonent Check
#include <stdio.h>
int main()
{
char ch;
// Ask the user to input a character
printf("Enter a Alphabet: ");
scanf("%c", &ch);
// Check if the character is a vowel using switch statement
switch (ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is a consonent.\n", ch);
}
return 0;
}
-
Positive-Even-Odd number check
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("cls");
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
{
printf("no. is Positive\n");
if (num % 2 == 0)
printf("no. is even\n");
else
printf("no. is odd\n");
}
else
printf("NO, IS NEGATIVE\n");
return 0;
}
-
Palindrome series
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("cls"); // Clear the console screen (specific to Windows OS).
// Variables to store the number, remainder, and result.
int n, r, num, res = 0;
// Prompt the user to enter a number.
printf("enter the number=");
scanf("%d", &num);
// Store the original number for later comparison.
n = num;
// Reverse the number.
while (n > 0)
{
r = n % 10; // Get the last digit of the number.
res = res * 10 + r; // Append the digit to the reversed number.
n = n / 10; // Remove the last digit from the number.
}
// Output the reversed number.
printf("reverse of %d is %d\n", num, res);
// Check if the original number is a palindrome.
if (num == res)
printf("%d is a palindrome number\n", num);
else
printf("%d is not a palindrome number\n", num);
return 0;
}
Loops
-
Table of any number
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("cls"); // Clear the console screen.
int n; // Variable to store the user-input number.
printf("Enter a number to generate its table: ");
scanf("%d", &n); // Read the number from user input.
// Loop to generate and print the multiplication table for the number 'n'.
for (int i = 1; i <= 10; i++)
printf("%d\n", n * i); // Print each line of the multiplication table.
return 0; // Indicate that the program ended successfully.
}
-
Star Pyramid
#include <stdio.h>
int main() {
int i, j, space, rows, count = 0;
printf("Enter the no. of rows = ");
scanf("%d", &rows);
int k = space;
for (i = 1; i <= rows; i++) {
for (k = 1; k <= rows - i; k++)
printf(" ");
for (j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}
-
Pattern 123
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("cls");
int a = 1;
for (int i = 0; i <= 4; i++)
for (int j = 0; j < i; j++)
printf("%d", a);
a++;
printf("\n");
return 0;
}
-
Factorial
#include <stdio.h>
int main()
{
int num, fact = 1;
printf("enter any integer number: ");
scanf("%d", &num);
// Calculate factorial of the entered integer
for (int i = 1; i <= num; i++)
fact *= i;
printf("factorial of %d is : %d", num, fact);
return 0;
}
-
Armstrong number
#include <stdio.h>
int main() {
int n, temp, sum = 0, r;
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while (temp > 0) {
r = temp % 10;
sum += r * r * r;
temp /= 10;
}
if (sum == n)
printf("%d is a Armstrong number", n);
else
printf("Not an Armstrong number");
return 0;
}
Array
-
Sum of Matrixx
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("cls");
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int a[rows][cols], b[rows][cols], c[rows][cols];
printf("Enter elements of matrix A:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &a[i][j]);
printf("Enter elements of matrix B:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &b[i][j]);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
c[i][j] = a[i][j] + b[i][j];
printf("Sum of the matrices:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
-
Reverse of Array
#include <stdio.h>
// Function prototypes
void printArray(int a[], int n); // fn prototype
void reverse(int a[], int n);
int main()
{
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
printf("Enter element:");
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
printArray(a, n); // function call
reverse(a, n); // function call
printArray(a, n); // function call
return 0;
}
void printArray(int a[], int n)
{ // fn defination
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
void reverse(int a[], int n)
{ // fn defination
for (int i = 0; i < n / 2; i++) {
int temp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = temp;
}
}
-
Count Positive Integers
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Clear the console screen
system("cls");
// Initialize an array with 10 integer elements
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Print the array elements
printf("Array elements are: ");
for (int i = 0; i < 10; i++) {
printf("%d", a[i]);
if (i < 9) {
printf(", ");
}
}
printf("\n");
// Return 0 to indicate successful execution
return 0;
}
-
Basic Array Operations
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("cls");
// Declare and initialize an array of integers
int numbers[] = {10, 20, 30, 40, 50};
// Calculate the size of the array
int size = sizeof(numbers) / sizeof(numbers[0]);
// Print each element of the array
for (int i = 0; i < size; i++) {
printf("Element at index %d: %d\n", i, numbers[i]);
}
return 0;
}
Functions
-
Swaping of two numbers
#include <stdio.h>
#include <stdlib.h>
void swap(int *, int *);
int main()
{
system("cls"); // Clear the console screen (specific to Windows OS).
int a = 4, b = 6;
swap(&a, &b); // Swap the values of a and b using their memory addresses.
printf("the value of a is %d and b is %d", a, b); // Print the swapped values of a and b.
return 0; // Indicate that the program ended successfully.
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
-
C2f-and-f2c
/**
* This program prompts the user to enter a temperature in Celsius and converts it to Fahrenheit,
* then prompts the user to enter a temperature in Fahrenheit and converts it to Celsius.
*
* return int Returns 0 upon successful execution.
*/
#include <stdio.h>
#include <stdlib.h>
float c2f(float); // FUNCTION PROTOTYPE
float f2c(float);
int main()
{
system("cls"); // Clear the console screen
float c, fahrenheit, f, f1;
printf("Enter temperature in Celsius: ");
scanf("%f", &c);
fahrenheit = c2f(c); // Call the conversion function from Celsius to Fahrenheit
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &f);
f1 = f2c(f); // Call the conversion function from Fahrenheit to Celsius
printf("Temperature in Celsius: %.2f\n", f1);
return 0;
}
// Function to convert Celsius to Fahrenheit
float c2f(float c)
{
return (c * 9 / 5) + 32;
}
// Function to convert Fahrenheit to Celsius
float f2c(float f)
{
return (f - 32) * 5 / 9;
}
-
Add Using_fn
#include <stdio.h>
int add(int, int); // Function prototype
int main() {
int result = add(5, 10);
printf("Sum: %d", result);
return 0;
}
int add(int a, int b) {
return a + b;
}
-
Factorial-using-fn
#include <stdio.h>
int fact(int); // function prototype
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
printf("Factorial of a negative number is not defined.\n");
else
printf("Factorial of %d is %d.\n", num, fact(num)); // function call
return 0;
}
// function to calculate factorial
int fact(int n)
{
if (n == 0)
return 1; // Base case: factorial of 0 is 1
else if (n == 1)
return 1; // Base case: factorial of 1 is 1
else
return fact(n - 1) * n; // Recursive case
}
Searching & Sorting
-
Occurance-of-number
#include <stdio.h>
#include <stdlib.h>
int main() {
system("cls"); // Clear the console screen (specific to Windows OS)
int a[5] = {1, 3, 2, 3, 1}; // Initialize an array with predefined values
// Iterate over each element in the array
for(int i = 0; i < 5; i++) {
int count = 0; // Initialize count for occurrences
int printed = 0; // Flag to check if the number was already printed
// Check if the current number was already printed
for(int k = 0; k < i; k++) {
if(a[i] == a[k]) {
printed = 1; // Set flag if number was already printed
break; // Exit loop early if number was found
}
}
// Only count and print if the number was not already printed
if(!printed) {
// Count occurrences of the current number
for(int j = 0; j < 5; j++) {
if(a[i] == a[j]) {
count++; // Increment count for each occurrence
}
}
// Print the number and its count of occurrences
printf("%d occurs %d times\n", a[i], count);
}
}
return 0; // Return success status
}
-
Taking input for linear searching
#include <stdio.h>
#include <stdlib.h>
int main() {
// Clear the console screen
system("cls");
int key, i, n;
// Prompt user for the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
// Declare the array with size n
int a[n];
// Prompt user to enter the elements of the array
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
// Prompt user to enter the element to search for
printf("Enter the element to search: ");
scanf("%d", &key);
int found = 0;
// Perform linear search for the key in the array
for (i = 0; i < n; i++) {
if (a[i] == key) {
found = 1;
break;
}
}
// Output the result of the search
if (found) {
printf("%d is found at index %d\n", key, i);
} else {
printf("%d is not found\n", key);
}
return 0;
}
-
linear saerching in function
#include <stdio.h>
int linearSearch(int arr[], int n, int target);
int main()
{
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 10, target, i;
printf("Enter the element to be searched: ");
scanf("%d", &target);
int result = linearSearch(arr, n, target);
if (result == i) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found in the array.\n");
}
return 0;
}
int linearSearch(int arr[], int n, int target) {
int i;
for (i = 0; i < n; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}