0% found this document useful (0 votes)
36 views

Programs C

The document describes code implementations for common programming problems and exercises including a calculator, number guessing game, temperature converter, prime number checker, Fibonacci sequence, palindrome checker, factorial calculator, banking system, to-do list, digital clock, simple interest calculator, area of circle, reversing a string, summing natural numbers, multiplication table, checking even/odd numbers, finding ASCII value, swapping numbers, and checking leap years.

Uploaded by

Muhammad Faiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Programs C

The document describes code implementations for common programming problems and exercises including a calculator, number guessing game, temperature converter, prime number checker, Fibonacci sequence, palindrome checker, factorial calculator, banking system, to-do list, digital clock, simple interest calculator, area of circle, reversing a string, summing natural numbers, multiplication table, checking even/odd numbers, finding ASCII value, swapping numbers, and checking leap years.

Uploaded by

Muhammad Faiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Calculator

 Functions: printf, scanf, switch

#include <stdio.h>

int main() {

char operator;

double firstNumber, secondNumber;

printf("Enter an operator (+, -, *, /): ");

scanf("%c", &operator);

printf("Enter two operands: ");

scanf("%lf %lf", &firstNumber, &secondNumber);

switch (operator) {

case '+':

printf("%.1lf + %.1lf = %.1lf", firstNumber, secondNumber, firstNumber + secondNumber);

break;

case '-':

printf("%.1lf - %.1lf = %.1lf", firstNumber, secondNumber, firstNumber - secondNumber);

break;

case '*':

printf("%.1lf * %.1lf = %.1lf", firstNumber, secondNumber, firstNumber * secondNumber);

break;

case '/':

printf("%.1lf / %.1lf = %.1lf", firstNumber, secondNumber, firstNumber / secondNumber);

break;

default:

printf("Error! operator is not correct");


}

return 0;

Number Guessing Game

 Functions: printf, scanf, rand, srand, time

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main() {

int randomNumber, guess;

srand(time(0)); // Seed the random number generator

randomNumber = rand() % 100 + 1; // Random number between 1 and 100

printf("Guess the number (1 to 100): ");

scanf("%d", &guess);

if(guess == randomNumber)

printf("Congratulations! You guessed the right number.");

else

printf("Sorry, the correct number was %d.", randomNumber);

return 0;

}
Temperature Converter

 Functions: printf, scanf


#include <stdio.h>

int main() {

float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9 / 5) + 32;

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);

return 0;

Check Prime Number

 Functions: printf, scanf

#include <stdio.h>

int main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

