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

C PROGRAMMING REVISION WORKSHEET OK

Uploaded by

golsiaraoul9
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C PROGRAMMING REVISION WORKSHEET OK

Uploaded by

golsiaraoul9
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Topic 5: C PROGRAMMIING

A. Program to calculate the area and the circumference of a circle.

Introduction
Here we are writing a simple C program that calculates the area and circumference of circle
based on the radius value provided by user.
Formula:
Area = 3.14 * radius * radius
Circumference = 2 * 3.14 * radius

To calculate area and circumference we must know the radius of circle. The program will prompt
user to enter the radius and based on the input it would calculate the values. To make it simpler
we have taken standard PI value as 3.14 (constant) in the program. Other details are mentioned
as comments in the below example program.

#include <stdio.h>
int main()
{
int circle_radius;
float PI_VALUE=3.14, circle_area, circle_circumf;

//Ask user to enter the radius of circle


printf("\nEnter radius of circle: ");
//Storing the user input into variable circle_radius
scanf("%d",&circle_radius);

//Calculate and display Area


circle_area = PI_VALUE * circle_radius * circle_radius;
printf("\nArea of circle is: %f",circle_area);

//Caluclate and display Circumference


circle_circumf = 2 * PI_VALUE * circle_radius;
printf("\nCircumference of circle is: %.2f",circle_circumf);

return(0);
}

OUTPUT:

Enter radius of circle: 2


Area of circle is: 12.56
Circumference of circle is: 12.56
B. A C Program To Check If A Number Is Odd Or Even.

If a number is exactly divisible by 2 then its an even number else it is an odd number. In this
article we will check whether the input number is even or odd.

#include<stdio.h>
int main()
{
// This variable is to store the input number
int num;

printf("Enter an integer: ");


scanf("%d",&num);

// Modulus (%) returns remainder


if ( num%2 == 0 )
printf("%d is an even number", num);
else
printf("%d is an odd number", num);

return 0;
}

Output
C. C Program To Check Whether A Number Is Positive Or Negative

Introduction

In this example, you will learn to check whether a number (entered by the user) is negative or
positive. This program takes a number from the user and checks whether that number is either.

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

if (num < 0)
printf("You entered a negative number.");
else if (num > 0)
printf("You entered a positive number.");
else
printf("You entered 0");

return 0;
}
1. C program to calculate simple interest using function

In this example, we will create a function which calculates Simple interest and will call this
function from main method. The User will prompt to enter Principal Amount, the interest rate,
and the duration or the period. Then the program will calculate the interest

#include<stdio.h>
int main()
{
int P,N ;
float SI, R;

printf("Enter Principal Amount : ");


scanf("%d",&P);

printf("Enter Interest-Rate : ");


scanf("%f",&R);

printf("Enter Time Period : ");


scanf("%d",&N);

SI=P*R*N/100;

printf("\nSimple-Interest : %.2f\n",SI);

return 0;
}

Output:

Enter Principal Amount : 500


Enter Interest-Rate : 5
Enter Time Period : 6
Simple-Interest : 150.00
D. C program to convert Fahrenheit to Celsius

Introduction

In this article, you will learn and get code about conversion of temperature value from Fahrenheit
to Celsius. Before going to the program, let's understand about its formula used in conversion.

The Fahrenheit to Celsius Formula is:


celsius = (fahrenheit-32)*(100/180)

The formula given above can also be written as:


celsius = (fahrenheit-32)/(180/100)
And as the value of 180/100 is 1.8, so the previous formula can be further written as:
celsius = (fahrenheit-32)/1.8

#include<stdio.h>
#include<conio.h>
int main()
{
float fahrenheit, celsius;
printf("Enter Temperature Value (in Fahrenheit): ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit-32)/1.8;
printf("\nEquivalent Temperature (in Celsius) = %0.2f",
celsius);
getch();
return 0;
}

Now supply any temperature value (in Fahrenheit) say 98.6 and press ENTER key to see the
following output:
E. Program to compute quotient and remainder.

In this example, you will learn to find the quotient and remainder when an integer is divided by
another integer.

#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);

// Computes quotient
quotient = dividend / divisor;

// Computes remainder
remainder = dividend % divisor;

printf("Quotient = %d\n", quotient);


printf("Remainder = %d", remainder);
return 0;
}

In this program, the user is asked to enter two integers (dividend and divisor). They are stored
in variables dividend and divisor respectively.

