0% found this document useful (0 votes)
2 views57 pages

C Practice Questions

The document contains a collection of C programming questions and their corresponding answers, covering a variety of topics such as basic input/output, arithmetic operations, control structures, and functions. Each program is accompanied by its code, a brief description, and sample output. The examples range from simple tasks like printing 'Hello World!' to more complex operations like calculating the HCF of two numbers and converting temperatures.

Uploaded by

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

C Practice Questions

The document contains a collection of C programming questions and their corresponding answers, covering a variety of topics such as basic input/output, arithmetic operations, control structures, and functions. Each program is accompanied by its code, a brief description, and sample output. The examples range from simple tasks like printing 'Hello World!' to more complex operations like calculating the HCF of two numbers and converting temperatures.

Uploaded by

svijimeena2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DHANALAKSHMI COLLEGE OF ENGINEERING

DEPARTMENT OF TRAINING AND PLACEMENT


PROGRAMMING QUESTIONS WITH ANSWER

1. C program to print "Hello World!" / First C program

#include <stdio.h>
int main()
{
printf("Hello World!");

return 0;

}
OUTPUT
Hello World!

2. C program to find subtraction of two integer number

#include<stdio.h>

int main()
{
int a,b,sub;
printf("Enter the first no.: ");
scanf("%d",&a);
printf("Enter the second no.: ");
scanf("%d",&b);
sub= a-b;
printf("subtract is = %d\n", sub);
return 0;
}
OUTPUT
Enter the first no.: 12
Enter the second no.: 6
subtract is = 6

3. C Program to find sum and average of two numbers

#include <stdio.h>

int main()
{
int a,b,sum;
float avg;
printf("Enter first number :");
scanf("%d",&a);
printf("Enter second number :");
scanf("%d",&b);

sum=a+b;
avg= (float)(a+b)/2;

printf("\nSum of %d and %d is = %d",a,b,sum);


printf("\nAverage of %d and %d is = %f",a,b,avg);

return 0;
}
OUTPUT
Enter first number :20
Enter second number :35
Sum of 20 and 35 is = 55
Average of 20 and 35 is = 27.500000

4.C program to print ASCII value of a character

#include <stdio.h>

int main()
{
char ch;

//input character
printf("Enter the character: ");
scanf("%c",&ch);

printf("ASCII is = %d\n", ch);


return 0;
}

OUTPUT
Enter the character: A
ASCII is = 65

5.C program to find cube of an integer number using two different methods

#include <stdio.h>

int main()
{

int a,cube;
printf("Enter any integer number: ");
scanf("%d",&a);
//calculating cube
cube = (a*a*a);
printf("CUBE is: %d\n",cube);
return 0;
}

OUTPUT
Enter any integer number: 3
CUBE is: 27

6. C program to find quotient and remainder

#include <stdio.h>

int main()
{
int dividend, divisor;
int quotient, remainder;
printf("Enter dividend: ");
scanf("%d",&dividend);
printf("Enter divisor: ");
scanf("%d",&divisor);
quotient= dividend/divisor;
remainder= dividend%divisor;

printf("quotient: %d, remainder: %d\n",quotient,remainder);

return 0;
}

OUTPUT
Enter dividend: 48
Enter divisor: 2
quotient: 24, remainder: 0

[Link] to calculate simple interest

#include <stdio.h>
int main()
{
float amount,rate,time,si;

printf("Enter principal (Amount) :");


scanf("%f",&amount);
printf("Enter rate :");
scanf("%f",&rate);
printf("Enter time (in years) :");
scanf("%f",&time);
si=(amount*rate*time)/100;
printf("\nSimple Interest is = %f",si);

return 0;
}
OUTPUT

Enter principal (Amount) :4500


Enter rate :2
Enter time (in years) :2
Simple Interest is = 180.000000

8. Program to check whether number is EVEN or ODD

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

int num;

printf("Enter an integer number: ");


scanf("%d",&num);
if(num%2==0)
printf("%d is an EVEN number.",num);
else
printf("%d is an ODD number.",num);

return 0;
}
OUTPUT
Enter an integer number: 45
45 is an ODD number.

9. Program to find largest number among three numbers

/* c program to find largest number among 3 numbers*/


#include <stdio.h>

int main()
{
int a,b,c;
int largest;

printf("Enter three numbers (separated by space):");


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

if(a>b && a>c)


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

printf("Largest number is = %d",largest);

return 0;
}

OUTPUT
Enter three numbers (separated by space):23
245
367
Largest number is = 367

10. C program to check whether a person is eligible for voting or not?

#include<stdio.h>

int main()
{
int a ;

//input age
printf("Enter the age of the person: ");
scanf("%d",&a);

//check voting eligibility


if (a>=18)
{
printf("Eigibal for voting");
}
else
{
printf("Not eligibal for voting\n");
}

return 0;
}

OUTPUT
Enter the age of the person: 23
Eigibal for voting

11. C program to read marks and print percentage and division

#include<stdio.h>
int main()
{
int science;
int math;
int english;

int total;
float per;

science = 50;
math = 90;
english = 40;
//you can take input from user instead of these values

//calculating total
total= science + math + english;

//calculating percentage
per= (float) total*100/300;

printf("Total Marks: %d\n",total);


printf("Percentage is: %.2f\n",per);

//checking division and printing


if(per>=60)
{
printf("First division\n");
}
else if(per>=50)
{
printf("Second division");
}
else if(per>=40)
{
printf("Third division");
}
else
{
printf("Fail\n");
}

return 0;
}

OUTPUT

Total Marks: 180


Percentage is: 60.00
First division

12. Program to find gross salary of an employee

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

char name[30];
float basic, hra, da, pf, gross;
printf("Enter name: ");
gets(name);
printf("Enter Basic Salary: ");
scanf("%f",&basic);
printf("Enter HRA: ");
scanf("%f",&hra);
printf("Enter D.A.: ");
scanf("%f",&da);
pf= (basic*12)/100;
gross=basic+da+hra+pf;

printf("\nName: %s \nBASIC: %f \nHRA: %f \nDA: %f \nPF: %f \n***GROSS


SALARY: %f ***",name,basic,hra,da,pf,gross);

return 0;
}
OUTPUT
Enter name: Mike
Enter Basic Salary: 23000
Enter HRA: 9500
Enter D.A.: 9500

13. C program to convert temperature from Fahrenheit to Celsius and vice versa

/* C Program to convert temperature from Fahrenheit to Celsius and vice versa.*/


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

float fh,cl;
int choice;

printf("\n1: Convert temperature from Fahrenheit to Celsius.");


printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): ");
scanf("%d",&choice);
if(choice ==1){
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
else if(choice==2){
printf("\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
}
else{
printf("\nInvalid Choice !!!");
}
return 0;
}
OUTPUT
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 1
Enter temperature in Fahrenheit: 358
Temperature in Celsius: 181.11
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 2
Enter temperature in Celsius: 357
Temperature in Fahrenheit: 674.60

14. C program to calculate X^N (X to the power of N) using pow function

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

int main()
{
int x,n;
int result;

printf("Enter the value of base: ");


scanf("%d",&x);

printf("Enter the value of power: ");


scanf("%d",&n);

result =pow((double)x,n);

printf("%d to the power of %d is= %d", x,n, result);


getch();
return 0;
}

OUTPUT
Enter the value of base: 3
Enter the value of power: 7
3 to the power of 7 is= 2187

15. C program to find the difference of two numbers

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

int a,b;
int diff;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
if( a>b )
diff=a-b;
else
diff=b-a;

printf("\nDifference between %d and %d is = %d",a,b,diff);


return 0;
}

OUTPUT
Enter first number: 34
Enter second number: 21
Difference between 34 and 21 is = 13
16. C program to print size of variables using sizeof() operator

#include <stdio.h>

int main()
{

int a,b;
int diff;

printf("Enter first number: ");


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

// check condition to identify which is largest number


if( a>b )
diff=a-b;
else
diff=b-a;

printf("\nDifference between %d and %d is = %d",a,b,diff);


return 0;
}

OUTPUT
Enter first number: 48
Enter second number: 14
Difference between 48 and 14 is = 34

17. C program to demonstrate examples of escape sequences

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

printf("Hello\nWorld!"); //use of \n

printf("\nHello\tWorld!"); // use of \t

printf("\n\"Hello World!\""); //use of \"

printf("\nHello\bWorld!"); //use of \b
return 0;
}

OUTPUT
Hello
World!
Hello World!
"Hello World!"
Hello#World!

18. C program to find area and perimeter of circle

#include <stdio.h>
#define PI 3.14f
int main()
{
float rad,area, perm;
printf("Enter radius of circle: ");
scanf("%f",&rad);
area=PI*rad*rad;
perm=2*PI*rad;
printf("Area of circle: %f \nPerimeter of circle: %f\n",area,perm);
return 0;
}

OUTPUT
Enter radius of circle: 4
Area of circle: 50.240002
Perimeter of circle: 25.120001

19. C program to find area of a rectangle

#include <stdio.h>
int main()
{
float l,b,area;
printf("Enter the value of length: ");
scanf("%f",&l);
printf("Enter the value of breadth: ");
scanf("%f",&b);
area=l*b;
printf("Area of rectangle: %f\n",area);
return 0;
}
OUTPUT
Enter the value of length: 3
Enter the value of breadth: 4
Area of rectangle: 12.000000
20. C program to calculate HCF of two numbers

#include <stdio.h>
int findHcf(int a,int b)
{
int temp;

if(a==0 || b==0)
return 0;
while(b!=0)
{
temp = a%b;
a = b;
b = temp;
}
return a;
}
int main()
{
int a,b;
int hcf;

printf("Enter first number: ");


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

hcf=findHcf(a,b);
printf("HCF (Highest Common Factor) of %d,%d is: %d\n",a,b,hcf);

return 0;
}

OUTPUT
Enter first number: 234
Enter second number: 34
HCF (Highest Common Factor) of 234,34 is: 2

21. C program to multiply two numbers using plus operator

#include <stdio.h>
int main()
{
int a,b;
int mul,loop;

printf("Enter first number: ");


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

mul=0;

for(loop=1;loop<=b;loop++){
mul += a;
}

printf("Multiplication of %d and %d is: %d\n",a,b,mul);

return 0;
}
OUTPUT
Enter first number: 12
Enter second number: 17
Multiplication of 12 and 17 is: 204

22. C program to demonstrate example of global and local scope

#include <stdio.h>
int a=10; //global variable
void fun(void);
int main()
{
int a=20; /*local to main*/
int b=30; /*local to main*/

printf("In main() a=%d, b=%d\n",a,b);


fun();
printf("In main() after calling fun() ~ b=%d\n",b);
return 0;
}

void fun(void)
{
int b=40; /*local to fun*/

printf("In fun() a= %d\n", a);


printf("In fun() b= %d\n", b);
}

OUTPUT
In main() a=20, b=30
In fun() a= 10
In fun() b= 40
In main() after calling fun() ~ b=30

23. C program to demonstrate example of floor and ceil functions

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

int main()
{
float val;
float fVal,cVal;

printf("Enter a float value: ");


scanf("%f",&val);

fVal=floor(val);
cVal =ceil(val);
printf("floor value:%f \nceil value:%f\n",fVal,cVal);
return 0;
}

OUTPUT
Enter a float value: 34.5
floor value:34.000000
ceil value:35.000000

24. C program to read Formatted Time Once through Scanf in C Language

#include <stdio.h>

int main(){
int hour,minute,second;

printf("Enter time (in HH:MM:SS) ");


scanf("%02d:%02d:%02d",&hour,&minute,&second);

printf("Entered time is %02d:%02d:%02d\n",hour, minute, second);

return 0;
}

OUTPUT
Enter time (in HH:MM:SS) 09.56.01
Entered time is [Link]

25. C program to define, modify and access a global variable


#include <stdio.h>
int x=100;
void modify_x(int val)
{
x=val;
}
int main()
{
printf("1) Value of x: %d\n",x);

//modifying the value of x


x=200;
printf("2) Value of x: %d\n",x);

//modifying value from function


modify_x(300);
printf("3) Value of x: %d\n",x);

return 0;
}
OUTPUT

1) Value of x: 100
2) Value of x: 200
3) Value of x: 300

26. C program to convert feet to inches

#include<stdio.h>
int main()
{
int feet,inches;
printf("Enter the value of feet: ");
scanf("%d",&feet);
//converting into inches
inches=feet*12;
printf("Total inches will be: %d\n",inches);
return 0;
}
OUTPUT
Enter the value of feet: 12.5
Total inches will be: 144

27. C program to print value in Decimal, Octal, Hexadecimal using printf

#include <stdio.h>
int main()
{
int value=2567;

printf("Decimal value is: %d\n",value);


printf("Octal value is: %o\n",value);
printf("Hexadecimal value is (Alphabet in small letters): %x\n",value);
printf("Hexadecimal value is (Alphabet in capital letters): %X\n",value);

return 0;
}
OUTPUT

Decimal value is: 2567


Octal value is: 5007
Hexadecimal value is (Alphabet in small letters): a07
Hexadecimal value is (Alphabet in capital letters): A07

28. C program to print ASCII value of entered character

#include <stdio.h>

int main()
{
char ch;

//input character
printf("Enter the character: ");
scanf("%c",&ch);

printf("ASCII is = %d\n", ch);


return 0;
}

