0% found this document useful (0 votes)
15 views15 pages

c Programs - Copy

Uploaded by

sivanagaraju779
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
15 views15 pages

c Programs - Copy

Uploaded by

sivanagaraju779
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 15

1.

C Program to Find Sum and Average of 3 Numbers

// C Program to Find Sum and Average of 3 Numbers


#include <stdio.h>
int main(){
int a, b, c, sum;
floatavg;
// Asking for input
printf("Enter 3 numbers: \n");
scanf("%d %d %d", &a, &b, &c);
// Calculating sum
sum = a + b + c;
// Calculating average of 3 numbers
avg = sum / 3;
// Displaying output
printf("Sum = %d \n", sum);
printf("Average = %.2f", avg);
return 0;
}

Output:

Enter Three Numbers :


10
20
30

Sum of Three Numebers is : 60


Average of Three Numebers is : 20

2.C Program to Convert Fahrenheit to Celsius


#include <stdio.h>

int main()

float celsius, fahrenheit;

printf("Please Enter the temperature in Fahrenheit: \n");

scanf("%f", &fahrenheit);
// Convert the temperature from f to c formula

celsius = (fahrenheit - 32) * 5 / 9;

//celsius = 5 *(fahrenheit - 32) / 9;

//celsius =(fahrenheit-32) * 0.55556;

printf("\n %.2f Fahrenheit = %.2f Celsius", fahrenheit, celsius);

return 0;

Output:
Please Enter the temperature in fahrenheit:
62
62.0 fahrenheit = 16.67 celsius

3. Write A c Program to Calculate Simple Interest

Simple interest formula is given by.

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);

printf("Enter time: ");


scanf("%f",&time);

printf("Enter rate: ");


scanf("%f",&rate);

/* Calculate simple interest */


SI =(principle * time * rate)/100;

/* Print the resultant value of SI */


printf("Simple Interest = %f", SI);

return0;
}
Output:
Enter Principal (amount): 1200
Enter time: 2
Enter rate: 3
Simple Interest = 72.000000

4.

(a)Write a c Program by using arithmetic operators

// Working of arithmetic operators


#include<stdio.h>
int main()
{
int a = 9,b = 4, 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("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

(b)Write a c Program by using Logical operators

// Working of logical operators

#include<stdio.h>
Int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", 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);

if((first>second) && (first>third) && (first>fourth))


printf("\nFirst number is largest");
else if((second>first) && (second>third) && (second>fourth))
printf("\nSecond number is largest");
else if((third>second) && (third>first) && (third>fourth))
printf("\nThird number is largest");
else if((fourth>second) && (fourth>third) && (fourth>first))
printf("\nFourth number is largest");

if((first<second) && (first<third) && (first<fourth))


printf("\nFirst number is smallest");
else if((second<first) && (second<third) && (second<fourth))
printf("\nSecond number is smallest");
else if((third<second) && (third<first) && (third<fourth))
printf("\nThird number is smallest");
else if((fourth<second) && (fourth<third) && (fourth<first))
printf("\nFourth number is smallest");

printf("\n");
return 0;
}

Output:
Enter four integers(separated by space):1
2
3
4

Fourth number largest


First number is smallest
(6)Write a C Program to print the day of the week using a switch case.

/**
* C program to print day of week using switch case
*/

#include <stdio.h>

int main()
{
int week;

/* Input week number from user */


printf("Enter week number(1-7): ");
scanf("%d", &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

7.Find the factorial of a given number using for loop

#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

8)find the given number is prime or not


#include<stdio.h>
int main(){
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}

Output:
Enter the number to check prime:24
Number is not prime

Enter the number to check prime:23


Number is 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:

Enter the number of rows : 5


*
**
***
****
*****
(b)Half Pyramid of Numbers
#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("%d ", j);
}
printf("\n");
}
return 0;
}
Output:

Enter the number of rows : 5


1
12
123
1234
12345

10. Write a c Program to find Addition of two matrices


#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d%d", &m, &n);
int a[m][n], b[m][n], c[m][n];
printf("Enter the elements of matrix A: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of matrix B: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
// add the matrices
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
// print the result
printf("The sum of the two matrices is: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}

Output:

Enter the number of rows and columns of the matrices: 2 2


Enter the elements of matrix A:
12
34
Enter the elements of matrix B:
56
78
The sum of the two matrices is:
68
10 12

11.C Program to Find Sum of two One-Dimensional Arrays using Malloc

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>

/* Swap function declaration */


voidswap(int* num1,int* num2);

intmain()
{
int num1, num2;

/* Input numbers */
printf("Enter two numbers: ");
scanf("%d%d",&num1,&num2);

/* Print original values of num1 and num2 */


printf("Before swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);

/* Pass the addresses of num1 and num2 */


swap(&num1,&num2);

/* Print the swapped values of num1 and num2 */


printf("After swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);

return0;
}

/**
* Function to swap two numbers
*/
voidswap(int* num1,int* num2)
{
int temp;

// Copy the value of num1 to some temp variable


temp =*num1;

// Copy the value of num2 to num1


*num1=*num2;

// Copy the value of num1 stored in temp to num2


*num2= temp;

printf("After swapping in swap function n");


printf("Value of num1 = %d \n",*num1);
printf("Value of num2 = %d \n\n",*num2);
}

Output:
Enter two numbers:10 20
Before Swapping in main
Value of num1=10
Value of num2=20

After swapping in swap function


Value of num1=20
Value of num2=10

After swapping in main


Value of num1=20
Value of num2=10

13.Write a recursive function to generate fibonacci series

#include<stdio.h>

int main()

int first=0, second=1, i, n, sum=0;

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

scanf("%d",&n);

//accepting the terms


printf("Fibonacci Series:");

for(i=0 ; i<n ; i++)

if(i<= 1)

sum=i;

//to print 0 and 1

else

sum=first + second;

first=second;

second=sum;

//to calculate the remaining terms.

//value of first and second changes as a new term is printed.

printf(" %d",sum)

return 0;

}
Output:

Enter the number of terms:5


Fibonacci series:01123

14.Linked List Operations Program( Single/Double/Circular) ANY ONE.


15.Stack Operations Program.
16.Queue Operations Program.
17.Tree Traversal Program.

You might also like