# Basic C Programs

## Display the message “Hello World”.

``````#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}``````

## Display the area of a rectangle.

``````#include <stdio.h>
int main()
{

/* display the area of rectangle
Basic Logic: Area R = L*B
*/

float a;
float b;

printf("Enter The Length Of Rectangle:");
scanf("%f",&a);

printf("Enter The Bredth Of Rectangle:");
scanf("%f",&b);

float area = a*b;
printf("%.2f",area);

return 0;
}``````

## Find the Area and Circumference of a Circle (c=2πr).

``````#include <stdio.h>
int main()
{

/* Area and Circumference of a Circle
Basic Logic: Area R = pi*r*r   c= 2pir
*/

float circumference, area, r;
float pi = 3.14;

scanf("%f",&r);

area= pi * r * r;
printf("Area is: %.2f", area);

circumference = 2 * pi * r;
printf("\ncircumference is : %.2f", circumference);

return 0;
}``````

## Print ASCII Value of the input Character.

``````#include<stdio.h>
int main()
{
char a;
int b;
printf("Enter The Character ");
scanf("%c",&a);
b=a;
printf("\nOutput Is: %d ",b);
return 0;
}``````

## Calculate a Simple Interest.

``````#include<stdio.h>
int main()
{
int p;
float r, t;
scanf("%d",&p);
printf("\nEnter Your Rate of interest : ");
scanf("%f",&r);
scanf("%f",&t);

float si = p*t*r/100;
printf("Simple intrest is : %f",si);

return 0;
}``````

## Calculate the tax amount an employee must pay. (tax = 15% of income).

``````#include<stdio.h>
int main()
{
int tax, salary;
scanf("%d",&salary);
printf("\nTax Rate is 15%");

float taxamount = salary*15/100;
printf("\nTax Amount is : Rs %.2f", taxamount);

return 0;
}``````

## Convert currency from Ck to Dollar. (1 dollar = Ck. 135.5)

``````#include<stdio.h>
int main()
{
float ck,dollar;

scanf("%f",&npr);
printf("\nRate: 1 dollar = Rs.135.5");

float exchange = ck/135.5;
printf("\nExchanged Dollar : \$ %.2f", exchange);

return 0;
}``````

## Convert Temperature from Fahrenheit to Celsius. C = 5/9 (f-32).

``````#include<stdio.h>
int main()
{
//Fahrenheit = ((9/5) * Celsius) + 32;
float fa, ce;

printf("\nEnter Temperature in Fahrenheit: ");
scanf("%f",&fa);

float convert = (fa - 32) * 5 / 9;
printf("\nTemperature in Celcius is:  %.2f", convert);

return 0;
}``````

## Display the Size of the Different Datatypes in C.

``````#include <stdio.h>

int main() {
printf("Size of Int Data Types in C = %2d bytes \n", sizeof(short int));
printf("Size of Long Int Data Types in C = %2d bytes \n", sizeof(long int));
printf("Size of Float Data Types in C = %2d bytes \n", sizeof(float));
printf("Size of Double Data Types in C = %2d bytes \n", sizeof(double));
printf("Size of Long Double Data Types in C = %2d bytes \n", sizeof(long double));
printf("Size of Char Data Types in C = %2d bytes \n", sizeof(char));
return 0;
}``````

## Read Integer N and Print the First Three Powers (N^1, N^2, N^3).

``````
#include <stdio.h>

int main() {

int ck;

scanf("%d",&ck);

printf("\nPower 1 : %d", ck);
printf("\nPower 2 : %d", ck*ck);
printf("\nPower 3 : %d", ck*ck*ck);

return 0;
}``````

## Find the square root of an input number.

``````
#include <stdio.h>

int main() {

int ck;
scanf("%d",&ck);

printf("\nSquare of The Number : %d", ck*ck);

return 0;
}``````

## WAP to find the roots of a quadratic equation of the form ax2+bx+c=0.

``xx``

## WAP to check whether the input number is even or not..

``````
#include <stdio.h>

int main() {

int ck;
scanf("%d",&ck);

if (ck % 2 == 0) {
printf("%d is even.\n", ck);
} else {
printf("%d is odd.\n", ck);
}

return 0;
}``````

## WAP to input age and display whether the person is eligible for voting or not.

``````
#include <stdio.h>

int main() {

int ck;
scanf("%d",&ck);

if (ck <18 ) {
printf("Your Age is %d so you are not eligible for voting.\n", ck);
} else {
printf("Your Age is %d so you are eligible for voting.\n", ck);
}

return 0;
}``````

