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

Tyl Answers

The document contains multiple C programming exercises that cover various topics such as converting total days to years, months, and days; temperature conversion; calculating sums and products of numbers; generating multiplication tables; and more. Each exercise includes the C code, a brief description of the task, and example outputs. The exercises range from easy to medium difficulty, providing a comprehensive overview of basic programming concepts in C.

Uploaded by

joshua.ece24
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)
17 views17 pages

Tyl Answers

The document contains multiple C programming exercises that cover various topics such as converting total days to years, months, and days; temperature conversion; calculating sums and products of numbers; generating multiplication tables; and more. Each exercise includes the C code, a brief description of the task, and example outputs. The exercises range from easy to medium difficulty, providing a comprehensive overview of basic programming concepts in C.

Uploaded by

joshua.ece24
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
You are on page 1/ 17

Level: Easy

1.Write a C program to convert Total days to year, month, and days.​



C​
#include <stdio.h>

int main() {
int totalDays, years, months, days;

printf("Enter the total number of days: ");


scanf("%d", &totalDays);

years = totalDays / 365;


totalDays %= 365;
months = totalDays / 30; // Assuming an average of 30 days per month
days = totalDays % 30;

printf("Years: %d\n", years);


printf("Months: %d\n", months);
printf("Days: %d\n", days);

return 0;
}
Output:​

Enter the total number of days: 400
Years: 1
Months: 1
Days: 5

2.C program to convert temperature from Fahrenheit to Celsius and vice versa​

C​
#include <stdio.h>

int main() {
float fahrenheit, celsius;
char choice;

printf("Enter 'F' to convert Fahrenheit to Celsius or 'C' to convert Celsius to Fahrenheit: ");
scanf(" %c", &choice); // Note the space before %c to consume any leftover newline character
if (choice == 'F' || choice == 'f') {
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9.0;
printf("Temperature in Celsius: %.2f\n", celsius);
} else if (choice == 'C' || choice == 'c') {
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5.0) + 32;
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
} else {
printf("Invalid choice.\n");
}

return 0;
}
Output:​

Enter 'F' to convert Fahrenheit to Celsius or 'C' to convert Celsius to Fahrenheit: F
Enter temperature in Fahrenheit: 68
Temperature in Celsius: 20.00

3.Write a C Program to Accept ten numbers and display the sum of even and the product
of odd numbers​

C​
#include <stdio.h>

int main() {
int numbers[10];
int sumEven = 0, productOdd = 1;
int i, countOdd = 0;

printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
if (numbers[i] % 2 == 0) {
sumEven += numbers[i];
} else {
productOdd *= numbers[i];
countOdd++;
}
}

printf("Sum of even numbers: %d\n", sumEven);


if (countOdd > 0) {
printf("Product of odd numbers: %d\n", productOdd);
} else {
printf("No odd numbers entered.\n");
}

return 0;
}
Output:​

Enter 10 numbers:
1 2 3 4 5 6 7 8 9 10
Sum of even numbers: 30
Product of odd numbers: 945

4.Write a C Program to generate a Multiplication table of a given input number.​



C​
#include <stdio.h>

int main() {
int number, i;

printf("Enter a number: ");


scanf("%d", &number);

printf("Multiplication table of %d:\n", number);


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

return 0;
}
Output:​

Enter a number: 5
Multiplication table of 5:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

5. Write a C Program to calculate simple interest.

```c
#include <stdio.h>

int main() {
float principal, rate, time, simpleInterest;

printf("Enter principal amount: ");


scanf("%f", &principal);
printf("Enter rate of interest: ");
scanf("%f", &rate);
printf("Enter time (in years): ");
scanf("%f", &time);

simpleInterest = (principal * rate * time) / 100;

printf("Simple Interest: %.2f\n", simpleInterest);

return 0;
}
```

**Output:**

```
Enter principal amount: 1000
Enter rate of interest: 5
Enter time (in years): 2
Simple Interest: 100.00
```

6.Write a C Program to Swap 2 numbers with a third variable​



C​
#include <stdio.h>

int main() {
int a, b, temp;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

printf("Before swapping: a = %d, b = %d\n", a, b);

temp = a;
a = b;
b = temp;

printf("After swapping: a = %d, b = %d\n", a, b);

return 0;
}
Output:​

Enter two numbers: 10 20
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10

7.Write a C Program to find Quotient and Remainder​



C​
#include <stdio.h>

int main() {
int dividend, divisor, quotient, remainder;

printf("Enter dividend: ");


scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);

if (divisor == 0) {
printf("Error: Divisor cannot be zero.\n");
return 1; // Indicate an error
}
quotient = dividend / divisor;
remainder = dividend % divisor;

printf("Quotient: %d\n", quotient);


printf("Remainder: %d\n", remainder);

return 0;
}
Output:​

Enter dividend: 25
Enter divisor: 4
Quotient: 6
Remainder: 1

8.Write a C Program to calculate Area and Circumference of a circle based on user input​

C​
#include <stdio.h>
#include <math.h> // For M_PI (pi)

#define M_PI 3.14159 // You can also use this to define PI

int main() {
float radius, area, circumference;

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


scanf("%f", &radius);

area = M_PI * radius * radius;


circumference = 2 * M_PI * radius;

printf("Area: %.2f\n", area);


printf("Circumference: %.2f\n", circumference);

return 0;
}
Output:​

