A collection of C programs from my university exercises.
Now, I find it fun, but back then, it often brought out some "F" words. Those were the days!
Write a program that takes two variables as input and swaps their values without using a third variable.
Write a program that converts temperature from Celsius to Fahrenheit. The formula is:
Write a program that calculates an individual's monthly salary after deducting tax. The program should take the number of monthly working hours and the hourly wage as input. If the total salary is less than 140,000, no tax is applied. For any amount above 140,000, 10% tax is deducted.
Write a program that calculates and displays the value of y
based on the following conditions:
y = if (x>20) -4x2+3 else 3x-7
Write a program that takes the number n
as input, representing the nth day of the year, and determines which day of the week and which week of the year it is.
Write a program that reads three numbers from the input and displays the smallest one in the output.
Write a program that reads three numbers from the input, sorts them in ascending order, and displays them from smallest to largest.
Write a program that takes two numbers x
and y
as input and calculates the sum of their absolute values |x| + |y|
.
Write a program that takes a natural number n
as input and displays the nth uppercase letter of the alphabet. For example, if the input is 2, the output should be B
.
Write a program that reads an integer from the input and displays its equivalent in both octal and hexadecimal formats.
Write a program that reads three numbers from the input and calculates the maximum value among them, then displays the result.
Write a program that takes the number of a Persian month as input and displays the number of days in that month.
Write a program that takes the coefficients a
, b
, and c
of the quadratic equation ax^2 + bx + c = 0
as input and calculates and displays the real roots, if they exist. First, calculate the discriminant D = b^2 - 4ac
. Based on the value of D
, determine the number and nature of the roots:
- If
D < 0
, there are no real roots. - If
D = 0
, there are two equal real roots:x = -b / (2a)
. - If
D > 0
, there are two distinct real roots:x1 = (-b - √D) / (2a)
andx2 = (-b + √D) / (2a)
.
Write a program that takes three numbers, a
, b
, and c
, as input and sorts them in ascending order.
15. Function of x
Write a program that takes the value of x
as input and calculates the value of y
according to the following rules:
y = 1, if x > 0
y = 0, if x = 0
y = -1, if x < 0
Write a program that takes the coordinates of the center and radius of a circle, along with the coordinates of another point, and determines whether the point is inside, outside, or on the circle.
Write a program that displays all even three-digit numbers. Since the first even three-digit number is 100, set the initial counter to 100 and continue the loop until the counter is less than 1000. In each iteration, display the counter and then increment it by 2 to get the next even number.
Write a program that takes 10 grades of a student as input and calculates the average of those grades.
Write a program that takes an integer n
as input and calculates the product of all numbers from 1 to n
(i.e., the factorial of n
). Since the factorial can be a very large number, define the variable f
as long int
.
Write a program that displays the 5x5 multiplication table.
Write a program that takes an integer n
as input and displays a triangle of stars. For example, if n
is 5, the output should be:
*
**
***
****
*****
Write a program that takes an integer n
as input and displays a pattern where stars and plus signs alternate. For n = 4
, the output should be:
*
++
***
++++
Write a program that takes an integer n
as input and displays a checkerboard pattern alternating between stars and plus signs. For n = 4
, the output should look like:
*
+*
*+*
+*+*
Write a program that takes an integer n
as input and displays an inverted triangle of stars. For n = 4
, the output should look like:
****
***
**
*
Write a program that takes an integer n
as input and displays a right-aligned triangle of stars. For n = 4
, the output should be:
*
**
***
****
Write a program that takes an integer n
as input and displays a triangle pattern where stars decrease from left to right, and plus signs increase. For n = 4
, the output should be:
++++
*+++
**++
***+
****
Write a program that takes an integer n
as input and displays a right-aligned triangle of stars, but the number of stars increases by 2 each row. For n = 4
, the output should be:
*
***
*****
*******
Write a program that takes an integer n
as input and displays a pattern where each row consists of the row number repeated. For n = 4
, the output should be:
1
22
333
4444
Write a program that takes an integer n
as input and displays a pattern where the largest number starts at the top and decreases while the count of repeated digits increases. For n = 4
, the output should be:
4
33
222
1111
Write a program that takes an integer n
as input and displays a pattern where the numbers start at n
and decrease in each line, with the number of digits increasing per row. For n = 4
, the output would look like:
4
43
432
4321
Write a program that takes an integer n
as input and displays a pattern where the numbers start at n
and decrease in each line, with the number of digits decreasing per row. For n = 4
, the output would look like:
4321
432
43
4
Write a program that takes an integer n
as input and displays a pattern where the numbers start at 1
and increase in each row, with spaces to align the numbers to the right. For n = 4
, the output would look like:
1
21
321
4321
Write a program that takes an integer n
as input and displays a triangle where the numbers start at 1
, increase to the current row number, and then decrease back down. For n = 4
, the output would look like:
1
121
12321
1234321
Write a program that takes an integer n
as input and displays a triangle where the number of stars increases by 2 in each row. For n = 4
, the output would look like:
1
123
12345
1234567
Write a program that takes an integer n
as input and displays a diamond shape of stars. The number of stars should increase up to the middle and then decrease symmetrically. For n = 4
, the output would look like:
*
***
*****
***
*
Write a program that takes an integer n
as input and displays all divisors of n
. A divisor is a number that divides n
without leaving a remainder. The program should print each divisor.
Write a program that takes a single digit as input and displays the corresponding word for that digit. The program should use a switch-case structure to display the word equivalent of the number (for example, 1
should be displayed as One
).
Write a program that takes the first letter of a color (a
, r
, or g
) as input and displays the full name of the color. The input may be in either uppercase or lowercase.
Write a program that takes a single English letter as input and determines if it is a vowel (A, E, I, O, U) or not
Write a program that takes the side length of a square (n
) as input and displays a hollow square made of stars. The program should print a square with stars on the borders and spaces inside. The side length n
must be between 1 and 20. For example, if n = 5
, the output should be:
*****
* *
* *
* *
*****
Write a program that takes an integer n
as input and determines if the number is prime. A prime number is a number greater than 1 that has no divisors other than 1 and itself. For example,
Write a program that prints the first 15 terms of the Fibonacci sequence. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. The first two terms are 1
and 1
. The next terms are obtained by adding the two previous terms. The sequence starts like this: 1, 1, 2, 3, 5, 8, 13, 21, ...
Write a program that takes an integer n
as input and determines whether n
is part of the Fibonacci sequence or not.
Write a program that finds and displays the largest four-digit number in the Fibonacci sequence.
The square of any natural number n
can be calculated by summing n
consecutive odd numbers starting from 1. For example:
- n = 1 > 1² = 1
- n = 2 > 2² = 1 + 3
- n = 3 > 3² = 1 + 3 + 5
Write a program that calculates the square of a given natural number using this method, without using the multiplication operation.
Write a program that approximates the mathematical constant e
using the following formula, accurate to four decimal places:
[ e = 1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + \dots ]
Assume that each day of the week is represented by a code such that:
- Saturday has a code of 0,
- Sunday has a code of 1,
- and so on, with Friday having a code of 6.
Write a program that takes the code for the first day of a 31-day month as input and displays the calendar for that month. For example, if the input is 2, it means the first day of the month is a Monday. The output calendar should be displayed as follows:
Sat Sun Mon Tue Wed Thu Fri
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Write a program that takes an input number n
and prints n
rows of output with appropriate spacing, similar to the following example:
i 10 * i 100 * i 1000 * i
1 10 100 1000
2 20 200 2000
3 30 300 3000
Write a program that takes two natural numbers as input and finds their Least Common Multiple (LCM). The Least Common Multiple of two numbers is the smallest number that is divisible by both without a remainder. For example, the LCM of 23 and 16 is 368.
Write a program that takes an integer as input and displays all the prime numbers less than or equal to that number.
Write a program that reads a binary number (an integer consisting only of 0s and 1s) and prints its decimal equivalent. For example, for the input 10110
, the output should be 22
.
Write a program that computes the value of ( e^x ) using the following formula up to 10 terms:
[ e^x = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \dots ]
Write a program that generates a table of trigonometric ratios for angles from 1 to 89 degrees. The table should display the angle (in degrees), sine, cosine, tangent, and cotangent values to F
decimal places, with each value in its own column. Use a step of 1 degree.
Write a program that generates a table of negative powers of 2 (i.e., 2^-1, 2^-2, etc.). The table should display the values in decimal form, fractional form, and exponential form. The number of rows in the table should be specified by the input. An example output with three rows would look like this:
Decimal Fraction Power of 2
0.5000 1/2 2^-1
0.2500 1/4 2^-2
0.1250 1/8 2^-3
Define a six-element array of integers named A
with the initial values 3, 7, 27, 38, and 12. Write a program that reverses the order of the elements in this array.
The array is defined as follows:
A[arraysize]={5, 7, -12, 235, 178, -100, 236, 1, 9, 0, 11, 102};
Write a program that sums up all the elements in the array A
and prints the result.
Write a program that reads 10 integers from input and displays them in reverse order.
Consider an array of integers named A
of size n = 5
. Write a program that reads the elements of this array from input, separates the positive and negative numbers, and stores the positive numbers in an array pos
and the negative numbers in an array neg
.
Write a program that uses an array to compute and print the first ten terms of the Fibonacci series.
Given an array of n = 10
integers, write a program that removes all duplicate values from the array and prints the unique numbers along with their frequencies.
Write a program that takes a hexadecimal number as a string input and converts it to its decimal (base 10) equivalent, then prints the result.