## WAP to Find the smallest of two numbers.

``````
#include <stdio.h>

int main() {

int ck1, ck2;
printf("Enter 1st Number :");
scanf("%d",&ck1);

printf("Enter 2nd Number :");
scanf("%d",&ck2);

if (ck1 <ck2 ) {
printf("Smallest Number is :%d \n", ck1);
} else if (ck2 <ck1) {
printf("Smallest Number is :%d \n", ck2);
} else {
printf("Both numbers are equal.\n");
}

return 0;
}``````

## WAP to Find the greatest of three numbers.

``````
#include <stdio.h>

int main() {

float ck1, ck2,ck3;

printf("Enter 1st Number: ");
scanf("%f", &ck1);

printf("Enter 2nd Number: ");
scanf("%f", &ck2);

printf("Enter 3rd Number: ");
scanf("%f", &ck3);

if (ck1 >= ck2 && ck1 >= ck3) {
printf("%.2f is the greatest.\n", ck1);
} else if (ck2 >= ck1 && ck2 >= ck3) {
printf("%.2f is the greatest.\n", ck2);
} else {
printf("%.2f is the greatest.\n", ck3);
}

return 0;
}``````

## WAP to Check whether the input character Is Uppercase, Lowercase Alphabet or a Digit or a Special Symbol.

``````#include <stdio.h>

int main() {
char ch;

printf("Enter a character: ");
scanf("%c", &ch);

if ((ch >= 'A' && ch <= 'Z')) {
printf("%c is an uppercase letter.\n", ch);
} else if (ch >= 'a' && ch <= 'z') {
printf("%c is a lowercase letter.\n", ch);
} else if (ch >= '0' && ch <= '9') {
printf("%c is a digit.\n", ch);
} else {
printf("%c is a special symbol.\n", ch);
}

return 0;
}
``````

## Program to Check whether the current year is leap year or not.

``````#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.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}

return 0;
}
``````

## WAP to check whether the input character is vowel or consonant.

``````#include <stdio.h>

int main() {
char ck;

scanf("%c", &ck);

if (ck == 'a' || ck == 'e' || ck == 'i' || ck == 'o' || ck == 'u' ||
ck == 'A' || ck == 'E' || ck == 'I' || ck == 'O' || ck == 'U') {
printf("%c is a Vowel Letter\n", ck);
} else {
printf("%c is a Consonant\n", ck);
}

return 0;
}
``````

## Write a C program to determine the grade of a student based on their marks using if-else statements.

``````#include <stdio.h>

int main() {
int dl, cfa, eng, math, st;
float per;

printf("Enter Marks Obtained in DL: ");
scanf("%d", &dl);
printf("Enter Marks Obtained in cfa: ");
scanf("%d", &cfa);
printf("Enter Marks Obtained in eng: ");
scanf("%d", &eng);
printf("Enter Marks Obtained in math: ");
scanf("%d", &math);
printf("Enter Marks Obtained in st: ");
scanf("%d", &st);

per = (dl + cfa + eng + math + st) / 5.0;
printf("Percentage = %.2f\n", per);

if (per >= 90) {
} else if (per >= 80) {
} else if (per >= 70) {
} else if (per >= 60) {
} else if (per >= 50) {
} else if (per >= 40) {
} else {
}

return 0;
}
``````

## Write a C program to check whether a given number is positive, negative, or zero using if-else statements.

``````#include <stdio.h>

int main() {
int ck;

scanf("%d", &ck);

if (ck > 0) {
printf("The number is positive.\n");
} else if (ck < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}

return 0;
}
``````

## Write a C program to calculate the income tax based on the given salary using if-else statements and tax brackets.

``xx``

## WAP to print multiplication table of a number using for loop

``````#include <stdio.h>

int main() {
int ck;

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

printf("Multiplication Table of %d:\n", ck);

for (int i = 1; i <= 10; ++i) {
printf("%d x %d = %d\n", ck, i, ck * i);
}

return 0;
}
``````

## WAP to find the sum of first 100 natural numbers.

``````#include <stdio.h>

int main() {
int n = 100;
int sum = 0;

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

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

return 0;
}``````

## WAP to input a number and display the sum of all the digits.

``````#include <stdio.h>

int main() {
int nbr,nbr1, sum = 0;

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

nbr1 = nbr;

while (nbr > 0) {
int digit = nbr % 10;
sum += digit;
nbr /= 10;
}

printf("The sum of digits of %d is: %d\n", nbr1, sum);

return 0;
}

