0% found this document useful (0 votes)
13 views8 pages

Programs 12 24

The document contains multiple C programs that demonstrate various programming concepts, including calculating the sum of digits, generating Fibonacci sequences, solving quadratic equations, and performing matrix operations. Each program is accompanied by example inputs and outputs to illustrate its functionality. The programs cover a range of topics from basic arithmetic to array manipulation and matrix operations.

Uploaded by

vontela.neelima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Programs 12 24

The document contains multiple C programs that demonstrate various programming concepts, including calculating the sum of digits, generating Fibonacci sequences, solving quadratic equations, and performing matrix operations. Each program is accompanied by example inputs and outputs to illustrate its functionality. The programs cover a range of topics from basic arithmetic to array manipulation and matrix operations.

Uploaded by

vontela.neelima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

12.

write a c program to find the sum of individual digits of a positive integer and
test given number is palindrome.

#include<stdio.h>
int main()
{
int n,num,sum,r,s=0,p=0;
printf(“enter a number\n”);
scanf(“%d”,&n);
num=n;
while(n>0)
{
r=n%10;
s=s+r;
p=p*10+r;
n=n/10;
}
printf(“sum of individual digits=%d\n”,s);
if(num==p)
printf(“%d is palindrome \n”,num);
else
printf(“%d is not palindrome”,num);
return 0;
}
Output:
enter a number
121
sum of individual digits=4
121 is palindrome

13. A Fibonacci sequence is is defined as follows: the first and second terms in the
sequence are 0 and 1.Subsequent terms are found by adding the preceding two
terms in the sequence. Write a a C program to generate the first n terms of the
sequence.

#include<stdio.h>
int main()
{
int i=0,j=1,k,c,n;
printf(“enter number of terms\n”);
scanf(“%d”,&n);
printf(“%d\t %d\t”,i,j);
k=i+j;
for(c=3;c<=n;c++)
{
printf(“%d\t”,k);
i=j;
j=k;
k=i+j;
}
return 0;
}

Output:
enter number of terms
5
0 1 1 2 3

14. write a c program to find the roots of a quadratic equation.


#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,r1,r2,d;
printf("enter values for a,b and c\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
printf("entered value should not be zero\n");
else
{
d=b*b-4*a*c;
if(d>0)
{
r1=(-b+sqrt(d)/(2*a));
r2=(-b+sqrt(d)/(2*a));
printf("roots are real and unequal\n");
printf("roots are %f and %f\n",r1,r2);
}
else if(d==0)
{
r1=-b/(2*a);
r2=-b/(2*a);
printf("roots are real and equal\n");
printf("roots are %f and %f\n",r1,r2);
}
else
printf("roots are imaginary\n");
}
return 0;
}
Output:
enter values for a,b and c
543
roots are imaginary

15. write a c program to calculate the following, where x is a fractional value.


1-x/2+x^2/4-x^3/6

#include<stdio.h>
#include<math.h>
int main()
{
float x,s;
printf("enter x value\n");
scanf("%f",&x);
s=1-(pow(x,1)/2)+(pow(x,2)/4)-(pow(x,3)/6);
printf("%f",s);
return 0;
}

Output:
enter x value
4
-7.666667

16. write a c program to read in two numbers, x and n, and then compute the sum
of this geometric progression:1+x+x^2+x^3+....... x ^n. For example :if n is 3 and x
is 5,then the program computes 1+5+25+125.

#include<stdio.h>
#include<math.h>
int main()
{
int x,n,i;
float s=0;
printf("enter x and nvalue\n");
scanf("%d%d",&x,&n);
for(i=0;i<=n;i++)
s=s+pow(x,i);
printf("sum of geometric progression = %f",s);
return 0;
}
Output:
enter x and n value
5
3
sum of geometric progression = 156.000000
17. Write a c program to initialize an array and display elements.
#include <stdio.h>

int main()
{
int a[5]={1,2,3,4,5}; //compile time initialization
int i;
for(i=0;i<5;i++)
printf("%d\n",a[i]); //display array elements
return 0;
}
18. Write a c program to read elements into an array and display them.

