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

Assignment1(c) (1)

The document contains six C programming examples. These examples demonstrate how to find the sum and average of four numbers, solve an equation, convert temperatures, print ASCII values, swap two numbers using different methods, and calculate the sum of digits of a five-digit number. Each example includes code snippets and user input prompts.

Uploaded by

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

Assignment1(c) (1)

The document contains six C programming examples. These examples demonstrate how to find the sum and average of four numbers, solve an equation, convert temperatures, print ASCII values, swap two numbers using different methods, and calculate the sum of digits of a five-digit number. Each example includes code snippets and user input prompts.

Uploaded by

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

1.Find out the sum and average of 4 given numbers.

#include <stdio.h>
int main()
{
int num1, num2, num3, num4, sum;
float average;
printf("Enter four numbers:\n ");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
sum = num1 + num2 + num3 + num4;
average = sum / 4;
printf("Sum of the numbers: %d\n", sum);
printf("Average of the numbers: %f\n", average);
return 0;
}
2.Solve the equation by user input: x=a + b - c*d/e.
#include <stdio.h>
int main()
{
float a, b, c, d, e, x;
printf("Enter value for a,b,c,d,and e:\n");
scanf("%f%f%f%f%f", &a,&b,&c,&d,&e);
if (e == 0)
{
printf("Error: Division by zero is not allowed.\n");
}
else
{
x = a + b - (c * d / e);
printf("The value of x is: %.2f\n", x);
}
return 0;
}
3.Input temperature in Celsius and convert it to Fahrenheit and vice
versa
#include <stdio.h>
int main()
{
float temperature;
int choice;
printf("1. Celsius to Fahrenheit\n");
printf("2. Fahrenheit to Celsius\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &choice);
if (choice == 1)
{
printf("Enter temperature in Celsius: ");
scanf("%f", &temperature);
printf("%f Celsius = %f Fahrenheit.\n", temperature,(temperature*9/5)+32);
}
else if (choice == 2)
{
printf("Enter temperature in Fahrenheit:"); scanf("%f", &temperature);
printf("%f Fahrenheit = %f Celsius \n", temperature,(temperature - 32)*5/9);
}
else
{
printf("Invalid choice !!.Please enter either 1 or 2.\n");
}
return 0;
}
4.Input a character and print it’s ASCII value.
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("The ASCII value of '%c' is %d.\n", ch, ch);
return 0;
}

5.Swapping of two number (Using Third variable, without using


third variable, without using+-/* operator.
#include <stdio.h>
int main()
{
int a, b, temp;
printf("Enter the value of a & b:\n");
scanf("%d %d", &a, &b); temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b); printf("Enter the value of a &
b:\n");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b); printf("Enter the value of a &
b: \n");
scanf("%d %d", &a, &b);
a = a ^ b;
b = a ^ b; a = a ^ b;
printf("After swapping: a = %d, b = %d\n", a, b); return 0;
}
6.Write a c program to calculate the sum of digits of a five digit numbers.
#include <stdio.h>
int main()
{
int number, sum;
printf("Enter a five-digit number: "); scanf("%d", &number);
sum = (number % 10) + ((number / 10) % 10) +
((number / 100) % 10) +
((number / 1000) % 10) +
((number / 10000) % 10);
printf("The sum of the digits is: %d\n",sum); return 0;
}

You might also like