printf("Enter dividend: ");


scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);
Then the quotient is evaluated using / (the division operator), and stored in quotient.
quotient = dividend / divisor;
Similarly, the remainder is evaluated using % (the modulo operator) and stored in remainder.
remainder = dividend % divisor;
Finally, the quotient and remainder are displayed using printf().

printf("Quotient = %d\n", quotient);


printf("Remainder = %d", remainder);
F. Program to find sum of natural numbers using for loop
The user enters the value of n and the program calculate the sum of first n natural numbers using
for loop.

#include <stdio.h>
int main()
{
int n, count, sum = 0;
printf("Enter the value of n(positive integer): ");
scanf("%d",&n);
for(count=1; count <= n; count++)
{
sum = sum + count;
}
printf("Sum of first %d natural numbers is: %d",n, sum);
return 0;
}

Output:

Enter the value of n(positive integer): 6


Sum of first 6 natural numbers is: 21

Version 2
#include <stdio.h>
int main()
{
int n, count, sum = 0;
printf("Enter the value of n(positive integer): ");
scanf("%d",&n);

/* When you use while loop, you have to initialize the


* loop counter variable before the loop and increment
* or decrement it inside the body of loop like we did
* for the variable "count"
*/
count=1;
while(count <= n){
sum = sum + count;
count++;
}
printf("Sum of first %d natural numbers is: %d",n, sum);
return 0;
}
G. C Program to calculate the factorial of a number

Introduction

Factorial: The Factorial of a specified number refers to the


product of all given series of consecutive whole numbers
beginning with 1 and ending with the specified number
We use the “!” to represent factorial
Example:
5! = 1 x 2 x 3 x 4 x 5 = 120

#include<stdio.h>
int main(){
int i,
int fact;
int num;
fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
for(i=1;i<=num;i++)
fact = fact * i;
printf("%d! = %d\n", num, f);
return 0;
}

H. C Program to find the average of two numbers


Here we will write two C programs to find the average of two numbers(entered by user).
#include <stdio.h>
int main()
{
int num1, num2;
float avg;
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
avg =(num1+num2)/2;
printf("Average of %d and %d is: %.2f",num1,num2,avg);
return 0;
}
Output:
Enter first number: 12
Enter second number: 13
Average of 12 and 13 is: 12.50

I. C program to calculate a volume of a cylinder

/* This C program calculate the volume of a cylinder*/


#include<stdio.h>
int main(void)
{
int r ;
int h;
float V ;
float pi;
pi= 3.14;
printf("Enter the radius of the cylinder in meter\n");
scanf("%d", &r);
printf("Enter the height of the cylinder in meter \n");
scanf("%d", &h);
V=pi*r*r*h;
printf("The volume of a cylinder of %d m radius and %d m height in cubic meter is %.2f:",r,h,V );
return 0;
}

J. Program to calculate the Area of a Circle( MOCK P3 OL CSC 2022)

#include<stdio.h>
int main(void)
{
int R ;
int h;
Int A ;
Float pi= 3.14;
printf("Enter the radius of a circle \n");
scanf("%d", &R);
A=pi*R*R;
printf("The Area of the circle is is %d\n:"A );
return 0;
}
a. Type the program above, compile the program. If any errors correct till it compiles, then
save it as Task3B.
b. What is this program supposed to do?
c. Run the program and input the value 4 for the radius when prompt for a number. Write
the output you observe below.

K. C Program to convert from Cm, to m(GCE OL CSC P3 2021)

