KEMBAR78
POGRAMMIN | PDF | Area | Division (Mathematics)
0% found this document useful (0 votes)
19 views22 pages

POGRAMMIN

The document contains a series of C programming exercises that cover various topics such as conversions (kilometers to meters, seconds to hours), arithmetic operations (division, simple interest calculation), geometric calculations (area of shapes, volume of a cube), and control structures (if-else statements, switch cases). Each exercise includes a brief description and the corresponding C code implementation. The programs are designed to help users understand basic programming concepts and mathematical calculations.

Uploaded by

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

POGRAMMIN

The document contains a series of C programming exercises that cover various topics such as conversions (kilometers to meters, seconds to hours), arithmetic operations (division, simple interest calculation), geometric calculations (area of shapes, volume of a cube), and control structures (if-else statements, switch cases). Each exercise includes a brief description and the corresponding C code implementation. The programs are designed to help users understand basic programming concepts and mathematical calculations.

Uploaded by

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

1) //Write a C program to convert kilometer to meter

#include <stdio.h>
void main()
{
float km,meter;
printf("Enter the distance in Kilometer=");
scanf("%f", &km);
meter=km*1000;
printf("%f kilometer is equal to %f meter \n",km,meter);
}

2) //Write a C program to divide an integer by another integer and find its


quotient and remainder

#include <stdio.h>
void main()
{
int dividend,divisor,remainder,quotient;
printf("Enter the Dividend= \n");
scanf("%d", &dividend);
printf("Enter the Divisor= \n");
scanf("%d", &divisor);
quotient=dividend/divisor;
remainder=dividend%divisor;
printf("Quotient=%d \n",quotient);
printf("Remainder=%d \n",remainder);
}

3) //Write a C program to convert entered the number of days into years, months
and remaining days

#include <stdio.h>
void main()
{
int days, years, months, rd;
printf("Enter the number of days= \n");
scanf("%d", &days);
years= days/365;
months=(days%365) / 30;
rd= (days % 365) % 30;
printf("%d days is equivalent to %d years, %d months and %d Remaining days \
n",days,years,months,rd);

4) //Write a C program to convert entered Seconds to hours, minutes and seconds

#include <stdio.h>
void main()
{
int hours,seconds,minutes,rs;
printf("Enter Seconds= \n");
scanf("%d", &seconds);
hours= seconds/3600;
minutes=(seconds%3600) / 60;
rs= (seconds % 3600) % 60;
printf("%d Seconds is equivalent to %d hours, %d minutes and %d seconds \
n",seconds,hours,minutes,rs);

5) //Write a C program that reads the principle,rate of interest and number of


years and find the simple interest and amount

#include <stdio.h>
void main()
{
float principle,rate,years,si,amount;
printf("Welcome to Simple Interest calculater\n");

printf("Enter the Principle amount=\n");


scanf("%f", &principle);

printf("Enter the Rate of interest (in percentage)=\n");


scanf("%f", &rate);

printf("Enter the number of Years=\n");


scanf("%f", &years);

si = (principle * rate * years) / 100;


amount = principle + si;

printf("Simple Interest=%f\n",si);
printf("Amount=%f \n",amount);

6) //If a cube has its side, its volume and surface area given by the formulae
v=a^3 [v=a*a*a] and s=6a^2 [s=6*a*a*]. write a c program to read side (a) and print
the volume (v) and surface area (s)

#include <stdio.h>
void main()
{
float side, volume, surfaceArea;

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


scanf("%f", &side);

volume = side * side * side;


surfaceArea = 6 * side * side;

printf("\nVolume of the cube: %.2f\n", volume);


printf("Surface Area of the cube: %.2f\n", surfaceArea);

7) //The area of a triangle is given by the formula; area =1/2* base * height.
write a c program to read base and height and print the area.

#include <stdio.h>
void main()
{
float base,height,area;
printf("Enter the base of the Triangle: \n ");
scanf("%f", &base);

printf("Enter the height of the Triangle: \n");


scanf("%f", &height);

area = 0.5*base*height;

printf("The area of triangle is %f \n",area);

8) //write a c program to read the radius of a circle and display its area

#include <stdio.h>
void main()
{
float radius,area;

printf("Enter the radius of circle: \n ");


scanf("%f", &radius);

area = 3.14*radius*radius;

printf("The area of circle is %f \n",area);

9) //write a c program to convert a temperature reading in degree celsius