#include <stdio.h>
int main()
{
int a[5], i;
printf("Enter elements: ");
for(i = 0; i < 5; ++i)
scanf("%d",&a[i]);
printf("You entered: \n");
for(i = 0; i < 5; ++i)
printf("%d\n", a[i]);
return 0;
}
19. write a c program to display array elements in reverse order.

#include <stdio.h>
int main() {
int a[5],i;
printf("enter elements\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("elements of array in reverse\n"); for(i=4;i>=0;i--)
printf("%d\n",a[i]);
return 0;
}
20. write a c program to find sum and average in an array of 10 elements.

#include<stdio.h>
int main()
{
int a[5], i,sum=0;
float avg;
printf("Enter elements: ");
for(i = 0; i < 5; ++i)
scanf("%d",&a[i]);
// finding sum and average of elements in an array
for(i = 0; i < 5; ++i)
sum=sum+a[i];
avg=(float) sum/5;
printf("You entered: \n");
for(i = 0; i < 5; ++i)
printf("%d\n", a[i]);
printf(“sum=%d avg=%f\n”,sum,avg);
return 0;
}
21. write a c program to create and display 2-dimensional arrays.

#include<stdio.h>
int main( )
{
int i,j;
int a[3][3] = { { 1,2,3}, {4,5,6}, {7,8,9}};
printf(“elements of an array \n \n”);
for( i=0; i<3; i++)
{
for ( j=0; j<3; j++)
{
printf (“%d\t”, a[ i ][ j ]);
}
printf(“\n”);
}
}
22. write a c Program to read the matrix of the order upto 10 x 10 elements and display the
same in matrix form.
include<stdio.h>
int main( )
{
int i, j, row, col, a[10][10];
printf(“ Enter row size and column size \n:”);
scanf(“ %d %d ”, &row, &col);
printf(“\n Enter Elements of matrix A: \n”);
for( i=0; i<row ; i++)
{
for( j=0; j<col; j++)
{
scanf(“%d”, &a[ i ][ j ]);
}
}
printf(“ \n The matrix is: \n”);
for( i=0; i<row; i++)
{
for( j=0; j<col; j++)
{
printf(“ %d ”, a[ i ][ j ]);
}
printf(“\n”);
}
return 0;
}
23. write a c Program to read the elements of the matrix of the order upto 10 x 10 & transpose its

elements.

include<stdio.h>
int main( )
{
int i,j, row, col, a[10][10], b[10][10];
printf(“ \n Enter order of matrix upto (10 x 10) A:”);
scanf(“ %d %d ”, &row, &col);
printf(“\n Enter Elements of matrix A: \n”);
for( i=0; i < row; i++)
{
for( j=0; j<col; j++)
scanf(“ %d ”, &a[ i ][ j ]);
}
/* transposing logic simply copying one matrix elements to another in reverse order */

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


{
for( j=0; j < col; j++)
b[ j ][ i ]=a[ i ][ j ];
}
printf(“ \n The Matrix Transpose is \ n”);
for( i=0; i<row; i++)
{
for( j=0; j<col; j++)
printf(“%d”, b[ i ][ j ]);
printf (“ \ n”);
}
return 0;
}
24.write a c program to perform matrix addition.
#include<stdio.h>
int main( )
{
int i,j,r1,c1, a[10][10], b[10][10];
printf(“Enter Order of Matrix A & B upto 10 x 10:”);
scanf(“%d %d”, &r1, &c1);
printf(“Enter Elements of Matrix of A: \n”);
for( i=0; i < r1; i++)
{
for( j=0; j<c1; j++)
scanf(“ %d ”, &a[ i ][ j ]);
}
printf(“Enter Elements of Matrix of B: \ n”);
for( i=0; i < r1; i++)
{
for( j=0; j < c1; j++)
scanf(“ %d ”, &b[ i ][ j ]);
}

printf(“\n Matrix Addition \n”);


for( i=0; i < r1; i++)
{
for( j=0; j < c1; j++)
printf(“%d\t”, a[ i ][ j ] + b[ i ][ j ]);
printf (“ \n”);
}
return 0;
}

You might also like