c Programs - Copy
c Programs - Copy
Output:
int main()
scanf("%f", &fahrenheit);
// Convert the temperature from f to c formula
return 0;
Output:
Please Enter the temperature in fahrenheit:
62
62.0 fahrenheit = 16.67 celsius
Where,
P is the principle amount
T is the time and
R is the rate
/**
* C program to calculate simple interest
*/
#include<stdio.h>
int main()
{
float principle, time, rate, SI;
/* Input principle, rate and time */
printf("Enter principle (amount): ");
scanf("%f",&principle);
return0;
}
Output:
Enter Principal (amount): 1200
Enter time: 2
Enter rate: 3
Simple Interest = 72.000000
4.
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output:
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
#include<stdio.h>
Int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Output:
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
5)To find maximun and minimum of four numbers using if-else statement.
#include<stdio.h>
int main(void)
{
int first, second, third, fourth;
printf("Enter four integers (separated by space): ");
scanf("%d %d %d %d", &first, &second, &third, &fourth);
printf("\n");
return 0;
}
Output:
Enter four integers(separated by space):1
2
3
4
/**
* C program to print day of week using switch case
*/
#include <stdio.h>
int main()
{
int week;
switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number
between 1-7.");
}
return 0;
}
Output:
Enter week number (1-7): 1
Monday
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Output:
Enter a number: 5
Factorial of 5 is: 120
Output:
Enter the number to check prime:24
Number is not prime
9
(a) Write a c Program to Construct a pyramid of numbers
Half Pyramid of *
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Output:
1. /*
2. * C program to find the sum of two one-dimensional arrays using
3. * Dynamic Memory Allocation
4. */
5.
6. #include <stdio.h>
7. #include <malloc.h>
8. #include <stdlib.h>
9.
10. void main()
11. {
12.
13. int i, n;
14. int *a, *b, *c;
15.
16. printf("Enter the size of the arrays\n");
17. scanf("%d", &n);
18.
19. a = (int *)malloc(n * sizeof(int));
20. b = (int *)malloc(n * sizeof(int));
21. c = (int *)malloc(n * sizeof(int));
22.
23. printf("Enter Elements of First List\n");
24.
25. for (i = 0; i < n; i++)
26. {
27. scanf("%d", a + i);
28. }
29.
30. printf("Enter Elements of Second List\n");
31.
32. for (i = 0; i < n; i++)
33. {
34. scanf("%d", b + i);
35. }
36.
37. for (i = 0; i < n; i++)
38. {
39. *(c + i) = *(a + i) + *(b + i);
40. }
41.
42. printf("Resultant List is\n");
43.
44. for (i = 0; i < n; i++)
45. {
46. printf("%d\n", *(c + i));
47. }
48. return 0;
49. }
Output:
Enter the size of the arrays
5
Enter Elements of First List
23
45
67
12
90
Enter Elements of Second List
87
56
90
45
10
Resultant List is
110
101
157
57
100
12.write aC program to swap two number using call by reference
/**
*
*/
#include<stdio.h>
intmain()
{
int num1, num2;
/* Input numbers */
printf("Enter two numbers: ");
scanf("%d%d",&num1,&num2);
return0;
}
/**
* Function to swap two numbers
*/
voidswap(int* num1,int* num2)
{
int temp;
Output:
Enter two numbers:10 20
Before Swapping in main
Value of num1=10
Value of num2=20
#include<stdio.h>
int main()
scanf("%d",&n);
if(i<= 1)
sum=i;
else
sum=first + second;
first=second;
second=sum;
printf(" %d",sum)
return 0;
}
Output: