Beruflich Dokumente
Kultur Dokumente
This C Program calculates the area of a triangle given it’s three sides.
Problem Solution
The formula or algorithm used is: Area = sqrt(s(s – a)(s – b)(s – c)), where s = (a + b + c) / 2 or perimeter
/ 2. and a, b & c are the sides of triangle.
Program/Source Code
Here is source code of the C program to calculate the area of a triangle. The C program is successfully
compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the area of a triangle, given three sides
*/
#include <stdio.h>
#include <math.h>
void main()
{
int s, a, b, c, area;
In this C program, library function defined in <math.h> header file is used to compute mathematical
functions. We are reading the three sides of a triangle using ‘a’, ‘b’, ‘c’ integer variables. To find the area
of a triangle, the following formula is used.
Area = sqrt (s * (s – a) * (s – b) * (s – c))
Runtime Test Cases
$ cc pgm1.c -lm
$ a.out
Enter the values of a, b and c
12 10 8
Area of a triangle = 39
2. Even or odd program in C
C program to check odd or even: We will determine whether a number is odd or even by using
different methods all are provided with a C language program. As you have studied in Mathematics
that in decimal number system even numbers are divisible by two while odd numbers are not so we
can use modulus operator(%) which returns remainder, for example, 4%3 gives 1 (remainder when
four is divided by three). Even numbers are of the form 2*n, and odd numbers are of the form (2*n+1)
where n is is an integer.
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &n);
9.
10. if (n%2 == 0)
11. printf("Even\n");
12. else
13. printf("Odd\n");
14.
15. return 0;
16. }
We can use bitwise AND (&) operator to check odd or even, as an example consider binary of 7
(0111) when we perform 7 & 1 the result will be one, and you may observe that the least significant bit
of every odd number is 1. Therefore (odd_number & 1) will be one always and also (even_number &
1) is always zero.
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Input an integer\n");
8. scanf("%d", &n);
9.
10. if (n & 1 == 1)
11. printf("Odd\n");
12. else
13. printf("Even\n");
14.
15. return 0;
16. }
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Input an integer\n");
8. scanf("%d", &n);
9.
10. n%2 == 0 ? printf("Even\n") : printf("Odd\n");
11.
12. return 0;
13. }
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &n);
9.
10. if ((n/2)*2 == n)
11. printf("Even\n");
12. else
13. printf("Odd\n");
14.
15. return 0;
16. }
In C programming language when we divide two integers we get an integer result, for example, 7/3 =
2. So we can use it to find whether the number is odd or even. Consider an integer 'n' we can first
divide it by two and then multiply it by two if the result is the same as the original number then the
number is even otherwise it is odd. For example, 11/2 = 5, 5*2 = 10 (which isn't equal to eleven), now
consider 12/2 = 6 and 6*2 = 12 (same as original number). These are some logic which you may use
to determine if a number is odd or even.
Definition
A formal definition of an even number is that it is an integer of the form n = 2k, where k is an
integer;[3] it can then be shown that an odd number is an integer of the form n = 2k + 1.
#include <stdio.h>
int main()
{
int number;
return 0;
}
Sample Output
Enter an integer : 4
The number you entered is an EVEN number
3. Sum of n numbers in C
Sum of n numbers in C: This program adds n numbers which will be entered by a user. The user will
enter a number indicating how many numbers to add and then the user will enter n numbers. We can
do this by using or without using an array.
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, sum = 0, c, value;
6.
7. printf("How many numbers you want to add?\n");
8. scanf("%d", &n);
9.
10. printf("Enter %d integers\n", n);
11.
12. for (c = 1; c <= n; c++)
13. {
14. scanf("%d", &value);
15. sum = sum + value;
16. }
17.
18. printf("Sum of the integers = %d\n", sum);
19.
20. return 0;
21. }
You can use long int or long long data type for the variable sum.
Download Add n numbers program.
Output of program:
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, sum = 0, c, array[100];
6.
7. scanf("%d", &n);
8.
9. for (c = 0; c < n; c++)
10. {
11. scanf("%d", &array[c]);
12. sum = sum + array[c];
13. }
14.
15. printf("Sum = %d\n", sum);
16.
17. return 0;
18. }
The advantage of using an array is that we have a record of the numbers entered and can use them
further in the program if required but storing them requires additional memory.
1. #include <stdio.h>
2.
3. long calculate_sum(int [], int);
4.
5. int main()
6. {
7. int n, c, array[100];
8. long result;
9.
10. scanf("%d", &n);
11.
12. for (c = 0; c < n; c++)
13. scanf("%d", &array[c]);
14.
15. result = calculate_sum(array, n);
16.
17. printf("Sum = %ld\n", result);
18.
19. return 0;
20. }
21.
22. long calculate_sum(int a[], int n) {
23. static long sum = 0;
24.
25. if (n == 0)
26. return sum;
27.
28. sum = sum + a[n-1];
29.
30. return calculate_sum(a, --n);
31. }
This program takes the 3 numbers and finds the biggest among all.
Problem Solution
Here is source code of the C program to calculate the biggest of 3 numbers. The C program is
successfully compiled and run on a Linux system. The program output is also shown below.
1. /*
2. * C program to find the biggest of three numbers
3. */
4. #include <stdio.h>
5.
6. void main()
7. {
8. int num1, num2, num3;
9.
10. printf("Enter the values of num1, num2 and num3\n");
11. scanf("%d %d %d", &num1, &num2, &num3);
12. printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3);
13. if (num1 > num2)
14. {
15. if (num1 > num3)
16. {
17. printf("num1 is the greatest among three \n");
18. }
19. else
20. {
21. printf("num3 is the greatest among three \n");
22. }
23. }
24. else if (num2 > num3)
25. printf("num2 is the greatest among three \n");
26. else
27. printf("num3 is the greatest among three \n");
28. }
Program Explanation
1. Take the three numbers and store it in the variables num1, num2 and num3 respectively.
2. Firstly check if the num1 is greater than num2.
3. If it is, then check if it is greater than num3.
4. If it is, then print the output as “num1 is the greatest among three”.
5. Otherwise print the ouput as “num3 is the greatest among three”.
6. If the num1 is not greater than num2, then check if num2 is greater than num3.
7. If it is, then print the output as “num2 is the greatest among three”.
8. Otherwise print the output as “num3 is the greatest among three”.
Runtime Test Cases
Case:1
Enter the values of num1, num2 and num3
6 8 10
num1 = 6 num2 = 8 num3 = 10
num3 is the greatest among three
Case:2
Enter the values of num1, num2 and num3
10 87 99
num1 = 10 num2 = 87 num3 = 99
num3 is the greatest among three