#include <stdio.h>
void main()
{
float fahrenheit,celsius;

printf("Enter the degree in Fahrenheit: \n ");


scanf("%f", &fahrenheit);

celsius = 0.55555555555*(fahrenheit-32);

printf("%f Fahrenheit is equal to %f Celsius \n", fahrenheit, celsius );

10) //write a c program to enter any 4 digit number and find sum of them

#include <stdio.h>
void main()
{
int number, digit, sum = 0;

printf("Enter a four digit number: \n ");


scanf("%d", &number);

if (number < 1000 || number > 9999)


{
printf("Invalid input. Please enter a four-digit number.\n");
}

while (number > 0)


{
digit = number % 10;
sum += digit;
number /= 10;
}

printf("\nSum of the digits: %d\n", sum);

12) //write a c program to read the value of any four digit number and display it
in reverse order

#include <stdio.h>
void main()
{
int number, reversednumber = 0;

printf("Enter a four digit number: \n ");


scanf("%d", &number);

if (number < 1000 || number > 9999)


{
printf("Invalid input. Please enter a four-digit number.\n");
}

while (number > 0)


{
int digit=number%10;
reversednumber=reversednumber*10+digit;
number /= 10;
}

printf("\nNumber in reverse order: %d\n", reversednumber);

13) //write a c program to input an arbitrary number and find out whether it is
positive or negative

#include <stdio.h>
void main()
{
float number;

printf("Enter a number: ");


scanf("%f", &number);
if (number > 0) {
printf("\nThe entered number is positive.\n");
} else if (number < 0) {
printf("\nThe entered number is negative.\n");
} else {
printf("\nThe entered number is zero.\n");
}

14) //write a c program to find out whether it is an odd number or even number.

#include <stdio.h>
void main()
{
int number,odd,even;

printf("Enter a number: ");


scanf("%d", &number);

if (number%2==0)
{
printf("%d is an Even number \n",number);
} else {
printf("%d is an Odd number \n",number);
}

15) //write a c program that reads three numbers and displays the largest among
them

#include <stdio.h>
void main()
{
int num1, num2, num3;

printf("Enter three numbers: ");


scanf("%d %d %d", &num1, &num2, &num3);

if (num1 >= num2 && num1 >= num3) {


printf("\nThe largest number is: %.2d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("\nThe largest number is: %.2d\n", num2);
} else {
printf("\nThe largest number is: %.2d\n", num3);
}

16) //write a c program that checks whether the number entered by the user is
exactly divisible by 5 but not by 11.

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

printf("Enter a number: ");


scanf("%d", &number);

if (number % 5 == 0 && number % 11 != 0) {


printf("\nThe entered number is exactly divisible by 5 but not by 11.\n");
} else {
printf("\nThe entered number does not meet the specified criteria.\n");
}

17) //write a c program to read any years and check whether the entered yeat is a
leap or not

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

printf("Enter a year: ");


scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


{
printf("\nThe entered year %d is a leap year.\n", year);
} else
{
printf("\nThe entered year %d is not a leap year.\n", year);
}

18) //write a c program to find the commission amount on the basis of sales amount
as per the following condition: sales amount (0-1000) then commission is 5%, sales
amount (1001-2000) then commission is 10%, sales amount (> 2000) then commission is
12%

#include <stdio.h>
void main()
{
float salesAmount, commission;

printf("Enter the sales amount: ");


scanf("%f", &salesAmount);

if (salesAmount >= 0 && salesAmount <= 1000)

{
commission = 0.05 * salesAmount;
} else if (salesAmount > 1000 && salesAmount <= 2000)

{
commission = 0.10 * salesAmount;
} else if (salesAmount > 2000)
{
commission = 0.12 * salesAmount;
} else

{
printf("Invalid input. Please enter a non-negative sales amount.\n");
}

printf("\nCommission amount: %.2f\n", commission);

19) //a man is paid at the hourly rate of rs.250 per hour for the first 30 hours
worked in a week. then after the overtime is paid at 1.5 times the hourly rate for
the next time 25 hours and 2 times the hourly rate for further hours worked. write
a c program to input the number of hours worked in a week and print weekly wages

