0% found this document useful (0 votes)
22 views10 pages

C Programs for Basic Calculations

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

C Programs for Basic Calculations

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

#include <stdio.

h>

int main() {
int n;

// Input the number of natural numbers to process


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

// Print the square and cube of the first n natural numbers


for (int i = 1; i <= n; i++) {
printf("Number: %d, Square: %d, Cube: %d\n", i, i * i, i * i * i);
}

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

printf("Enter n:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
for (j=1;j<=i;j++)
{
printf("%d ",j);
}

printf("\n");
}

}
#include <stdio.h>

int main() {
int num, i;

printf("Enter a number: ");


scanf("%d", &num);

if (num <= 1) {
printf("%d is not a prime number.\n", num);
}

for (i = 2; i < num; i++) {


if (num % i == 0) {
printf("%d is not a prime number.\n", num);
}
}

printf("%d is a prime number.\n", num);

return 0;
}
#include<stdio.h>
int main()
{
int age;

printf("Enter age: ");


scanf("%d", &age);

if (age >=18)
{
printf("Elligible to vote.");
}
else
{
printf("Not elligible to vote.");
}
return 0;
}
#include <stdio.h>

int main() {

const float HRA_PERCENTAGE = 0.10;


const float TA_PERCENTAGE = 0.05;

float basic_pay, hra, ta, total_salary;

printf("Enter the basic pay of the employee: ");


scanf("%f", &basic_pay);

hra = HRA_PERCENTAGE * basic_pay;


ta = TA_PERCENTAGE * basic_pay;

total_salary = basic_pay + hra + ta;

printf("The total salary of the employee is: %.2f\n", total_salary);

return 0;
}
#include <stdio.h>

int main() {
int n, i;
float price, quantity, total = 0.0;

printf("Enter the number of grocery items: ");


scanf("%d", &n);

char item_name[50];

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


printf("\nEnter the name of item %d: ", i);
scanf("%s", item_name);
printf("Enter price of %s: ", item_name);
scanf("%f", &price);
printf("Enter quantity of %s: ", item_name);
scanf("%f", &quantity);

total += price * quantity;


}

printf("\nTotal Grocery Bill To Be Paid: %.2f\n", total);

return 0;
}
PROGRAM:
WITH TEMPORARY VARIABLE:
#include&lt;stdio.h&gt;
void main()
{
int a=10,b=20,temp;
temp=a;
a=b;
b=temp;
printf(&quot;After swapping:%d %d&quot;,a,b);
}
WITHOUT TEMPORARY VARIABLE:
#include&lt;stdio.h&gt;
void main()
{
int a=10,b=20;
a=a+b;
b=a-b;
a=a-b;
printf(&quot;After swapping:%d %d&quot;,a,b);
}
OUTPUT:
After swapping: 20 10

PROGRAM:
#include&lt;stdio.h&gt;
void main()
{
float r,area;
printf(“Enter the radius:”)
scanf(“%f”,&amp;r);
area=3.14*r*r;
printf(&quot;Area:%f&quot;,area);
}

OUTPUT:
Enter the radius:3
Area:28.26
PROGRAM:
#include&lt;stdio.h&gt;
void main()
{
char c;
printf(&quot;Enter any char:&quot;);
scanf(&quot;%c&quot;,&amp;c);
printf(&quot;\nASCII value of %c is %d&quot;,c,c);
}

OUTPUT:
Enter any char:A
ASCII value of A is 95

PROGRAM:
#include&lt;stdio.h&gt;
void main()
{
char a,b;
printf(&quot;Enter the character in uppercase:&quot;);
scanf(&quot;%c&quot;,&amp;a);
b=a+32;
printf(&quot;The character in lowercase is:%c&quot;,b);
}

OUTPUT:
Enter the character in uppercase:M
The character in lowercase is:m
:
#include&lt;stdio.h&gt;
void main()
{
float f,c;
printf(&quot;Enter the farenheit:&quot;);
scanf(&quot;%f&quot;,&amp;f);
c=0.56*(f-32);
printf(&quot;The value in celcius is:%f&quot;,c);
}
OUTPUT:
Enter the farenheit:98.6
The value in celcius is:37.295998
PROGRAM:
#include&lt;stdio.h&gt;
#include&lt;math.h&gt;
void main() {
int x1, y1, x2, y2,x,y,distance;
printf(&quot;Enter the coordinates of point 1 (x1 y1): &quot;);
scanf(&quot;%d %d&quot;, &amp;x1, &amp;y1);
printf(&quot;Enter the coordinates of point 2 (x2 y2): &quot;);
scanf(&quot;%d %d&quot;, &amp;x2, &amp;y2);
x = x2 - x1;
y = y2 - y1;
distance = sqrt(x * x + y * y);
printf(&quot;Distance is: %d\n&quot;, distance);
}
OUTPUT:

You might also like