OUTPUT
Enter the character: S
ASCII is = 83

29. C program to print How Many Inputs are taken from Keyboard using
Scanf?

#include <stdio.h>

int main(){
int count=0;
int num;
int arr[100],i=0;

while(num!=-1){
printf("Enter an integer number (-1 to exit): ");
count+=scanf("%d",&num);
arr[i++]=num;
}
printf("\nTotal Inputs (including 0) are: %d\n",count);
printf("Entered numbers are:\n");
for(i=0;i<count;i++){
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}
OUTPUT
integer number (-1 to exit): 10
Enter an integer number (-1 to exit): 20
Enter an integer number (-1 to exit): 30
Enter an integer number (-1 to exit): 40
Enter an integer number (-1 to exit): -1

Total Inputs (including 0) are: 5


Entered numbers are:
10 20 30 40 -1

30. C Program to calculate Employee and Employer Provident Fund

#include <stdio.h>
#define EMPLOYEE_PERCENTAGE 12.5f
#define EMPLOYER_PERCENTAGE 12.0f
int main(){
float basicPay;
float employeeFund,employerFund;
printf("Enter basic pay: ");
scanf("%f",&basicPay);
employeeFund=(basicPay/100)*EMPLOYEE_PERCENTAGE;
employerFund=(basicPay/100)*EMPLOYER_PERCENTAGE;
printf("Basic Pay: %f\n",basicPay);
printf("Employee contribution: %f\n",employeeFund);
printf("Employer Contribution: %f\n",employerFund);
return 0;
}

OUTPUT
Enter basic pay: 4500
Basic Pay: 4500.000000
Employee contribution: 562.500000
Employer Contribution: 540.000000

31. C program to set buffer with specific value using memset in C - Example of
memset()

#include <stdio.h>
#include <string.h>
int main()
{
unsigned char buffer[10]={0};
int i;
printf("buffer: %s\n",buffer);
//set value with space
memset(buffer,' ', 9); //last byte should be null
printf("buffer (memset with space): %s",buffer); printf(".\n");
//set value with 'x'
memset(buffer,'x', 9); //last byte should be null
printf("buffer (memset with x): %s",buffer); printf(".\n");
//set value with value 15
memset(buffer,15, 9); //last byte should be null
printf("buffer (memset with value 15): %s",buffer); printf(".\n");
printf("buffer (memset with value 15 printing integer values:\n*** LAST VALUE
WILL BE NULL ***):\n");
for(i=0;i<10;i++){
printf("%02d ",buffer[i]);
}
printf(".\n");
return 0;
}
OUTPUT

buffer:
buffer (memset with space): .
buffer (memset with x): xxxxxxxxx.
buffer (memset with value 15): #########.
buffer (memset with value 15 printing integer values:
*** LAST VALUE WILL BE NULL ***):
15 15 15 15 15 15 15 15 15 00 .

32. Write a C program to evaluate the net salary of an employee given the
following constraints
#include <stdio.h>
int main()
{
//variable to store values
float basic, da, hra, ta, others;
float pf,it;
float net_salary;
printf("Enter Basic Salary ($): ");
scanf("%f",&basic);
printf("Enter HRA ($): ");
scanf("%f",&hra);
printf("Enter TA ($): ");
scanf("%f",&ta);
printf("Enter others ($): ");
scanf("%f",&others);
da = (basic*12)/100;
pf = (basic*14)/100;
it = (basic*15)/100;
net_salary = basic + da + hra + ta + others - (pf+it);
printf("Net Salary is: $ %.02f\n",net_salary);
return 0;
}

OUTPUT

Enter Basic Salary ($): 2500


Enter HRA ($): 500
Enter TA ($): 300
Enter others ($): 100
Net Salary is: $ 2975.00

33. C program to swap two integer numbers without using temporary variable

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

int a,b;
//Input values
printf("Enter the value of a: ");
scanf("%d",&a);
printf("Enter the value of b: ");
scanf("%d",&b);
printf("Before swapping... a: %d, b: %d\n",a,b);
a = a+b; //step 1
b = a-b; //step 2
a = a-b; //step 3
printf("After swapping... a: %d, b: %d\n",a,b);
return 0;
}

OUTPUT

Enter the value of a: 12


Enter the value of b: 15
Before swapping... a: 12, b: 15
After swapping... a: 15, b: 12

34. C program to read name and marital status of a girl and print her name
with Miss or Mrs

#include <stdio.h>
#include <string.h>

// main function
int main()
{
// Declare a char buffer to take input for name
char name[30]={0};
// Declare a char buffer to take input for answer
char YesNo[10]={0};
printf("Enter the name of a girl : ");
gets(name);
printf("Is the girl married (Y-Yes, N-No) : ");
gets(YesNo);
if((!strcmp(YesNo,"yes")) || (!strcmp(YesNo,"Y")))
printf("Her full name is : Mrs. %s",name);
else if((!strcmp(YesNo,"no")) || (!strcmp(YesNo,"N")))
printf("Her full name is : Miss %s",name);
else
printf("Marital status is wrong");

return 0;
}

OUTPUT

Enter the name of a girl : MEENA


Is the girl married (Y-Yes, N-No) : Y
Her full name is : Mrs. MEENA

35. C program to check given number is divisible by A and B


#include <stdio.h>

//function will return 1 if given number is


//divisible by both numbers a and b
int CheckDivision(int num, int a , int b)
{
//we have to check whether num is
//divisible by a and b
if( num%a==0 && num%b==0 )
return 1;
else
return 0;
}

// main function
int main()
{
int number=0,A=0,B=0;

printf("Enter any integer number : ");


scanf("%d",&number);

printf("Enter first divisor : ");


scanf("%d",&A);

printf("Enter second divisor : ");


scanf("%d",&B);

if(CheckDivision(number,A,B))
printf("%d is divisible by %d and %d\n",number,A,B);
else
printf("%d is not divisible by %d and %d\n",number,A,B);

return 0;
}

OUTPUT

Enter any integer number : 34


Enter first divisor : 2
Enter second divisor : 4
34 is not divisible by 2 and 4

Enter any integer number : 24


Enter first divisor : 2
Enter second divisor : 3
24 is divisible by 2 and 3
36. C program to find sum of all numbers from 0 to N without using loop

#include <stdio.h>
int main(void) {
int n, sum;
//input value of n
printf("Enter the value of n: ");
scanf("%d", &n);
//initialize sum with 0
sum =0;
//use formula to get the sum from 0 to n
sum = n*(n+1)/2;
//print sum
printf("sum = %d\n", sum);

return 0;
}

OUTPUT

Enter the value of n: 12


sum = 78

37. C program Input hexadecimal value in C language