#include <stdio.h>
void main()
{
int hoursWorked;
float hourlyRate = 250.0; // Hourly rate in Rs.
float weeklyWages;

// Get user input


printf("Enter the number of hours worked in a week: ");
scanf("%d", &hoursWorked);

// Calculate weekly wages based on the given conditions


if (hoursWorked <= 30) {
// Regular hours
weeklyWages = hoursWorked * hourlyRate;
} else if (hoursWorked <= 55) {
// Overtime at 1.5 times the hourly rate
weeklyWages = 30 * hourlyRate + (hoursWorked - 30) * 1.5 * hourlyRate;
} else {
// Overtime at 2 times the hourly rate for additional hours
weeklyWages = 30 * hourlyRate + 25 * 1.5 * hourlyRate + (hoursWorked - 55)
* 2 * hourlyRate;
}

printf("\nWeekly Wages: Rs. %.2f\n", weeklyWages);

20) // write a c program to display the name of the day on the basis of entered
number 1 to 7.

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

printf("THE NAME OF THE DAY ON THE BASIS OF ENTERED NUMBER \n");


printf("PLEASE ENTER A NUMBER BETWEEN (1-7) \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Sunday \n");
break;

case 2:
printf("Monday \n");
break;

case 3:
printf("Tuesday \n");
break;

case 4:
printf("Wednesday \n");
break;

case 5:
printf("Thursday \n");
break;

case 6:
printf("Friday \n");
break;

case 7:
printf("Saturday \n");
break;

default:
printf("Please enter the number between (1-7) \n");
}

21) // write a program to calculate the area of a circle, a rectangle or a triangle


depending upon the user choice

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

printf("Choose a shape to calculate area:\n");


printf("1. Circle\n");
printf("2. Rectangle\n");
printf("3. Triangle\n");
printf("Enter your choice (1/2/3): ");
scanf("%d", &choice);

switch (choice) {
case 1:
// Area of a circle
{
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
printf("\nThe area of the circle is: %.2f\n", area);
}
break;

case 2:
// Area of a rectangle
{
float length, width;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
area = length * width;
printf("\nThe area of the rectangle is: %.2f\n", area);
}
break;

case 3:
// Area of a triangle
{
float base, height;
printf("Enter the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);
area = 0.5 * base * height;
printf("\nThe area of the triangle is: %.2f\n", area);
}
break;

default:
printf("\nInvalid choice. Please choose 1, 2, or 3.\n");

}
}

22) // write a program to enter the day, month and year of the date of birth of a
person and th day, month and year of today and find the age of the person in years,
months and days

#include <stdio.h>
void main()
{
int birthDay, birthMonth, birthYear;
int currentDay, currentMonth, currentYear;
int ageYears, ageMonths, ageDays;

printf("Enter your date of birth (DD MM YYYY): ");


scanf("%d %d %d", &birthDay, &birthMonth, &birthYear);

printf("Enter the current date (DD MM YYYY): ");


scanf("%d %d %d", &currentDay, &currentMonth, &currentYear);
ageYears = currentYear - birthYear;
ageMonths = currentMonth - birthMonth;
ageDays = currentDay - birthDay;

if (ageMonths < 0 || (ageMonths == 0 && ageDays < 0)) {


ageYears--;
ageMonths += 12;
}

printf("\nAge: %d years, %d months, %d days\n", ageYears, ageMonths, ageDays);

23) // write a program to add, subtract, multiply or divide between two numbers on
the basis of entered character a,s,m or d respectively

#include <stdio.h>
void main()
{
float num1, num2, result;
int choose;

printf("Enter the first number: ");


scanf("%f", &num1);

printf("Enter the second number: ");


scanf("%f", &num2);

printf("Choose an operation: \n ");

printf("1) Addition: \n ");


printf("2) Subtraction: \n ");
printf("3) Multiplication: \n ");
printf("4) Division: \n ");
scanf(" %d", &choose);

switch (choose) {
case 1:

result = num1 + num2;


printf("\n %2f + %2f = %2f \n", num1, num2, result);
break;

case 2:

result = num1 - num2;


printf("\n%2f - %2f = %2f\n", num1, num2, result);
break;

case 3:

result = num1 * num2;


printf("\n%2f * %2f = %2f\n", num1, num2, result);
break;

case 4:
if (num2 != 0) {
result = num1 / num2;
printf("\n%2f / %2f = %2f\n", num1, num2, result);
} else {
printf("\nError: Division by zero is not allowed.\n");

}
break;

default:
printf("\nInvalid operation. Please choose between (1-4) \n");

24) // write a program that finds the sum, difference, and product of 2 numbers
using a switch case statement

#include <stdio.h>
void main()
{
float num1, num2;
int choose;

printf("Enter the first number: ");


scanf("%f", &num1);

printf("Enter the second number: ");


scanf("%f", &num2);

printf("Choose an operation: \n ");

printf("1) Addition: \n ");


printf("2) Subtraction: \n ");
printf("3) Multiplication: \n ");
scanf(" %d", &choose);

switch (choose) {
case 1:
// Addition
printf("\n%2f + %2f = %2f\n", num1, num2, num1 + num2);
break;

case 2:
// Subtraction
printf("\n%2f - %2f = %2f\n", num1, num2, num1 - num2);
break;

case 3:
// Multiplication
printf("\n%2f * %2f = %2f\n", num1, num2, num1 * num2);
break;

default:
printf("\nInvalid operation. Please choose between (1-3).\n");

}
}

25) // write a c program to display the name of the day in a week, depending on the
number entered through the keyboard using the switch-case statement

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