Enter the radius of the circle: 3.5
Area: 38.48
Circumference: 21.99
9.Write a C Program to find the largest number of the three.​

C​
#include <stdio.h>

int main() {
int num1, num2, num3, largest;

printf("Enter three numbers: ");


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

largest = num1; // Assume num1 is the largest initially

if (num2 > largest) {


largest = num2;
}

if (num3 > largest) {


largest = num3;
}

printf("The largest number is: %d\n", largest);

return 0;
}
Output:​

Enter three numbers: 15 8 22
The largest number is: 22

10.Write a C Program to calculate the grade of the student according to the specified
marks.​

C​
#include <stdio.h>

int main() {
int marks;

printf("Enter the student's marks: ");


scanf("%d", &marks);
if (marks >= 90) {
printf("Grade: A+\n");
} else if (marks >= 80) {
printf("Grade: A\n");
} else if (marks >= 70) {
printf("Grade: B\n");
} else if (marks >= 60) {
printf("Grade: C\n");
} else if (marks >= 50) {
printf("Grade: D\n");
} else {
printf("Grade: Fail\n");
}

return 0;
}
Output:​

Enter the student's marks: 75
Grade: B

Level: Medium

1.Write a C program to multiply two numbers using the plus operator.​



C​
#include <stdio.h>

int multiply(int a, int b) {


int result = 0;
for (int i = 0; i < b; i++) {
result += a;
}
return result;
}

int main() {
int num1, num2, product;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);
if (num2 < 0)
{
printf("Error: Second number must be positive.\n");
return 1;
}

product = multiply(num1, num2);

printf("Product: %d\n", product);

return 0;
}
Output:​

Enter two numbers: 6 7
Product: 42

2.Write a C Program to check whether the input number is a palindrome or not​



C​
#include <stdio.h>

int main() {
int number, reversedNumber = 0, remainder, originalNumber;

printf("Enter an integer: ");


scanf("%d", &number);

originalNumber = number;

while (number != 0) {
remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
}

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

return 0;
}
Output:​

Enter an integer: 12321
12321 is a palindrome.

3.Write a C Program to check whether the given number is an Armstrong or not​



C​
#include <stdio.h>
#include <math.h>

int main() {
int number, originalNumber, remainder, n = 0;
float result = 0.0;

printf("Enter an integer: ");


scanf("%d", &number);

originalNumber = number;

// Count the number of digits


originalNumber = number;
while (originalNumber != 0) {
originalNumber /= 10;
++n;
}

originalNumber = number;

while (originalNumber != 0) {
remainder = originalNumber % 10;
result += pow(remainder, n);
originalNumber /= 10;
}

if ((int)result == number) {
printf("%d is an Armstrong number.\n", number);
} else {
printf("%d is not an Armstrong number.\n", number);
}
return 0;
}
Output:​

Enter an integer: 153
153 is an Armstrong number.

4.Write a C Program to check whether the given number is a prime or not​



C​
#include <stdio.h>
#include <stdbool.h>

int main() {
int number, i;
bool isPrime = true;

printf("Enter a positive integer: ");


scanf("%d", &number);

if (number <= 1) {
isPrime = false;
} else {
for (i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}

return 0;
}
Output:​

Enter a positive integer: 29
29 is a prime number.
5.Write a C Program to generate Fibonacci series​

#include <stdio.h>

int main() {
int n, i, t1 = 0, t2 = 1, nextTerm;

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


scanf("%d", &n);

printf("Fibonacci Series: ");

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


printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf("\n");

return 0;
}
```

**Output:**

```
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
```

6.Write a C Program to print the factorial of a number​



C​
#include <stdio.h>

int main() {
int n, i;
unsigned long long factorial = 1;

printf("Enter an integer: ");


scanf("%d", &n);

if (n < 0) {
printf("Error: Factorial of a negative number doesn't exist.\n");
} else {
for (i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", n, factorial);
}

return 0;
}
Output:​

Enter an integer: 6
Factorial of 6 = 720

7.Write a C Program to reverse a given number​



C​
#include <stdio.h>

int main() {

int number, reversedNumber = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &number);

while (number != 0) {

remainder = number % 10;

reversedNumber = reversedNumber * 10 + remainder;

number /= 10;

printf("Reversed Number: %d\n", reversedNumber);

return 0;

}
Output:​

Enter an integer: 12345

Reversed Number: 54321

8.Write a C Program to generate the following pattern:​


#include <stdio.h>

int main() {

int rows = 3; // Number of rows in the pattern

int i, j;

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

printf("$");

for (j = 1; j <= i + 2; j++) {

printf("*");

printf("$\n");

return 0;

Output:​
***

****

*****
9.Write a C Program to generate the following pattern:​

1

12

123

1234

12345

C​
#include <stdio.h>

int main() {

int rows = 5; // Number of rows in the pattern

int i, j;

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

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

printf("%d", j);

printf("\n");

return 0;

}
Output:​

1

12

123

1234

12345

10.Write a C Program to calculate the sum of Natural Numbers.​



C​
#include <stdio.h>

int main() {

int n, i, sum = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

if (n < 1) {

printf("Error: Please enter a positive integer.\n");

return 1; // Indicate an error

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

sum += i;

}
printf("Sum of natural numbers up to %d is: %d\n", n, sum);

return 0;

Output:​

Enter a positive integer: 10

Sum of natural numbers up to 10 is: 55

You might also like