0% found this document useful (0 votes)
5 views17 pages

C Programs for Basic Operations and Calculations

The document contains multiple C programming examples authored by Chirag Mahara, showcasing various programming tasks including printing messages, swapping numbers, converting temperatures, calculating average marks, computing gross salary, and more. Each example includes the program code, expected output, and a confirmation of successful execution. The programs cover fundamental programming concepts and operations such as loops, conditionals, and arithmetic calculations.

Uploaded by

Jhandu Baam
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)
5 views17 pages

C Programs for Basic Operations and Calculations

The document contains multiple C programming examples authored by Chirag Mahara, showcasing various programming tasks including printing messages, swapping numbers, converting temperatures, calculating average marks, computing gross salary, and more. Each example includes the program code, expected output, and a confirmation of successful execution. The programs cover fundamental programming concepts and operations such as loops, conditionals, and arithmetic calculations.

Uploaded by

Jhandu Baam
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

Write a program to print hello 5 times.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

PROGRAM-

#include <stdio.h>
int main() {
int count = 1;
while (count <= 5) {
printf("Hello\n");
count++;
}
return 0;
}

OUTPUT-
Hello
Hello
Hello
Hello
Hello

=== Code Execution Successful ===


Write a program to swap two numbers.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

PROGRAM-
#include <stdio.h>