printf("THE NAME OF THE DAY ON THE BASIS OF ENTERED NUMBER \n");


printf("PLEASE ENTER A NUMBER BETWEEN (1-7) \n");
scanf("%d", &choice);

switch (choice)
{
case 1:
printf("Sunday \n");
break;

case 2:
printf("Monday \n");
break;

case 3:
printf("Tuesday \n");
break;

case 4:
printf("Wednesday \n");
break;

case 5:
printf("Thursday \n");
break;

case 6:
printf("Friday \n");
break;

case 7:
printf("Saturday \n");
break;

default:
printf("Please enter the number between (1-7) \n");
}

26) // write a c program to find the sum of first 50 natural numbers, that is
1+2+3+...+50

#include <stdio.h>
void main()
{
int n = 50;
int sum = 0,i;

for(i = 1; i <= n; i++)


{
sum += i;
}

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

27) // write a c program to multiply following operation: 20 X 22 X 24 X 26 X 28

#include <stdio.h>
void main()
{
int numbers[] = {20, 22, 24, 26, 28};
int product = 1,i;

for (i = 0; i < sizeof(numbers) / sizeof(numbers[0]); i++)


{
product *= numbers[i];
}

printf("The product of the given numbers is: %d\n", product);

28) // write a c program to display the sum of 'n' terms of 'even' numbers

#include <stdio.h>
void main()
{
int n,i, sum = 0;

printf("Enter the number of terms (n): ");


scanf("%d", &n);

for(i=2; n>0; i+=2, n--)


{
sum+=i;
}

printf("The sum of %d terms of even numbers is: %d \n",n, sum);

29) // write a c program to display the sum of 'odd' numbers from 1 to 10

#include <stdio.h>
void main()
{
int i,sum = 0;
for (i = 1; i <= 10; i += 2)
{
sum += i;
}

printf("The sum of odd numbers from 1 to 10 is: %d\n", sum);


}

30) // write a c program to print the first 10 terms of any series using for loop

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

printf("The first 10 terms of the series are:\n");

for (i = 1; i <= 10; i++) {


int term = i * i;
printf("%d ", term);
}

printf("\n");

31) // write a c program to print 10 terms of the following series using for loop,
1,5,9,13,..

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

printf("The first 10 terms of the series are:\n");

int term = 1;

for (i = 1; i <= 10; i++) {


printf("%d ", term);
term += 4;
}

printf("\n");

32) // write a c program to find the sum of the cubes of the first 10 numbers. i.e.
1^3+2^3+3^3+4^3+..+10^3

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

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

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

33) // write a c program to evaluate the sum of n terms of the following series:
1/1 + 1/3 + 1/5 + 1/7 + ...

#include <stdio.h>
void main()
{
int n, i;
float sum = 0.0;

printf("Enter the number of terms: ");


scanf("%d", &n);

for (i = 0; i < n; i++)


{
sum += 1.0 / (2 * i + 1);
}

printf("Sum of the series for %d terms: %f\n", n, sum);

34) // write a c program to find the sum of the series: 1/12 + 1/22 + 1/32 + ....
1/n

#include <stdio.h>
void main()
{
int n, i;
float sum = 0.0;

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


scanf("%d", &n);

for (i = 1; i <= n; i++)


{
sum += 1.0 / (10 * i + 2);
}

printf("Sum of the series for %d terms: %f\n", n, sum);

35) // write a c program to display the multiplication table of 1 to 10

#include <stdio.h>
void main()
{
int i, j;

for (i = 1; i <= 10; i++)


{
printf("Multiplication table for %d:\n", i);
for (j = 1; j <= 10; j++)
{
printf("%d x %d = %d\n", i, j, i * j);
}
printf("\n");
}

36) // write a c program to read a four digit number and display it in reverse
order using loop