for(i = 2; i <= n/2; ++i) {

if(n % i == 0) {

flag = 1;
break;

if (n == 1) {

printf("1 is neither prime nor composite.");

} else {

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

return 0;

Fibonacci Sequence

 Functions: printf, scanf

#include <stdio.h>

int main() {

int i, n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");


for (i = 1; i <= n; ++i) {

printf("%d, ", t1);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

return 0;

Palindrome Checker

 Functions: printf, scanf, strlen, strrev

#include <stdio.h>

#include <string.h>

int main() {

char str[100], rev[100];

printf("Enter a string: ");

scanf("%s", str);

strcpy(rev, str);

strrev(rev);

if(strcmp(str, rev) == 0)

printf("The string is a palindrome.");

else

printf("The string is not a palindrome.");


return 0;

}
Palindrome Checker

 Functions: printf, scanf, strlen, strrev

#include <stdio.h>

#include <string.h>

int main() {

char str[100], rev[100];

printf("Enter a string: ");

scanf("%s", str);

strcpy(rev, str);

strrev(rev);

if(strcmp(str, rev) == 0)

printf("The string is a palindrome.");

else

printf("The string is not a palindrome.");

return 0;

Factorial Calculator

 Functions: printf, scanf

#include <stdio.h>

int main() {
int n, i;

unsigned long long factorial = 1;

printf("Enter an integer: ");

scanf("%d", &n);

if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {

for(i = 1; i <= n; ++i) {

factorial *= i;

printf("Factorial of %d = %llu", n, factorial);

return 0;

Simple Banking System

 Functions: printf, scanf, do...while

#include <stdio.h>

int main() {

int option;

float balance = 0.0f, deposit, withdraw;

do {

printf("ATM System\n");
printf("1. Deposit\n");

printf("2. Withdraw\n");

printf("3. Balance\n");

printf("4. Exit\n");

printf("Choose an option: ");

scanf("%d", &option);

switch(option) {

case 1:

printf("Enter amount to deposit: ");

scanf("%f", &deposit);

balance += deposit;

break;

case 2:

printf("Enter amount to withdraw: ");

scanf("%f", &withdraw);

if(withdraw <= balance)

balance -= withdraw;

else

printf("Insufficient balance.\n");

break;

case 3:

printf("Current balance: %.2f\n", balance);

break;

} while(option != 4);

return 0;

}
To-Do List

 Functions: printf, scanf, fgets, strcmp, strtok

#include <stdio.h>

#include <string.h>

int main() {

char tasks[10][100];

int count = 0, i;

char input[100], *token;

do {

printf("Enter task or 'done' to finish: ");

fgets(input, 100, stdin);

token = strtok(input, "\n");

if(strcmp(token, "done") != 0) {

strcpy(tasks[count], token);

count++;

} while(strcmp(token, "done") != 0 && count < 10);

printf("To-Do List:\n");

for(i = 0; i < count; i++) {

printf("%d. %s\n", i+1, tasks[i]);

return 0;

}
Digital Clock

 Functions: printf, time, localtime, strftime

#include <stdio.h>

#include <time.h>

int main() {

time_t rawtime;

struct tm * timeinfo;

char buffer[80];

time(&rawtime);

timeinfo = localtime(&rawtime);

strftime(buffer, sizeof(buffer), "%H:%M:%S", timeinfo);

printf("Current time: %s\n", buffer);

return 0;

Program Name: Simple Interest Calculator

 Function Used: printf, scanf

#include <stdio.h>

int main() {

float principal, rate, time, interest;

printf("Enter principal amount: ");


scanf("%f", &principal);

printf("Enter annual interest rate: ");

scanf("%f", &rate);

printf("Enter time (in years): ");

scanf("%f", &time);

interest = (principal * rate * time) / 100;

printf("Simple Interest = %.2f", interest);

return 0;

Program Name: Area of Circle

 Function Used: printf, scanf, #define


 Code:

#include <stdio.h>

#define PI 3.14159

int main() {

float radius, area;

printf("Enter the radius of the circle: ");

scanf("%f", &radius);

area = PI * radius * radius;

printf("Area of the circle = %.2f", area);

return 0;

}
Program Name: Reverse a String
 Function Used: printf, scanf, strlen
 Code

#include <stdio.h>

#include <string.h>

int main() {

char str[100], rev[100];

int i, j, len;

printf("Enter a string: ");

scanf("%s", str);

len = strlen(str);

for(i = len - 1, j = 0; i >= 0; i--, j++)

rev[j] = str[i];

rev[j] = '\0';

printf("Reversed string: %s", rev);

return 0;

}
Program Name: Sum of Natural Numbers

 Function Used: printf, scanf, for

#include <stdio.h>

int main() {

int n, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

for(int i = 1; i <= n; i++) {

sum += i;

printf("Sum = %d", sum);

return 0;

}
Program Name: Multiplication Table

 Function Used: printf, scanf, for

#include <stdio.h>

int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

for(int i = 1; i <= 10; ++i) {

printf("%d * %d = %d \n", num, i, num * i);

return 0;

1. }

o }
o
2. Program Name: Check Even or Odd
o Function Used: printf, scanf

#include <stdio.h>

int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

if(num % 2 == 0)

printf("%d is even.", num);

else

printf("%d is odd.", num);

return 0;

}
Program Name: Find ASCII Value of a Character

 Function Used: printf, scanf

#include <stdio.h>

int main() {

char c;

printf("Enter a character: ");

scanf("%c", &c);

printf("ASCII value of %c = %d", c, c);


return 0;

Program Name: Swap Two Numbers

 Function Used: printf, scanf

#include <stdio.h>

int main() {

int a, b, temp;

printf("Enter two integers: ");

scanf("%d %d", &a, &b);

temp = a;

a = b;

b = temp;

printf("After swapping, a = %d, b = %d", a, b);

return 0;

Program Name: Check Leap Year

 Function Used: printf, scanf

#include <stdio.h>

int main() {

int year;
printf("Enter a year: ");

scanf("%d", &year);

if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

printf("%d is a leap year.", year);

else

printf("%d is not a leap year.", year);

return 0;

Program Name: Find Largest of Three Numbers

 Function Used: printf, scanf

#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three numbers: ");

scanf("%d %d %d", &a, &b, &c);

if(a >= b && a >= c)

printf("%d is the largest number.", a);

else if(b >= a && b >= c)

printf("%d is the largest number.", b);

else

printf("%d is the largest number.", c);

return 0;
}

Program Name: Count Vowels in a String

 Function Used: printf, scanf, tolower

#include <stdio.h>

#include <ctype.h>

int main() {

char str[100];

int count = 0;

printf("Enter a string: ");

scanf("%s", str);

for(int i = 0; str[i] != '\0'; ++i) {

char ch = tolower(str[i]);

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

++count;

printf("Number of vowels: %d", count);

return 0;

Program Name: Generate Random Numbers

 Function Used: printf, rand, srand, time

#include <stdio.h>
#include <stdlib.h>

#include <time.h>

int main() {

srand(time(0)); // Seed the random number generator

for(int i = 0; i < 5; ++i) {

printf("%d ", rand() % 100);

return 0;

Program Name: Check Armstrong Number

 Function Used: printf, scanf, pow

#include <stdio.h>

#include <math.h>

int main() {

int num, originalNum, remainder, n = 0;

float result = 0.0;

printf("Enter an integer: ");

scanf("%d", &num);

originalNum = num;

for (originalNum = num; originalNum != 0; ++n) {

originalNum /= 10;
}

for (originalNum = num; originalNum != 0; originalNum /= 10) {

remainder = originalNum % 10;

result += pow(remainder, n);

if ((int)result == num)

printf("%d is an Armstrong number.", num);

else

printf("%d is not an Armstrong number.", num);

return 0;

}
Program Name: Binary to Decimal Conversion

 Function Used: printf, scanf, pow

#include <stdio.h>

#include <math.h>

int main() {

long long n;

int dec = 0, i = 0, rem;

printf("Enter a binary number: ");

scanf("%lld", &n);

while (n != 0) {

rem = n % 10;

n /= 10;
dec += rem * pow(2, i);

++i;

printf("Decimal: %d", dec);

return 0;

Program Name: Concatenate Two Strings

 Function Used: printf, scanf, strlen

#include <stdio.h>

#include <string.h>

int main() {

char s1[100], s2[100];

printf("Enter string 1: ");

scanf("%s", s1);

printf("Enter string 2: ");

scanf("%s", s2);

strcat(s1, s2);

printf("Concatenated string: %s", s1);

return 0;

Program Name: Find the Length of a String


 Function Used: printf, scanf, strlen

#include <stdio.h>

#include <string.h>

int main() {

char s[100];

printf("Enter a string: ");

scanf("%s", s);

printf("Length of string: %zu", strlen(s));

return 0;

Program Name: Calculate Power of a Number

 Function Used: printf, scanf, pow

#include <stdio.h>

#include <math.h>

int main() {

double base, exp;

printf("Enter base: ");

scanf("%lf", &base);

printf("Enter exponent: ");

scanf("%lf", &exp);
printf("Result: %.2lf", pow(base, exp));

return 0;

}
Program Name: Find the Largest Element in an Array

 Function Used: printf, scanf

#include <stdio.h>

int main() {

int n, arr[1000], max;

printf("Enter number of elements: ");

scanf("%d", &n);

for(int i = 0; i < n; ++i) {

scanf("%d", &arr[i]);

max = arr[0];

for(int i = 1; i < n; ++i) {

if(arr[i] > max)

max = arr[i];

printf("Largest element: %d", max);

return 0;
}

Program Name: Calculate the Average of Numbers in an Array

 Function Used: printf, scanf

#include <stdio.h>

int main() {

int n, arr[1000];

float sum = 0.0;

printf("Enter number of elements: ");

scanf("%d", &n);

for(int i = 0; i < n; ++i) {

scanf("%d", &arr[i]);

sum += arr[i];

printf("Average: %.2f", sum / n);

return 0;

}
Program Name: Count the Number of Digits in an Integer

 Function Used: printf, scanf

#include <stdio.h>

int main() {

long long n;

int count = 0;
printf("Enter an integer: ");

scanf("%lld", &n);

while (n != 0) {

n /= 10;

++count;

printf("Number of digits: %d", count);

return 0;

You might also like