int main() {
int a,b;
printf("Enter the values of a and b=");
scanf ("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("Values after swapping a=%d b=%d",a,b);

return 0;
}

OUTPUT-
Enter the values of a and b=12. 19
Values after swapping a=0 b=12

=== Code Execution Successful ===


Write a C program to convert temperature form f degree to F.
( F=( C*18)/32 )

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

PROGRAM-

#include <stdio.h>
int main()
{
int c;
printf("Enter the number=");
scanf("%d",&c);
float f=(c*9/5)+32;
printf("The given value in f is =%.2f”,f);
printf("\n");
return 0;

OUTPUT-

Enter the number=34


The given value in f is =93.00

=== Code Execution Successful ===


Write a program to enter marks of a student in four subjects. Find the average marks of the students
accurately upto 2 decimal places.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

PROGRAM-

#include <stdio.h>
int main()
{
int a,b,c,d;
printf("Enter the marks in Maths=");
scanf("%d",&a);
printf("\nEnter the marks in Science=");
scanf("%d",&b);
printf("\nEnter the marks in English=");
scanf("%d",&c);
printf("\nEnter the marks in Computer Science=");
scanf("%d",&d);
float average=(a+b+c+d)/4;
printf("\nThe average marks of the 4 subjects=%.2f",average);

return 0;
}

OUTPUT-

Enter the marks in Maths=99

Enter the marks in Science=88

Enter the marks in English=89

Enter the marks in Computer Science=100

The average marks of the 4 subjects=94.00

=== Code Execution Successful ===


Write a c program to compute the gross salary of an employee by reading his basic pay form the
keyboard. (G Salary=BP+DA+TA+HRA
HRA=10% of BP
TA=5%of BP
DA=15%BP)

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

PROGRAM-
#include <stdio.h>
int main()
{
float bp, hra, ta, da, grosssalary;
printf("Enter the basic pay=");
scanf("%f", &bp);

// Calculations
hra = 0.10*bp;
ta = 0.05*bp;
da = 0.15*bp;
grosssalary = bp + hra + ta + da;
printf(“The gross salary is = %.2f\n", grosssalary);
return 0;
}

Output-
Enter the basic pay=200000
The gross salary is = 260000.00

=== Code Execution Successful ===


Write a c program to convert the given number of second in hours, minutes and seconds.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

PROGRAM-
#include <stdio.h>
int main()
{
int sec,hrs,mins,s,m;
printf("Enter the seconds =");
scanf("%d",&s);
hrs=s/(60*60);
m=s%3600;
mins=m/60;
sec=s%60;
printf("The time is %d hours %d mins %d seconds", hrs,mins,sec);
return 0;
}

OUTPUT-

Enter the seconds =56789


The time is 15 hours 46 mins 29 seconds

=== Code Execution Successful ===


Write a c program to read length in cm and then convert it into meters and kilometres. Display the
converted length into screen.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

PROGRAM-

#include <stdio.h>
int main()
{
int l,m,km,cm;
printf("Enter the length in cm");
scanf("%d",&l);
km=l/100000;
int a=l%100000;
m=a/100;
int b=l%100;
cm=b;
printf("The length is %d km %d m %dcm ",km,m,cm);
return 0;
}

OUTPUT-

Enter the length in cm123456


The length is 1 km 234 m 56cm

=== Code Execution Successful ===


Write a program to calculate the speed of a bike in m/s when the bike travels in d km of distance in
h hours.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

PROGRAM-

#include <stdio.h>
int main()
{
float d,t,s;
printf("Enter the distance traveled in km=");
scanf("%d",&d);
printf("Enter the time travels in hours=");
scanf("%d",&t);
d=d*1000;
t=t*3600;
s=d/t;
printf("The speed of bike in m/s is %.2f",s);
return 0;
}

OUTPUT-
Enter the distance traveled in km=10
Enter the time travels in hours=1
The speed of bike in m/s is 2.78

=== Code Execution Successful ===


Write a program to calculate the CP by reading the principle p, rate r, and time t, as input by a user.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

Program-
#include<stdio.h>
#include<math.h>
int main()
int p, r, n;
float x, y, cp ;
printf(“Enter the value of Principal: ”);
scanf(“%d”, &p);
printf(“Enter the value of rates:”);
scanf(“%d”, &r);
printf(“Enter the number of years:”);
scanf(“%d”, &n);
x=((1+r)/100);
power= pow(x,n);
cp=p*power;
printf(“The compound Interest is %.2f”,cp);
Return 0;
}

Output:
Enter the value of Principal: 1000,
Enter the value of rate: 5,
Enter the number of years: 2
The compound interest is 1102.50
Write a c program to display the sum of individual digits of a given number.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

Program-

#include <stdio.h>
int main()
{
int a ;
printf("Enter a three digit number=");
scanf ("%d",&a);
int n1=a%10;
int a1=a/10;
int n2=a1%10;
int a2=a1/10;
int sum=n1+n2+a2;
printf("%d",sum);
return 0;
}

Output:
Enter the number=432
9

=== Code Execution Successful ===


Write a program to display the products individual digits of a given three digits number.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

Program-

#include <stdio.h>
int main()
{
int a ;
printf("Enter a three digit number=");
scanf ("%d",&a);
int n1=a%10;
int a1=a/10;
int n2=a1%10;
int a2=a1/10;
int product=n1*n2*a2;
printf("%d",product);
return 0;
}

Output:
Enter a three digit number=222
8

=== Code Execution Successful ===


Write a program to check whether a number is even or odd.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

Program-

#include <stdio.h>
int main()
{
int a ;
printf("Enter the number=");
scanf ("%d",&a);
if (a%2==0){
printf("%d is a even number",a);
}
else {printf("%d is a odd number",a);}
return 0;
}

Output:
Enter the number=665
665 is a odd number

=== Code Execution Successful ===


Write a program whether a no is divisible by 5 and 11.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

Program-
#include <stdio.h>
int main()
{
int a ;
printf("Enter the number=");
scanf ("%d",&a);
if (a%5==0 && a%11==0){
printf("%d is divisible ",a);
}
else {printf("%d is not divisible",a);}
return 0;
}

Output:
Enter the number=55
55 is divisible

=== Code Execution Successful ===


Write an algorithm to read a year from user and check weather that year is leap year or not a leap
year.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

Program-
#include <stdio.h>
int main()
{
int a ;
printf("Enter a year=");
scanf ("%d",&a);
if (a%400==0){
printf("Leap year");
}
else if (a%100==0){
printf("Not a leap year");
}
else if(a%4==0){
printf("Leap year");
}
else {printf("Not a leap year");}
return 0;
}

Output:
Enter a year=2006
Not a leap year

=== Code Execution Successful ===


The age of three brothers Rohan, Ritik and Rishab are input through a keyboard.
Write a C program to determine the youngest of the three brothers. Also, check if the
youngest brother is eligible to get a driving license or not. Display the results.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

Program-
#include <stdio.h>
int main() {
int rohan, ritik, rishab;
printf("Enter the age of Rohan: ");
scanf("%d", &rohan);
printf("Enter the age of Ritik: ");
scanf("%d", &ritik);
printf("Enter the age of Rishab: ");
scanf("%d", &rishab);
int youngest;
if (rohan <= ritik && rohan <= rishab) {
youngest = rohan;
} else if (ritik <= rohan && ritik <= rishab) {
youngest = ritik;
} else {
youngest = rishab;
}

if (youngest >= 18) {


printf("The youngest brother is %d years old and is eligible for a driving license.\n",
youngest);
} else {
printf("The youngest brother is %d years old and is not eligible for a driving license.\n",
youngest);
}

return 0;
}

Output:
Enter the age of Rohan: 23
Enter the age of Ritik: 33
Enter the age of Rishab: 17
The youngest brother is 17 years old and is not eligible for a driving license.
The Techsoft company decided to insure its employees. The company decides upon a
policy that is based on the gross salary of the employees working in the company. The
insurance is deducted as per the following slabs:
If salary<=10000/month then insurance =5% of the salary.
If salary>10000/month and salary<=25000 then insurance =7% of salary.
If salary>25000/month and salary<=50000 then insurance =10% of salary
If salary>50000/month then insurance =12% of salary

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE
Program-
#include <stdio.h>
int main() {
float s, i;
printf("Enter the gross salary of the employee: ");
scanf("%f", &s);
if (s <= 10000) {
i = s* 0.05;
} else if (s > 10000 && s<= 25000) {
i = s* 0.07;
} else if (s > 25000 && s <= 50000) {
i = s * 0.10;
} else {
i = s* 0.12;
}

printf("The insurance deduction for a salary of %.2f is: %.2f\n", s, i);

return 0;
}

Output:

Enter the gross salary of the employee: 56666


The insurance deduction for a salary of 56666.00 is: 6799.92

=== Code Execution Successful ===


Write a C program to read three test scores of a student and find the average of the best
two scores. Assume a test score of MM 25 each.

Name - Chirag Mahara


Roll no.- 22
Section -K1
Course- Btech
Branch- CSE

Program-
#include <stdio.h>

int main() {
float s1, s2, s3;
float av;
printf("Enter the score of Test 1 :");
scanf("%f", &s1);
printf("Enter the score of Test 2 :");
scanf("%f", &s2);
printf("Enter the score of Test 3 :");
scanf("%f", &s3);
float min;

if (s1 <= s2 && s1 <= s3) {


min = s1;
} else if (s2 <= s1 && s2 <= s3) {
min = s2;
} else {
min = s3;
}
av = (s1 + s2 + s3 - min) ;

printf("The average of the best two scores is: %f\n", av);

return 0;
}

Output:
Enter the score of Test 1 :22
Enter the score of Test 2 :21
Enter the score of Test 3 :24
The average of the best two scores is: 46.000000

=== Code Execution Successful ===

You might also like