``````

## WAP to reverse a Given Number Using looping.

``````#include <stdio.h>

int main() {
int nbr, rnbr = 0;

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

int onbr = nbr;

while (nbr > 0) {
int digit = nbr % 10;
rnbr = rnbr * 10 + digit;
nbr /= 10;
}

printf("The reversed number of %d is: %d\n", onbr, rnbr);

return 0;
}
``````

## Number Is a Palindrome or Not.

``````#include <stdio.h>

int main() {
int nbr, rnbr = 0, onbr;

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

onbr = nbr;

while (nbr > 0) {
int digit = nbr % 10;
rnbr = rnbr * 10 + digit;
nbr /= 10;
}

if (onbr == rnbr) {
printf("%d is a palindrome.\n", onbr);
} else {
printf("%d is not a palindrome.\n", onbr);
}

return 0;
}
``````

## Find the Number Is Armstrong or Not Using While Loop.

``````#include <stdio.h>
#include <math.h>

int main() {
int number, originalNumber, remainder, n = 0;
double result = 0.0;

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

originalNumber = number;

// Count the number of digits
while (originalNumber != 0) {
originalNumber /= 10;
++n;
}

originalNumber = number;

// Calculate Armstrong number
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += pow(remainder, n);
originalNumber /= 10;
}

if ((int)result == number) {
printf("%d is an Armstrong number.\n", number);
} else {
printf("%d is not an Armstrong number.\n", number);
}

return 0;
}
``````

## Display Fibonacci Series.

``````#include <stdio.h>

int main() {
int n, first = 0, second = 1, next;

printf("Enter the number of terms: ");
scanf("%d", &n);

printf("Fibonacci Series:\n");

for (int i = 0; i < n; ++i) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}

return 0;
}
``````

## Find GCD(HCF) of Two Numbers.

``````#include <stdio.h>

int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}

int main() {
int num1, num2;

printf("Enter 1st number: ");
scanf("%d", &num1);

printf("\nEnter 2nd number: ");
scanf("%d", &num2);

if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}

int result = gcd(num1, num2);
printf("GCD of %d and %d is: %d\n", num1, num2, result);

return 0;
}
``````

## Find LCM of Two Numbers.

``````#include <stdio.h>

int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}

int main() {
int num1, num2, lcm;

printf("Enter 1st number: ");
scanf("%d", &num1);

printf("Enter 2nd number: ");
scanf("%d", &num2);

lcm = (num1 * num2) / gcd(num1, num2);

printf("LCM of %d and %d is: %d\n", num1, num2, lcm);

return 0;
}
``````

## Count Number of Digits of an Integer Using Loop.

``````#include <stdio.h>

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

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

int temp = number;
while (temp != 0) {
temp /= 10;
count++;
}

printf("Number of digits in %d is: %d\n", number, count);

return 0;
}
``````

## WAP to swap the value of two variables by using third variable (temp).

``````#include <stdio.h>

int main() {
int a, b;

printf("Enter 1st Variable, ie a = ");
scanf("%d", &a);

printf("\nEnter 2nd Variable, ie b = ");
scanf("%d", &b);

// Swapping without using a third variable
a = a + b;
b = a - b;
a = a - b;

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

return 0;
}
``````

## WAP to swap the value of two variables without using third variable.

``xx``

## WAP to input any five integers in an array and display the largest one.

``````#include <stdio.h>

int main() {
int numbers;

printf("Enter five integers:\n");
for (int i = 0; i < 5; ++i) {
scanf("%d", &numbers[i]);
}

int largest = numbers;
for (int i = 1; i < 5; ++i) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}

printf("The largest integer is: %d\n", largest);

return 0;
}
``````

## WAP to input any five integers in an array and display the second largest number.

``````#include <stdio.h>

int main() {
int numbers;

printf("Enter five integers:\n");
for (int i = 0; i < 5; ++i) {
scanf("%d", &numbers[i]);
}
int largest = numbers;
int secondLargest = numbers;

for (int i = 1; i < 5; ++i) {
if (numbers[i] > largest) {
secondLargest = largest;
largest = numbers[i];
} else if (numbers[i] > secondLargest && numbers[i] != largest) {
secondLargest = numbers[i];
}
}

printf("The second largest integer is: %d\n", secondLargest);

return 0;
}
``````
Scroll to Top