#include<stdio.h>
int main(void)
{
int m, cm, mum ;
printf("Enter the number of centimetres: ");
scanf("%d", &num);
m=num/100;
cm=num%100;
printf("The %d cm is equivalent to %d m and %d cm\n”, num, m, cm);
char c = getchar();
}
a. Type the C program compile and run, if any errors keep correcting and compiling
until all the errors are corrected.
b. Save as Task3
c. Run the program two times, providing 1250 as input the first time, and 45678 as input the
second time. Write the output you observe each time in the spaces provided below.
Input of 1250
Output __________________________________________________________
_________________________________________________________________
Input of 45678
Output____________________________________________________________
__________________________________________________________________
L. C Program to Check the wearing of face mask to an entrance. (GCE OL CSC P3 2022)

The C program below are designed to allow entrance through the gates only for people wearing
a face mask and whose temperatures are below 39.
#include<stdio.h>
int main(void)
{
Float temp;
Char mask;
printf("wearing a mask (Y/N)?:");
scanf("%c", &mask);
printf("what is the temperature?:" );
scanf("%f", &temp);
if ((temp>=39) && ( mask==”N”))
{ printf(“Temperature is not OK\n”);
Printf ( “mask is not Ok\n”);
printf(“Do not open Gate\n”);
}
Else if ( mask==”N”)
{
Printf ( “mask is not Ok\n”);
printf(“Temperature is OK\n”);
printf(“Do not open Gate\n”);
}
Else if (temp>=39)
{ Printf ( “mask is Ok\n”);
printf(“Temperature is not OK\n”);
printf(“Do not open Gate\n”);
}
return 0;
}
C Program to find the area of sector

Area of a Sector formula


Area of a Sector = (π * radius * radius * central angle)/360

This C program gets radius and central angle as user inputs and computes the area of a sector.

#include<stdio.h>

void main()
{
int radius, central_angle;
float area=0;
printf("\nFinds Area of a sector\n-------------------");
printf("\nEnter radius: ");
scanf("%d", &radius);
printf("Enter center angle:");
scanf("%d", ¢ral_angle);

area = (3.14*radius*radius*central_angle)/360;

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


}

Output:

$ cc area-of-sector.c
$ ./a.out

Finds Area of a sector


-------------------
Enter radius: 8
Enter center angle:60
Area of Sector: 33.49
C Program To Calculate Profit Percentage

This c program is used to find the profit percentage.

Profit Percentage Algorithm


Profit = Sold Amount - Cost Amount
Profit Percentage = (Profit * 100/Cost Amount)

Profit Percentage C Program


#include<stdio.h>

void main()
{
double cost, sold, profit, profit_per;
printf("Enter cost amount: ");
scanf("%lf", &cost);
printf("Enter sold amount: ");
scanf("%lf", &sold);
profit = sold - cost;
profit_per = ((profit *100)/cost);
printf("Profit Percentage: %lf\n", profit_per);
}
Output:

Enter cost amount: 3000


Enter sold amount: 4000
Profit Percentage: 33.333333

C Program to find the area of circle

Area of a Circle formula


Area of a Circle = π * (radius*radius)
C Program to find the area of circle
This C program gets radius as user inputs and computes the area of a circle.

#include<stdio.h>

void main()
{
int radius;
float area=0;
printf("\nFinds area of circle\n-------------------");
printf("\nEnter radius: ");
scanf("%d", &radius);
area = 3.14*(radius*radius);
printf("Circle Area: %.2f", area);
}

Output:
Finds area of circle
-------------------
Enter radius: 8
Circle Area: 200.96
C Program to find the area of circle

Area of Semi-circle formula


Area of Semi-circle = ( π * radius*radius)/2
C Program to find the area of circle
This C program gets radius as user inputs and computes the area of semi-circle.

#include<stdio.h>

void main()
{
int radius;
float area=0;
printf("\nFinds area of semi-circle\n-------------------");
printf("\nEnter radius: ");
scanf("%d", &radius);

area = (3.14*radius*radius)/2;

printf("Semi-circle Area: %.2f", area);


}

Output:

Finds area of semi-circle


-------------------
Enter radius: 8
Semi-circle Area: 100.48
C Program To Print Multiplication Table
#include<stdio.h>

#define COLMAX 5
#define ROWMAX 5

void main()
{
int row, column, val;
row = 1;
printf(" Multiplication Table \n");
printf("----------------------------\n");
do
{
column = 1;
do
{
val = row*column;
printf("%4d", val);
column += 1;
}
while(column <= COLMAX);
printf("\n");
row += 1;
}
while(row <= ROWMAX);
printf("----------------------------\n");
}

Output:

$ cc multiplication-table.c
$ ./a.out
Multiplication Table
----------------------------
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
----------------------------
C Program to calculate number of weeks from days

This C program gets the number of days as user input and computes the number of weeks and
remaining days.

Also finds the number of months and number of years from the number of days user input.

#include<stdio.h>

void main()
{
int days, weeks, months, weeks_rem_days, months_rem_days, years,
years_rem_days;;
printf("Enter the number of days: ");
scanf("%d", &days);
weeks = days/7;
weeks_rem_days = days % 7;
months = days / 30;
years = days / 365;
weeks_rem_days = days % 7;
months_rem_days = days % 30;
years_rem_days = days % 365;

printf("\nWeeks: %d weeks and %d days,", weeks, weeks_rem_days);


printf("\nMonthss: %d months and %d days,", months, months_rem_days);
printf("\nYears: %d years and %d days,", years, years_rem_days);
}

Output

$ cc arithmetic.c
$ ./a.out
Enter the number of days: 650

Weeks: 92 weeks and 6 days,


Monthss: 21 months and 20 days,
Years: 1 years and 285 days,
C Program to find the sales tax

This c program is used to compute the sales tax based on user inputs cost and tax rate or
percentage.

Formula to find sales tax


Sales tax = price of item × tax rate

C Program to calculate the sales tax


#include<stdio.h>

void main()
{
double cost, tax_rate, sales_tax;
printf("Enter cost amount: ");
scanf("%lf", &cost);
printf("Enter tax rate: ");
scanf("%lf", &tax_rate);

sales_tax = cost*(tax_rate/100);;
printf("Sales tax: %.2lf\n", sales_tax);
}

Output:

$ cc sales-tax.c
$ ./a.out
Enter cost amount: 5000
Enter tax rate: 12
Sales tax: 600.00
C Program to implement banking system using switch case

This c program reads the deposit, withdraw amounts and computes the interest and balance based
on the user inputs and selected operations sequences.

This c program uses switch case to handle different logics like deposit, withdraw, calculate
interest and checking balance etc..

While loop is used to run the program indefinitely till user manually quit the program.

#include<stdio.h>

void main()
{
int balance=0, deposit, withdraw;
float ci;
char op;

while(1)
{
printf("\nBanking System");
printf("\n................");
printf("\nD ->Deposit");
printf("\nW ->Withdraw");
printf("\nB ->Balance");
printf("\nI ->Interest");
printf("\nQ ->Quit");
printf("\nEnter operation: ");
scanf(" %c", &op);
switch(op)
{
case 'D':
printf("\nEnter deposit amount: ");
scanf("%d", &deposit);
balance += deposit;
break;
case 'W':
printf("\nEnter withdraw amount: ");
scanf("%d", &withdraw);
balance -= withdraw;
break;
case 'B':
printf("Balance: %d", balance);
break;
case 'I':
ci = (float)balance*4/100;
balance += ci;
printf("\nInterest: %f", ci);
break;
case 'Q':
return;
default:
printf("Invalid Operation!");
}
}
}
OUTPUT
$ cc banking-system.c
$ ./a.out

Banking System
................
D ->Deposit
W ->Withdraw
B ->Balance
I ->Interest
Q ->Quit
Enter operation: D

Enter deposit amount: 70000


Banking System
................
D ->Deposit
W ->Withdraw
B ->Balance
I ->Interest
Q ->Quit
Enter operation: B
Balance: 70000
Banking System
................
D ->Deposit
W ->Withdraw
B ->Balance
I ->Interest
Q ->Quit
Enter operation: W
Enter withdraw amount: 25000

Banking System
................
D ->Deposit
W ->Withdraw
B ->Balance
I ->Interest
Q ->Quit
Enter operation: B
Balance: 45000
Banking System
................
D ->Deposit
W ->Withdraw
B ->Balance
I ->Interest
Q ->Quit
Enter operation: I
Interest: 1800.000000
Banking System
................
D ->Deposit
W ->Withdraw
B ->Balance
I ->Interest
Q ->Quit
Enter operation: B
Balance: 46800
Banking System
................
D ->Deposit
W ->Withdraw
B ->Balance
I ->Interest
Q ->Quit
Enter operation: Q

C program to find the volume of a sphere

This c program is used to compute the volume of a sphere based on the user inputs radius.

Formula to compute volume of a sphere:


Volume of a sphere = (4/3) × π × r3

here pi denotes Greek letter π and value is around 3.14

C program to calculate the volume of a sphere


// Finds volume of a sphere
#include<stdio.h>

void main()
{
int radius;
float volume=0, pi=3.14;
printf("\nFinds volume of a sphere\n-------------------");
printf("\nEnter radius: ");
scanf("%d", &radius);

volume = ((float)4/(float)3)*pi*(radius*radius*radius);

printf("Volume of a sphere is: %.2f", volume);


}

Output:

$ cc volume-of-sphere.c
$ ./a.out

Finds volume of a sphere


-------------------
Enter radius: 8
Volume of a sphere is: 2143.57
C program to find the volume of a cube

This c program is used to find the volume of a cube and length is provided as the user input.

Formula to compute volume of a cube


Volume of a cube = length × length × length

C program to calculate the volume of a cube


// Finds volume of a cube
#include<stdio.h>

void main()
{
int length;
float volume=0;
printf("\nFinds volume of a cube\n-------------------");
printf("\nEnter length of one side: ");
scanf("%d", &length);

volume = (length*length*length);

printf("Volume of a cube is: %.2f", volume);


}

Output:

$ cc volume-of-cube.c
$ ./a.out

Finds volume of a cube


-------------------
Enter length of one side: 6
Volume of a cube is: 216.00
C Program Examples

C program to cmpute matrix addition using two dimensional array


C program to cmpute matrix subtraction using two dimensional array
C program to cmpute matrix multiplication using two dimensional array
C program to compute different order of matrix multiplication
C program to generate identity matrix for required rows and columns
C program to validate whether matrix is identity matrix or not
C program to validate whether matrix is sparse matrix or not
C program to validate whether matrix is dense matrix or not
C program to generate string as matrix diagonal characters using two dimesional array
C Program to find number of weeks, months and years from days
C Program To Implement Linked List and Operations
C Program To Implement Sorted Linked List and Operations
C Program to Reverse the Linked List
C Program to Stack and Operations using Linked List
C Program to Queue and Operations using Linked List
C Program to calculate multiplication of two numbers using pointers
C Program To Calculate Median
C Program To Calculate Standard Deviation
C Program For Fahrenheit To Celsius Conversion
C Program To Calculate Average
C Program For Quadratic Equations
C Program To Check Character Type
C Program To Find Largest Of Three Values
C Program To Find Max Value In Array
C Program to Find Min Value In Array
C Program to Print Multiplication Table
C Program for Frequency Counting
C Program to read a line of text
C Program To Find ASCII Value For Any Character
C Program To Find A Character Is Number, Alphabet, Operator, or Special Character
C Program To Find Reverse Case For Any Alphhabet using ctype functions
C Program To Find Number Of Vowels In Input String
C Program Pointers Example Code
C Program To Find Leap Year Or Not
C Program To Swap Two Integers Using Call By Reference
C Program To Swap Two Integers Without Using Third Variable
C Program To List Prime Numbers Upto Limit
C Program To List Composite Numbers Upto Limit
C Program To Calculate Compound Interest
C Program To Calculate Depreciation Amount After of Before Few Years
C Program To Calculate Profit Percentage
C Program To Calculate Loss Percentage
C Program To Find String Is Polindrome Or Not
C Program To Find Factorial of a Number
C Program To Check Number is a Polindrome or Not
C Program To Generate Random Integers
C Program To Generate Random Float Numbers
C Program to find Square Root of a Number
C Program to find Area of a Rectangle
C Program to find Perimeter of a Rectangle
C Program to find Area of a Square
C Program to find Area of a Triangle
C Program to find Area of a Parallelogram
C Program to find Area of a Rhombus
C Program to find Area of a Trapezium
C Program to find Area of a Circle and Semi-circle
C Program to find Circumference of a Circle and Semi-circle
C Program to find length of an arc
C Program to find Area of a Sector
C Program to find string length for list of strings
C Program to find the character at index in a string
C Program to compare characters
C Program to find eligible to vote or not
C Program to get system current date and time on linux
C Program to get positive number
C Program to implement calculator
C Program to implement banking system
C Program to find sum of number
C Program for array traversal using pointers
C Program for array traversal in reverse order using pointers
C Program to find particular element occurrences count in array
C Program to find even elements occurrences count in array
C Program to find odd elements occurrences count in array
C program to find week day in word from week day in number using two dimentsional array
C program to find month in word from month in number using pointers
C program to compute PMT for a loan
C program to compute EMI and round up using floor
C program to compute EMI and round up using ceil
C program to compute the EMI table with Interest, Principal and Balance
C program to get multiple line of string
C program to find nth element in array using pointers
C program to compute the volume of a cube
C program to compute the volume of a box
C program to compute the volume of a sphere
C program to compute the volume of a triangular prism
C program to compute the volume of a cylinder
C Program to Compute Perimeter of Square
C Program to compute the perimeter of triangle
C Program to compute the discount rate
C Program to compute the sales tax
C program to add, delete, search a record or show all using binary file
C program to add, delete, search a record or show all using text file

You might also like