C PROGRAMMING REVISION WORKSHEET OK
C PROGRAMMING REVISION WORKSHEET OK
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;
return(0);
}
OUTPUT:
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;
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;
SI=P*R*N/100;
printf("\nSimple-Interest : %.2f\n",SI);
return 0;
}
Output:
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.
#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", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
In this program, the user is asked to enter two integers (dividend and divisor). They are stored
in variables dividend and divisor respectively.
#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:
Version 2
#include <stdio.h>
int main()
{
int n, count, sum = 0;
printf("Enter the value of n(positive integer): ");
scanf("%d",&n);
Introduction
#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;
}
#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.
#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
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;
Output:
$ cc area-of-sector.c
$ ./a.out
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:
#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
#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;
Output:
#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;
Output
$ cc arithmetic.c
$ ./a.out
Enter the number of days: 650
This c program is used to compute the sales tax based on user inputs cost and tax rate or
percentage.
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
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
This c program is used to compute the volume of a sphere based on the user inputs radius.
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);
Output:
$ cc volume-of-sphere.c
$ ./a.out
This c program is used to find the volume of a cube and length is provided as the user input.
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);
Output:
$ cc volume-of-cube.c
$ ./a.out