#include <stdio.h>
int main()
{
unsigned char var1;
unsigned short var2;
unsigned int var3;
printf("Enter 1 byte value in Hexadecimal: ");
scanf("%x", &var1);
printf("Enter 2 bytes value in Hexadecimal: ");
scanf("%x", &var2);
printf("Enter 4 bytes value in Hexadecimal: ");
scanf("%x", &var3);
printf("Value of var1 in HEX: %X, Decimal: %d\n", var1,var1);
printf("Value of var2 in HEX: %X, Decimal: %d\n", var2,var2);
printf("Value of var3 in HEX: %X, Decimal: %d\n", var3,var3);
return 0;
}
OUTPUT

Enter 1 byte value in Hexadecimal: FA


Enter 2 bytes value in Hexadecimal: 20AC
Enter 4 bytes value in Hexadecimal: 1234ABD0
Value of var1 in HEX: FA, Decimal: 250
Value of var2 in HEX: 20AC, Decimal: 8364
Value of var3 in HEX: 1234ABD0, Decimal: 305441744

38. C program Printing an address of a variable in C

#include <stdio.h>
int main(void)
{
// declare variables
int a;
float b;
char c;
printf("Address of a: %p\n", &a);
printf("Address of b: %p\n", &b);
printf("Address of c: %p\n", &c);
return 0;
}

OUTPUT

Address of a: 0x7ffe6678d16c
Address of b: 0x7ffe6678d168
Address of c: 0x7ffe6678d167

39. printf() statement within another printf() statement in C

#include <stdio.h>

int main(void)
{
printf("%d", printf ("Hello"));
return 0;
}

OUTPUT

Hello5

40. printf() examples/variations in C

#include <stdio.h>
int main()
{
int num = 255;
int len = 0;
float val = 1.234567;
printf("Hello World");
printf("Hello world\nHow are you?");
printf("Hello \"World\", How are you?\n");
printf ("Hey I got 84.20%% in my final exams\n");
printf("num in octal format: %o\n", num);
printf ("num in hexadecimal format(lowercase) : %x\n", num);
printf ("num in hexadecimal format(uppercase) : %X\n", num);
printf ("Hello world, how are you?\
I love C programing language.\n");
printf ("The file is store at c:\\files\\word_files\n");
len = printf ("Hello\n");
printf ("Length: %d\n", len);
printf ("num (padded): %05d\n", num);
printf ("str1=\"%20s\", str2=\"%-20s\"\n", "Hello", "World");
printf("val = %.2f\n", val);
printf("num = %i \n", num);
printf("Address of num is: %p\n", &num);
return 0;
}

OUTPUT

Hello WorldHello world


How are you?Hello "World", How are you?
Hey I got 84.20% in my final exams
num in octal format: 377
num in hexadecimal format(lowercase) : ff
num in hexadecimal format(uppercase) : FF
Hello world, how are you? I love C programing language.
The file is store at c:\files\word_files
Hello
Length: 6
num (padded): 00255
str1=" Hello", str2="World "
val = 1.23
num = 255
Address of num is: 0x7ffc03e140e4

41. C program to calculate profit or loss

#include <stdio.h>
int main()
{
int cp;
int sp;
int amount;
printf("Enter the cost price: ");
scanf("%d", &cp);
printf("Enter the selling price: ");
scanf("%d", &sp);
if (sp > cp) {
/*formula to find profit*/
amount = sp - cp; OUTPUT
printf("profit = %d", amount); Enter the cost price: 250
} Enter the selling price: 270
else if (cp > sp) { profit = 20
/*formula to find loss*/
amount = cp - sp;
printf("loss = %d", amount);
}
else {
printf("No profit no loss\n");
}
return 0;
}

42. Calculate the distance between two cities from kilometers to meters,
centimeters, feet and inches using C program

#include <stdio.h>
int main()
{
/*Declare the variables*/
int distance;
float meter;
float feet;
float inches;
float centimeter;
printf("Enter the distance between Gwalior and Delhi (in KM): ");
scanf("%d", &distance);
meter = distance * 1000;
feet = distance * 3280.84;
inches = distance * 39370.1;
centimeter = distance * 100000;
printf("Meter = %f\n", meter);
printf("Feet = %f\n", feet);
printf("Inches = %f\n", inches);
printf("Centimeters = %f\n", centimeter);
return 0;
}
OUTPUT

Enter the distance between Gwalior and Delhi (in KM): 55


Meter = 55000.000000
Feet = 180446.203125
Inches = 2165355.500000
Centimeters = 5500000.000000

43. Calculating the distance in C: Here, we are implementing a C program that


will input distance between two cities in kilometers and print the distance in
meters, feet, and inches.

#include <stdio.h>
int main()
{
int distance;
float meter;
float feet;
float inches;
float centimeter;
printf(“Enter the distance between Gwalior and Delhi (in KM): “);
scanf(“%d”, &distance);
meter = distance * 1000;
feet = distance * 3280.84;
inches = distance * 39370.1;
centimeter = distance * 100000;
printf(“Meter = %f\n”, meter);
printf(“Feet = %f\n”, feet);
printf(“Inches = %f\n”, inches);
printf(“Centimeters = %f\n”, centimeter);
return 0;
}

OUTPUT
Enter the distance between Gwalior and Delhi (in KM): 55
Meter = 55000.000000
Feet = 180446.203125
Inches = 2165355.500000
Centimeters = 5500000.000000

44. C program to find area and perimeter of the rectangle

#include <stdio.h>
int main()
{
float length;
float breadth;
float area;
float perimeter;
printf("Enter the length: ");
scanf("%f", &length);
printf("Enter the breadth: ");
scanf("%f", &breadth);
area = length * breadth;
perimeter = (2 * length) + (2 * breadth);
printf("Area of the rectangle: %.02f\n", area);
printf("Perimeter of rectangle: %.02f\n", perimeter);
return 0;
}

OUTPUT

Enter the length: 20


Enter the breadth: 12
Area of the rectangle: 240.00
Perimeter of rectangle: 64.00

45. C program to generate random numbers within a range

rand() function in the C programming language is used to generate a random number.


The return type is of rand() function is an integer.

#include <stdio.h>
OUTPUT
#include <stdlib.h>
Random number is: 1804289383
int main(void)
{
printf("Random number is: %d ", rand());
return 0;
}

extra program : Program to generate random number between 1 to 100

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

int main()
{
int lower = 1, upper = 100, count = 10;
srand(time(0));
printf("The random numbers are: ");
for (int i = 0; i < count; i++) {
int num = (rand() % (upper - lower + 1)) + lower;
printf("%d ", num);
}

return 0;
}

46. C Example to subtract two integers without using Minus (-) operator

#include<stdio.h>

