1. Find ASCII Value
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
}
Output :-
Enter a character: P
ASCII value of P = 80
2. Create Multiplication Table
#include <stdio.h>
int main() {
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
Output :-
Enter an integer: 10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
3. Create Simple Calculator
#include <stdio.h>
int main() {
char operator;
double first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
Output :-
Enter an operator (+, -, *,): +
Enter two operands: 25
25
25.0 + 25.0 = 50.0
4. Check Leap Year
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}
return 0;
}
Output :-
Enter a year: 2020
2020 is a leap year.
Enter a year: 2021
2021 is not a leap year.
5. Fibonacci Series
#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;
}
Output :-
Enter the number of terms: 15
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
6. Display Characters A to Z
#include <stdio.h>
int main() {
char c;
printf("Enter u to display uppercase alphabets.\n");
printf("Enter l to display lowercase alphabets. \n");
scanf("%c", &c);
if (c == 'U' || c == 'u') {
for (c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
} else if (c == 'L' || c == 'l') {
for (c = 'a'; c <= 'z'; ++c)
printf("%c ", c);
} else {
printf("Error! You entered an invalid character.");
}
return 0;
}
Output :-
Enter u to display uppercase alphabets.
Enter l to display lowercase alphabets.
u
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
7. Simple Interest Calculator
#include<stdio.h>
void main()
{
printf("\n\n\t\tAnonymous\n\n\n");
float principal_amt, rate, simple_interest;
int time;
printf("Enter the value of principal amount, rate and time\n\n\n");
scanf("%f%f%d", &principal_amt, &rate, &time);
// considering rate is in percentage
simple_interest = (principal_amt*rate*time)/100.0;
// usually used to align text in form of columns in table
printf("\n\n\t\t\tAmount = Rs.%7.3f\n ", principal_amt);
printf("\n\n\t\t\tRate = Rs.%7.3f\n ", rate);
printf("\n\n\t\t\tTime= %d years \n", time);
printf("\n\n\t\t\tSimple Interest = Rs.%7.3f\n ", simple_interest);
printf("\n\n\t\t\tAnoymous!\n\n\n");
return 0;
}
Output :-
Anonymous
Enter the value of principal amount, rate and time
20
10
2
Amount = Rs. 20.000
Rate = Rs. 10.000
Time= 2 years
Simple Interest = Rs. 4.000
Anoymous!
8. Sum of Digits
#include<stdio.h>
#include<conio.h>
//declaring the recursive function
int sumOfDigit(int num);
void main()
{
int num, sum;
clrscr();
printf("Enter a number:\t");
scanf("%d", &num);
sum = sumOfDigit(num);
printf("The sum of digits of %d is: %d", num, sum);
getch();
}
int sumOfDigit(int num)
{
int s, a;
s = s + (num%10);
a = num/10;
if(a > 0)
{
sumOfDigit(a);
}
return s;
}
Output :-
Enter a number: 2500
The sum of digits of 2500 is: 7
9. Calculate Electricity Bill
/* C Program to Calculate Electricity Bill */
#include <stdio.h>
int main()
{
int Units;
float Amount, Sur_Charge, Total_Amount;
printf("\n Please Enter the Units that you Consumed : ");
scanf("%d", &Units);
if (Units < 50)
{
Amount = Units * 2.60;
Sur_Charge = 25;
}
else if (Units <= 100)
{
// First Fifty Units charge is 130 (50 * 2.60)
// Next, we are removing those 50 units from total units
Amount = 130 + ((Units - 50 ) * 3.25);
Sur_Charge = 35;
}
else if (Units <= 200)
{
// First Fifty Units charge is 130, and 50 - 100 is 162.50 (50 * 3.25)
// Next, we are removing those 100 units from total units
Amount = 130 + 162.50 + ((Units - 100 ) * 5.26);
Sur_Charge = 45;
}
else
{
// First Fifty Units 130, 50 - 100 is 162.50, and 100 - 200 is 526 (100 * 5.65)
// Next, we are removing those 200 units from total units
Amount = 130 + 162.50 + 526 + ((Units - 200 ) * 7.75);
Sur_Charge = 55;
}
Total_Amount = Amount + Sur_Charge;
printf("\n Electricity Bill = %.2f", Total_Amount);
return 0;
}
Output :-
Please Enter the Units that you Consumed : 100
Electricity Bill = 327.50
10. Find Days in a Months
/* C Program to Print Number of Days in a Month using Else If Statement */
#include <stdio.h>
int main()
{
int month;
printf(" Please Enter the Month Number 1 to 12 (Consider 1 = January, and 12 = December) : ");
scanf("%d", &month);
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 )
{
printf("\n 31 Days in this Month");
}
else if ( month == 4 || month == 6 || month == 9 || month == 11 )
{
printf("\n 30 Days in this Month");
}
else if ( month == 2 )
{
printf("\n Either 28 or 29 Days in this Month");
}
else
printf("\n Please enter Valid Number between 1 to 12");
return 0;
}
Output :-
Please Enter the Month Number 1 to 12 (Consider 1 = January, and 12 = December) : 6
30 Days in this Month
0 Comments