#include <stdio.h>
void main()
{
int num, reversed = 0, remainder;

printf("Enter a four-digit number: ");


scanf("%d", &num);

if (num < 1000 || num > 9999)


{
printf("Please enter a valid four-digit number.\n");
}

while (num > 0)


{
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

printf("Reversed number: %d\n", reversed);

37) // write a c program to read a positive integer less than 1000 and display its
multiplication table

#include <stdio.h>
void main()
{
int num, i;

printf("Enter a positive integer less than 20: ");


scanf("%d", &num);

if (num <= 0 || num >= 1000)


{
printf("Please enter a valid positive integer less than 20.\n");
}

printf("Multiplication table for %d:\n", num);


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

38)// write a c program to display the following patterns using a nested loop

#include <stdio.h>
void main()
{
int rows, i, j;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (i = 1; i <= rows; i++) {


for (j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
}

38)// write a c program to display the following patterns using a nested loop

#include <stdio.h>
void main()
{
int rows, i, j;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (i = 1; i <= rows; i++) {


for (j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}
}

38)// write a c program to display the following patterns using a nested loop

#include <stdio.h>
void main()
{
int rows, i, j;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (i = rows; i >= 1; i--) {


for (j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}
}

38)// write a c program to display the following patterns using a nested loop

#include <stdio.h>
void main()
{
int rows, i, j;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (i = rows; i >= 1; i--) {


for (j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
}

38)// write a c program to display the following patterns using a nested loop

#include <stdio.h>
void main()
{
int rows, i, j;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (i = 1; i <= rows; i++) {


for (j = 1; j <= rows; j++) {
printf("%-3d", i * j);
}
printf("\n");
}
}

38)// write a c program to display the following patterns using a nested loop

#include <stdio.h>
void main()
{
int rows, i, j;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (i = 1; i <= rows; i++) {


for (j = 1; j <= rows - i + 1; j++) {
printf("%c ", 'K');
}
for (j = 1; j <= i; j++) {
printf("%c ", 'A');
}
printf("\n");
}
}

38)// write a c program to display the following patterns using a nested loop

#include <stdio.h>
void main()
{
int rows, i, j, number = 1;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (i = 1; i <= rows; i++) {


for (j = 1; j <= i; j++) {
printf("%d ", number);
number += 2;
}
printf("\n");
}
}

38)// write a c program to display the following patterns using a nested loop

#include <stdio.h>
void main()
{
int rows, i, j, k;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (i = 1; i <= rows; i++) {

for (j = 1; j <= rows - i; j++) {


printf(" ");
}

for (k = 1; k <= 2 * i - 1; k++) {


printf("1");
}

printf("\n");
}
}

39)// write a c program to check whether the given number is a palindrome or not

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

originalNum = num;

while (num > 0) {


remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}

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

40) //write a c program to find the factorial of a given positive number

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

int n,i,factorial;

printf("Enter a positive integer: ");


scanf("%d", &n);

if (n <= 0)
{
printf("Error: Please enter a non-negative integer.\n");
} else
{
for (i=1;i<=n; i++)
{
factorial*=i;
}
printf("Factorial of %d = %d\n", n, factorial);
}
}

41) //write a c program to find the factorial of a given number

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

int n,i,factorial;

printf("Enter a positive integer: ");


scanf("%d", &n);

for (i=1;i<=n; i++)


{
factorial*=i;
}
printf("Factorial of %d = %d\n", n, factorial);
}

42)//write a c program to print 10 positive integers and their factorials

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

int n,i,factorial,result;

for (i=1;i<=n; i++)


{
result *= i;
}
printf("%-15s%-20s\n", "Number", "Factorial");
for ( i = 1; i <= 10; ++i)
{
result = factorial*=i;
printf("%-15d%-20llu\n", i, result);
}
}

43)//write a c program to check prime no or composite no

#include <stdio.h>
void main() {
int rem,n,i,count=0 ;

printf("Enter a number to find prime no. or composite no. \n ");


scanf("%d",& n);
for (i=1;i<=n;i++)
{
rem=n%i;
if(rem == 0)
{
count++;
}
}
if (count==1)
{
printf("Neither prime nor composite");
}
else if (count ==3)
{
printf("Prime no");
}
else
{
printf("Composite no.");
}

You might also like