int main()
{
int a,b,sub;

//input numbers
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);

//find subtraction using ~


sub=a+~b+1;

printf("subtraction of %d-%d=%d",a,b,sub);

return 0;
}
OUTPUT

Enter first number: 45


Enter second number: 32
subtraction of 45-32=13

47. C Example for different floating point values prediction

#include <stdio.h>
int main()
{
float a=21.2;
double b=21.2;

if (a==b)
printf("Both values are equal");
else
printf("Both values are not equal");
return 0;
}

OUTPUT
Both values are not equal

48. C Example for nested 'printf'

#include <stdio.h>

int main()
{

printf("\t%d",printf("DCE06"));

return (0);

OUTPUT

DCE06 5

49. C program to get remainder without using % operator

#include <stdio.h>
int main()
{
int a,b,rem;
printf("Enter first number :");
scanf("%d",&a);
printf("Enter second number :");
scanf("%d",&b);

rem=a-(a/b)*b;

printf("Remainder is = %d\n",rem);
return 0;
}
OUTPUT
Enter first number :26
Enter second number :14
Remainder is = 12

50. C program to convert ascii to integer (atoi implementation)


#include <stdio.h>
#include <string.h>
*/int a2i(char*);
*function declaration
* name : a2i
int main() * Desc : to convert ascii to integer
{ * Parameter : char* - character string
char string[5]; * return : int
printf("Enter a string value: ");
scanf("%s", string);
printf("\nInteger value = %d\n", a2i(string));

return 0;
}

int a2i(char* txt)


{
int sum, digit, i;
sum = 0;
for (i = 0; i < strlen(txt); i++) {
digit = txt[i] - 0x30;
sum = (sum * 10) + digit;
}
return sum;
}
OUTPUT
ng vale: 123
Enter Enter a string value: 123

Integer value = 123

51. C program to print ASCII table

#include <stdio.h>
int main()
{
unsigned char count;
for(count=32; count< 255; count+=1)
{
printf(" %3d - %c",count,count);
if(count % 6==0)
printf("\n");
}
return 0;
}
52. C program to swap two numbers using four different methods

#include <stdio.h>

int main()
{
int a,b,t;
printf(" Enter value of A ? ");
scanf("%d",&a);
printf(" Enter value of B ? ");
scanf("%d",&b);
printf("\n Before swapping : A= %d, B= %d",a,b);

/****first method using third variable*/


t=a;
a=b;
b=t;
printf("\n After swapping (First method) : A= %d, B= %d\n",a,b);

/****second method without using third variable*/


a=a+b;
b=a-b;
a=a-b;
printf("\n After swapping (second method): A= %d, B= %d\n",a,b);

/****Third method using X-Or */


a^=b;
b^=a;
a^=b;
printf("\n After swapping (third method) : A= %d, B= %d\n",a,b);

/****fourth method (single statement)*/


a=a+b-(b=a);
printf("\n After swapping (fourth method): A= %d, B= %d\n",a,b);

return 0;
}

OUTPUT

Enter value of A ? 65
Enter value of B ? 67
Before swapping : A= 65, B= 67
After swapping (First method) : A= 67, B= 65

After swapping (second method): A= 65, B= 67


After swapping (third method) : A= 67, B= 65

After swapping (fourth method): A= 65, B= 67

53. C program to check a given character is alphanumeric or not without using


the library function

#include <stdio.h>
int isAlphaNumeric(char ch)
{
if ((ch >= '0' & ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
return 1;

return 0;
}
int main()
{
char ch = 0;

printf("Enter character: ");


scanf("%c", &ch);
if (isAlphaNumeric(ch))
printf("Given character is an alphanumeric character\n");
else
printf("Given character is not an alphanumeric character\n");
return 0;
}

OUTPUT

Enter character: T
Given character is an alphanumeric character

54. C program to check a given character is a digit or not without using the
library function

#include <stdio.h>
int isDigit(char ch)
{
if (ch >= '0' && ch <= '9')
return 1;
return 0;
}
int main()
{
char ch = 0;
printf("Enter character: ");
scanf("%c", &ch);

if (isDigit(ch))
printf("Given character is a digit\n");
else
printf("Given character is not a digit\n");

return 0;
}

OUTPUT

Enter character: 5
Given character is a digit

55. C program to check a given character is a whitespace character or not


without using the library function

#include <stdio.h>

int isWhiteSpace(char ch)


{
if ((ch == ' ') || (ch == '\t') || (ch == '\n') || (ch == 'v') || (ch == '\r') || (ch == '\f'))
return 1;

return 0;
}

int main()
{
char ch1 = ' ';
char ch2 = '\t';
char ch3 = 't';

if (isWhiteSpace(ch1))
printf("Given character is a whitespace character\n");
else
printf("Given character is not a whitespace character\n");

if (isWhiteSpace(ch2))
printf("Given character is a whitespace character\n");
else
printf("Given character is not a whitespace character\n");
if (isWhiteSpace(ch3))
printf("Given character is a whitespace character\n");
else
printf("Given character is not a whitespace character\n");

return 0;
}

OUTPUT

Given character is a whitespace character


Given character is a whitespace character
Given character is not a whitespace character

56. C program to check a given character is an uppercase character or not


without using the library function

#include <stdio.h>

int isUppercase(char ch)


{
if (ch >= 'A' && ch <= 'Z')
return 1;
return 0;
}

int main()
{
char ch;

printf("Enter character: ");


scanf("%c", &ch);

if (isUppercase(ch))
printf("Given character is an uppercase character\n");
else
printf("Given character is not an uppercase character\n");

return 0;
}

OUTPUT
Enter character: M
Given character is an uppercase character

57. C program to check a given character is a lowercase character or not


without using the library function
#include <stdio.h>

int isLowercase(char ch)


{
if (ch >= 'a' && ch <= 'z')
return 1;

return 0;
}

int main()
{
char ch;

printf("Enter character: ");


scanf("%c", &ch);

if (isLowercase(ch))
printf("Given character is a lowercase character\n");
else
printf("Given character is not a lowercase character\n");

return 0;
}

OUTPUT
Enter character: v
Given character is not an uppercase character

58. C program to check a given character is a punctuation mark or not without


using the library function

#include <stdio.h>
int isPunctuation(char ch)
{
if (ch == '!' || ch == '\"' || ch == '#' || ch == '$' || ch == '%' || ch == '&' || ch == '\'' || ch
== '(' || ch == ')' || ch == '*' || ch == '+' || ch == ',' || ch == '-' || ch == '.' || ch == '/' || ch
== ':' || ch == ';' || ch == '<' || ch == '=' || ch == '>' || ch == '?' || ch == '@' || ch == '[' || ch
== '\\' || ch == ']' || ch == '^' || ch == '`' || ch == '{' || ch == '|' || ch == '}')
return 1;

return 0;
}

int main()
{
char ch;

printf("Enter character: ");


scanf("%c", &ch);

if (isPunctuation(ch))
printf("Given character is a punctuation mark\n");
else
printf("Given character is not a punctuation mark\n");

return 0;
}

Enter character: T
Given character is not a punctuation mark

Enter character: 'T'


Given character is a punctuation mark

59. C program to check whether a character is a printable character or not


without using library function

#include <stdio.h>
#include <ctype.h>
int isPunctuation(char ch)
{
if (ch == '!' || ch == '\"' || ch == '#' || ch == '$' || ch == '%' || ch == '&' || ch == '\'' || ch
== '(' || ch == ')' || ch == '*' || ch == '+' || ch == ',' || ch == '-' || ch == '.' || ch == '/' || ch
== ':' || ch == ';' || ch == '<' || ch == '=' || ch == '>' || ch == '?' || ch == '@' || ch == '[' || ch
== '\\' || ch == ']' || ch == '^' || ch == '`' || ch == '{' || ch == '|' || ch == '}')
return 1;

return 0;
}

int isAlphaNumeric(char ch)


{
if ((ch >= '0' & ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
return 1;

return 0;
}

int isPrintable(char ch)


{
if (isAlphaNumeric(ch) || isPunctuation(ch))
return 1;
return 0;
}

int main()
{
char ch1 = 'a';
char ch2 = 'A';
char ch3 = 95;

if (isPrintable(ch1) != 0)
printf("Given character is a printable character\n");
else
printf("Given character is not a printable character\n");

if (isPrintable(ch2) != 0)
printf("Given character is a printable character\n");
else
printf("Given character is not a printable character\n");

if (isPrintable(ch3) != 0)
printf("Given character is a printable character\n");
else
printf("Given character is not a printable character\n");

return 0;
}

OUTPUT

Given character is a printable character


Given character is a printable character
Given character is not a printable character

60. C program to convert a lowercase character into uppercase without using


library function

#include <stdio.h>
void stringLwr(char *s);
void stringUpr(char *s);
*****************************************
int main() * function name :stringLwr, stringUpr
{ * Parameter :character pointer s
char str[100]; * Description
stringLwr - converts string into lower case
printf("Enter any string : "); stringUpr - converts string into upper case
scanf("%[^\n]s",str); ******************************************
stringLwr(str);
printf("String after stringLwr : %s\n",str);

stringUpr(str);
printf("String after stringUpr : %s\n",str);
return 0;
}

/******** function definition *******/


void stringLwr(char *s)
{
int i=0;
while(s[i]!='\0')
{
if(s[i]>='A' && s[i]<='Z'){
s[i]=s[i]+32;
}
++i;
}
}

void stringUpr(char *s)


{
int i=0;
while(s[i]!='\0')
{
if(s[i]>='a' && s[i]<='z'){
s[i]=s[i]-32;
}
++i;
}
}

OUTPUT

Enter any string : GOOD


String after stringLwr : good
String after stringUpr : GOOD

61. C program to convert an uppercase character into lowercase without using


library function

#include <stdio.h>

char toLowerCase(char ch)


{
ch = ch + 32;
return ch;
}

int main()
{
char ch;

printf("Enter a uppercase character: ");


scanf("%c", &ch);

printf("Lowercase character is: %c\n", toLowerCase(ch));

return 0;
}

OUTPUT

Enter a uppercase character: S


Lowercase character is: s

62. C program to print all punctuation marks without using library function

#include <stdio.h>
#include <ctype.h>

int main()
{
int cnt;
printf("Punctuation marks are: \n");

for (cnt = 0; cnt <= 127; cnt++)


if (ispunct(cnt) != 0)
printf("%c ", cnt);

printf("\n");

return 0;
}

Output
Punctuation marks are:
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
63. C program to print all punctuation marks using the ispunct() function

#include <stdio.h>
#include <ctype.h>

int main()
{
int cnt;
printf("Punctuation marks are: \n");

for (cnt = 0; cnt <= 127; cnt++)


if (ispunct(cnt) != 0)
printf("%c ", cnt);

printf("\n");

return 0;
}
Output
Punctuation marks are:
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

64. C program to print all printable characters using the isprint() function

#include <stdio.h>
#include <ctype.h>

int main()
{
int cnt;
printf("Printable characters are: \n");

for (cnt = 0; cnt <= 127; cnt++)


if (isprint(cnt) != 0)
printf("%c ", cnt);

printf("\n");

return 0;
}
Output
Printable characters are:
!"#$%&'()*+,-./
0123456789:;<=>?@
ABCDEFGHIJKLMNOPQRSTUVWXYZ
[\]^_`abcdefghijklmnopqrst
uvwxyz{|}~

65. C program to print all printable characters without using the library
function
#include <stdio.h>
#include <ctype.h>

int isPunctuation(char ch)


{
if (ch == '!' || ch == '\"' || ch == '#' || ch == '$' || ch == '%' || ch == '&' || ch == '\'' || ch
== '(' || ch == ')' || ch == '*' || ch == '+' || ch == ',' || ch == '-' || ch == '.' || ch == '/' || ch
== ':' || ch == ';' || ch == '<' || ch == '=' || ch == '>' || ch == '?' || ch == '@' || ch == '[' || ch
== '\\' || ch == ']' || ch == '^' || ch == '`' || ch == '{' || ch == '|' || ch == '}' || ch == '~')
return 1;

return 0;
}

int isAlphaNumeric(char ch)


{
if ((ch >= '0' & ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
return 1;

return 0;
}

int isPrintable(char ch)


{
if (isAlphaNumeric(ch) || isPunctuation(ch))
return 1;
return 0;
}

int main()
{
int cnt;
printf("Printable characters are: \n");

for (cnt = 0; cnt <= 127; cnt++)


if (isPrintable(cnt) != 0)
printf("%c ", cnt);
printf("\n");

return 0;
}

Output
Printable characters are:
!"#$%&'()*+,-./
0123456789:;<=>?@
ABCDEFGHIJKLMNOPQRSTUVWXYZ
[\]^`abcdefghijklmnopqrstu
vwxyz{|}~

66. C program to make a beep sound

#include <stdio.h>

int main(int argc, char* argv[])


{
printf("Beep sound started\n");
printf("\a");
printf("Beep sound stopped\n");

return 0;
}

Output:
Beep sound started
Beep sound stopped

67. C program to convert a given number of days into days, weeks, and years

#include <stdio.h>

int main()
{
int ndays = 0;
int years = 0;
int weeks = 0;
int days = 0;

printf("Enter days: ");


scanf("%d", &ndays);

years = ndays / 365;


weeks = (ndays % 365) / 7;
days = (ndays % 365) % 7;

printf("%d years, %d weeks and %d days\n", years, weeks, days);

return 0;
}

Output
RUN 1:
Enter days: 367
1 years, 0 weeks and 2 days

RUN 2:
Enter days: 1280
3 years, 26 weeks and 3 day

RUN 3:
Enter days: 11
0 years, 1 weeks and 4 days

68. C program to find the roots of a quadratic equation

include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;

// condition for real and different roots


if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}

// condition for real and equal roots


else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}

// if roots are not real


else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart,
realPart, imagPart);
}

return 0;
}
Run Code

Output

Enter coefficients a, b and c: 2.3


4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i

69. C program to find the GCD (Greatest Common Divisor) of two integers

#include <stdio.h>

int main()
{
int num1 = 0;
int num2 = 0;
int rem = 0;
int X = 0;
int Y = 0;

printf("Enter Number1: ");


scanf("%d", &num1);

printf("Enter Number2: ");


scanf("%d", &num2);

if (num1 > num2) {


X = num1;
Y = num2;
}
else {
X = num2;
Y = num1;
}
rem = X % Y;
while (rem != 0) {
X = Y;
Y = rem;
rem = X % Y;
}
printf("Greatest Common Divisor is: %d\n", Y);

return 0;
}
Output
RUN 1:
Enter Number1: 10
Enter Number2: 20
Greatest Common Divisor is: 10

RUN 2:
Enter Number1: 10
Enter Number2: 225
Greatest Common Divisor is: 5

RUN 3:
Enter Number1: 110
Enter Number2: 17
Greatest Common Divisor is: 1

70. C program to find the LCM (Lowest Common Multiple) of two integers

#include <stdio.h>
int main()
{
int num1 = 0;
int num2 = 0;
int rem = 0;
int lcm = 0;
int X = 0;
int Y = 0;
printf("Enter Number1: ");
scanf("%d", &num1);

printf("Enter Number2: ");


scanf("%d", &num2);

if (num1 > num2) {


X = num1;
Y = num2;
}
else {
X = num2;
Y = num1;
}

rem = X % Y;

while (rem != 0) {
X = Y;
Y = rem;
rem = X % Y;
}

lcm = num1 * num2 / Y;


printf("Lowest Common Multiple is: %d\n", lcm);

return 0;
}

Output
RUN 1:
Enter Number1: 10
Enter Number2: 20
Lowest Common Multiple is: 20

RUN 2:
Enter Number1: 10
Enter Number2: 225
Lowest Common Multiple is: 450

RUN 3:
Enter Number1: 110
Enter Number2: 17
Lowest Common Multiple is: 1870

71. C program to calculate the area of a triangle given three sides

#include <stdio.h>
#include <math.h>
float calcuateAreaTriangle(int a, int b, int c)
{
float s = 0;
float area = 0;
s = (float)(a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
return area;
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
float area = 0;
printf("Enter the First edge of Triangle: ");
scanf("%d", &a);
printf("Enter the Second edge of Triangle: ");
scanf("%d", &b);
printf("Enter the Third edge of Triangle: ");
scanf("%d", &c);
area = calcuateAreaTriangle(a, b, c);
printf("Area of a triangle: %f\n", area);
return 0;
}
OUTPUT

Enter the First edge of Triangle: 2


Enter the Second edge of Triangle: 3
Enter the Third edge of Triangle: 4
Area of a triangle: 2.904737

72. C program to calculate the area of a triangle given base and height
#include < stdio.h >
int main()
{
float base, height, area;
printf("Enter length of base of Triangle\n");
scanf("%f", &base);
printf("Enter length of height of Triangle\n");
scanf("%f", &height);
area = (base * height) / 2.0;
printf("Area of Triangle is %0.2f\n", area);
return 0;
}
OUTPUT
Enter length of base of Triangle
15
Enter length of height of Triangle
25
Area of Triangle is 187.50

73. C program to calculate the area of Trapezium

#include <stdio.h>
int main(){
int b1, b2, height;
float area;
printf("Enter Base One: ");
scanf("%d", &b1);
printf("Enter Base Two: ");
scanf("%d", &b2);
printf("Enter the height: ");
scanf("%d", &height);
area = 0.5 * (b1 + b2) * height;
printf("Area of Trapezium: %.2f", area);
return 0;
}
OUTPUT

Enter Base One: 15


Enter Base Two: 25
Enter the height: 40
Area of Trapezium: 800.00

74. C program to calculate the area of the rhombus

#include <stdio.h>

int main()
{
float diagonal1, diagonal2;
float area;

printf("Enter diagonals of the given rhombus: \n ");


scanf("%f%f", &diagonal1, &diagonal2);
area = 0.5 * diagonal1 * diagonal2;
printf("Area of rhombus is: %.3f \n", area);
return 0;
}

output

Enter diagonals of the given rhombus:


30 40
Area of rhombus is: 600.000

75. C program to calculate the area of Parallelogram

#include <stdio.h>

int main()
{
float base, altitude;
float area;
printf("Enter base and altitude of the given Parallelogram: \n ");
scanf("%f%f", &base, &altitude);
area = base * altitude;
printf("Area of Parallelogram is: %.3f\n", area);
return 0;
}

OUTPUT
Enter base and altitude of the given Parallelogram:
17 19
Area of Parallelogram is: 323.000

76. C program to calculate the area of Cube

#include <stdio.h>
int main(){
int side;
float area;

printf("Enter the side of the cube: ");


scanf("%d", &side);

// C Program to Find Surface Area of a C


area = 6 * side * side;

printf("Surface Area of Cube: %.2f", area);


return 0;
}

OUTPUT
Enter the side of the cube: 2
Surface Area of Cube: 24.00

77. C program to calculate the volume of Cube

#include <stdio.h>
int main(){
int side;
float volume;

printf("Enter the side of the cube: ");


scanf("%d", &side);

volume= pow(side, 3);

printf("Volume of Cube: %6.2f", volume);


return 0;
}

OUTPUT

Enter the length of a side


34
Volume = 3930.40

78. C program to find the Surface Area and Volume of the Cylinder

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

int main(){

float r,h;
float surface_area,volume;

printf("Enter size of radius and height of a cylinder : ");


scanf("%f%f",&r,&h);
surface_area = 2 * M_PI * r * (r + h);
volume = M_PI * r * r * h;

printf("Surface area of cylinder is: %.3f",surface_area);


printf("\nVolume of cylinder is : %.3f",volume);

return 0;
}

OUTPUT
Enter size of radius and height of a cylinder: 4 10
Surface area of cylinder is: 351.858
Volume of cylinder is: 502.655

79. C program to calculate the surface area, volume, and space diagonal of
cuboids

#include <stdio.h>
#include <math.h>
int main()
{
float height = 0;
float width = 0;
float length = 0;
float volume = 0;
float area = 0;
float diagonal = 0;

printf("Enter the height: ");


scanf("%f", &height);
printf("Enter the width: ");
scanf("%f", &width);
printf("Enter the length: ");
scanf("%f", &length);
diagonal = sqrt(width * width) + (length * length) + (height * height);
area = 2 * (width * length) + (length * height) + (height * width);
volume = width * length * height;
printf("Volume of Cuboids is: %f\n", volume);
printf("Surface area of Cuboids is : %f\n", area);
printf("Space diagonal of Cuboids is: %f\n", diagonal);
return 0;
}

OUTPUT
Enter the height: 10
Enter the width: 20
Enter the length: 30
Volume of Cuboids is: 6000.000000
Surface area of Cuboids is: 1700.000000
Space diagonal of Cuboids is: 1020.000000

80. C program to calculate the surface area, volume of Cone

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

int main()
{
float height = 0;
float radius = 0;
float volume = 0;
float area = 0;
printf("Enter the height: ");
scanf("%f", &height);
printf("Enter the radius: ");
scanf("%f", &radius);
volume = (1.0 / 3) * (3.14) * radius * radius * height;
area = (3.14) * radius * (radius + sqrt(radius * radius + height * height));
printf("Volume of Cone: %f\n", volume);
printf("Surface area of Cone: %f\n", area);
return 0;
}

OUTPUT
Enter the height: 12.3
Enter the radius: 4
Volume of Cone: 205.984009
Surface area of Cone: 212.691849

81. C program to calculate the surface area, volume of the Sphere

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

int main()
{
float radius = 0;
float volume = 0;
float area = 0;
printf("Enter the radius: ");
scanf("%f", &radius);
volume = (4.0 / 3) * (3.14) * pow(radius, 3);
area = 4 * (3.14) * pow(radius, 2);
printf("Volume of Sphere %f\n", volume);
printf("Surface area of Sphere: %f\n", area);
return 0;
}

OUTPUT

Enter the radius: 2.45


Volume of Sphere: 61.569649
Surface area of Sphere: 75.391403

82. C program to calculate the mean, variance, and standard deviation of real
numbers

#include <stdio.h>
#include <math.h>
int main()
{
float arr[5] = { 12.5, 14.5, 13.2, 8.95, 17.89 };
float sum;
float mean;
float variance;
float deviation;
int i = 0;
for (i = 0; i < 5; i++)
sum = sum + arr[i];
mean = sum / 5;
sum = 0;
for (i = 0; i < 5; i++) {
sum = sum + pow((arr[i] - mean), 2);
}
variance = sum / 5;

deviation = sqrt(variance);
printf("Mean of elements : %.2f\n", mean);
printf("variance of elements: %.2f\n", variance);
printf("Standard deviation : %.2f\n", deviation);
return 0;
}

Output
Mean of elements : 13.41
variance of elements: 8.40
Standard deviation : 2.90
83. C program to read coordinate points and determine its quadrant

#include <stdio.h>
void main()
{
int x, y;
printf("Enter the values for X and Y\n");
scanf("%d %d", &x, &y);
if (x > 0 && y > 0)
printf("point (%d, %d) lies in the First quandrant\n");
else if (x < 0 && y > 0)
printf("point (%d, %d) lies in the Second quandrant\n");
else if (x < 0 && y < 0)
printf("point (%d, %d) lies in the Third quandrant\n");
else if (x > 0 && y < 0)
printf("point (%d, %d) lies in the Fourth quandrant\n");
else if (x == 0 && y == 0)
printf("point (%d, %d) lies at the origin\n");
}

OUTPUT

Enter the values for X and Y


20 30
point (-1079549476, -1079549480) lies in the First quandrant

84. C program to calculate the value of nCr

#include <stdio.h>
int getFactorial(int num)
{
int f = 1;
int i = 0;
if (num == 0)
return 1;
for (i = 1; i <= num; i++)
f = f * i;
return f;
}
int main()
{
int n = 0;
int r = 0;
int nCr = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
printf("Enter the value of R: ");
scanf("%d", &r);
nCr = getFactorial(n) / (getFactorial(r) * getFactorial(n - r));
printf("The nCr is: %d\n", nCr);
return 0;
}

OUTPUT
Enter the value of N: 7
Enter the value of R: 3
The nCr is: 35

85. C program to calculate the value of nPr

#include <stdio.h>
int getFactorial(int num)
{
int f = 1;
int i = 0;
if (num == 0)
return 1;
for (i = 1; i <= num; i++)
f = f * i;
return f;
}
int main()
{
int n = 0;
int r = 0;
int nPr = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
printf("Enter the value of R: ");
scanf("%d", &r);
nPr = getFactorial(n) / getFactorial(n - r);
printf("The nPr is: %d\n", nPr);
return 0;
}

OUTPUT
Enter the value of N: 7
Enter the value of R: 3
The nPr is: 210

86. c program to calculate the product of two binary numbers.

#include <stdio.h>
int binaryProduct(int binNum1, int binNum2)
{
int i = 0;
int rem = 0;
int product = 0;
int sum[20] = { 0 };
while (binNum1 != 0 || binNum2 != 0) {
sum[i] = (binNum1 % 10 + binNum2 % 10 + rem) % 2;
rem = (binNum1 % 10 + binNum2 % 10 + rem) / 2;
binNum1 = binNum1 / 10;
binNum2 = binNum2 / 10;
i = i + 1;
}
if (rem != 0)
sum[i] = rem;

while (i >= 0) {
product = product * 10 + sum[i];
i = i - 1;
}
return product;
}
int main()
{
long binNum1 = 0;
long binNum2 = 0;
long product = 0;
int digit = 0;
int factor = 1;
printf("Enter Number1: ");
scanf("%ld", &binNum1);
printf("Enter Number2: ");
scanf("%ld", &binNum2);
while (binNum2 != 0) {
digit = binNum2 % 10;
if (digit == 1) {
binNum1 = binNum1 * factor;
product = binaryProduct(binNum1, product);
}
else {
binNum1 = binNum1 * factor;
}
binNum2 = binNum2 / 10;
factor = 10;
}
printf("Product of numbers: %ld", product);
return 0;
}
OUTPUT
Enter Number2: 1110
Enter Number2: 10100
Product of numbers: 100011000

87. C program to extract the last two digits from a given year
#include <stdio.h>
int main()
{
int year = 0;
int res = 0;
printf("Enter year: ");
scanf("%d", &year);
res = year % 100;
printf("Result is: %02d", res);
return 0;
} OUTPUT
Enter year: 2021
Result is: 21

You might also like