1.Write a program that computes an expression. //evaluate an expression #include<stdio.h> #include<conio.h> #include<stdlib.h> void main(int argc,char* argv[]) { int a,b,c,d,v; a=atoi(argv[1]); b=atoi(argv[2]); c=atoi(argv[3]); d=atoi(argv[4]); v=((2*a)*(5*b))+c-((6*d)*(5*c)); printf("%d\n",v); } ------------------------------------------------------------ -------------------------- 2.Write a program that prints out the first 'n' numbers in the Fibonacci sequence. //fib series #include<stdio.h> #include<conio.h> #include<stdlib.h> void main(int argc,char* argv[]) { int n=atoi(argv[1]); int f1,f2,f3,i=1; f1=1; f2=0; f3=f1+f2; printf("%d\n",f3); while(i<n)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1.Write a program that computes an expression.
//evaluate an expression#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(int argc,char* argv[]){
int hex,b;sscanf(argv[1],"%x",&hex);b=0X3FF0;hex=hex&b;hex=hex>>4;printf("%X",hex);
}----------------------------------------------------------------------------------------5.Write a program to find the sum of series 1+x+x2+x3+x4+....+xn.
//sum of series#include<stdio.h>#include<stdlib.h>#include<math.h>void main(int argc,char* argv[])
{int n,i,x;long int s=0;x=atoi(argv[1]);n=atoi(argv[2]);for(i=0;i<=n;i++){
s=s+(long int)pow(x,i);}
printf("%ld\n",s);}------------------------------------------------------------------------------------------6.Write a program that prints the transpose of a matrix
//trans of a matrix#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<math.h>void main(int argc,char* argv[]){
}-----------------------------------------------------------------------------------------------7.Write a program to compute the sum of natural numbers.
//sum of first n natural nos#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(int argc,char* argv[]){
long int n;long int i,s=0;n=atoi(argv[1]);for(i=1;i<=n;i++){
------------------------------------------------------------------------------------------------------10.Write a program to display the given file contents in rank order.
}}---------------------------------------------------------------------------------------------------------13.Write a program that will print all possible combination of letters in a word lexicographically. ---------------------------------------------------------------------------------------------------------14.Write a program that reverses the content of each line in a file
}---------------------------------------------------------------------------------------------------------15.Write a program that sorts out the words in a file lexicographically
int a[10][10],b[10][10],r,c,i,j;printf("First Matrix read from file1\n");readmat(argv[1],a,&r,&c);
printf("Second Matrix read from file2 \n");readmat(argv[2],b,&r,&c);printf("Resultant Matrix printing to another file ");printf("Sum is \n");for(i=0;i<c;i++){
---------------------------------------------------------------------------------------------------------------------------------------------Data Structures and algorithm---------------------------------------------------------------------------------------------------------------------------------------------1.Single Linked List
/* theRear is a pointer at the rear end of the queue */theQueue* theRear=NULL;/* theFront is a pointer at the front end of the queue */theQueue* theFront=NULL;
/* Function Prototypes */ void fnCreateQueue(FILE*);void fnDisplay(theQueue*);
int main(int iArgc,char* pcArgv[]) {FILE* fpFile;fpFile=fopen(pcArgv[1],"r"); fnCreateList(fpFile);printf("\nDoubly linked list before delete\n");
fnDisplay(theHead);
fnDelNode(theHead);
printf("\nDoubly linked list after delete\n");
fnDisplay(theHead);return (0); }/* fnDisplay will display the doubly linked listed from start to end */void fnDisplay(theNode* psTraverse) {while(psTraverse!=NULL) {printf("%d ",psTraverse->iData); psTraverse=psTraverse->psRight;}}/* fnCreateList will read the data from the file and create a doubly linked list */void fnCreateList(FILE* fp) {theNode *ref,*temp;int m; while(!feof(fp)) {
struct SDll {int iData; struct SDll* psLeft; struct SDll* psRight; };/* theHead will point to the start of the doubly linked list */
theNode *theHead=NULL;
/*Function Prototype */void fnCreateList(FILE*); void fnDisplay(theNode*); void fnDelNode(theNode*); int main(int iArgc,char* pcArgv[]) {FILE* fpFile;fpFile=fopen(pcArgv[1],"r"); fnCreateList(fpFile);printf("\nDoubly linked list before delete\n");
fnDisplay(theHead);
fnDelNode(theHead);
printf("\nDoubly linked list after delete\n");
fnDisplay(theHead);return (0); }/* fnDisplay will display the doubly linked listed from start to end */void fnDisplay(theNode* psTraverse) {while(psTraverse!=NULL) {printf("%d ",psTraverse->iData); psTraverse=psTraverse->psRight;}}/* fnCreateList will read the data from the file and create a doubly linked list */void fnCreateList(FILE* fp) {theNode *ref,*temp;int m; while(!feof(fp))
struct SDll {int iData; struct SDll* psLeft; struct SDll* psRight; };/* theHead will point to the start of the doubly linked list */
theNode *theHead=NULL;
/*Function Prototype */void fnCreateList(FILE*); void fnDisplay(theNode*); void fnSwapNode(theNode*); int main(int iArgc,char* pcArgv[]) {FILE* fpFile;fpFile=fopen(pcArgv[1],"r"); fnCreateList(fpFile);printf("Doubly linked list before Swaping the node\n"); fnDisplay(theHead);fnSwapNode(theHead);printf("\nDoubly linked list after Swap\n"); fnDisplay(theHead);return (0); }/* fnDisplay will display the doubly linked listed from start to end */void fnDisplay(theNode* psTraverse) {while(psTraverse!=NULL) {printf("%d ",psTraverse->iData);
psTraverse=psTraverse->psRight;}}/* fnCreateList will read the data from the file and create a doubly linked list */void fnCreateList(FILE* fp) {theNode *ref,*temp;int m; while(!feof(fp)) {
case 4:printf ("Enter an integer: ");scanf ("%d", &n);fflush (stdin);p = makenode (n);head = insertAtEnd(p, head);break;
case 5:delAtEnd(head);break;
case 6:printf ("Enter an integer: ");scanf ("%d", &n);printf ("Enter the posn: ");scanf ("%d", &m);fflush (stdin);p = makenode (n);head = insertAtPos(p, head,m);break;
case 7:printf ("Enter the posn: ");scanf ("%d", &n);
fflush (stdin);head = delAtPos(head,n);break;
case 8:exit(0);
}}while(c!=9);return 0;
}-----------------------------------------------------------------------------------------------------------------------------------10.queue*/#include <stdio.h>#include <string.h>#include <stdlib.h>struct SQueue{ int iData;
struct SQueue* psLink;};typedef struct SQueue theQueue;theQueue* theRear=NULL; /* theRear is a pointer at the rear end of the queue */theQueue* theFront=NULL; /* theFront is a pointer at the front end of the queue */void fnCreateQueue(FILE* fp) // fnCreateQueue will read the data from the file and put it in the queue{ int num,i;
fpFile=fopen(pcArgv[1],"r");fnCreateList(fpFile);printf("Doubly linked list before deleting the node\n");fnDisplay(theHead);fnSearchLIst(atoi(pcArgv[2]));fnDeleteNode(atoi(pcArgv[3]));printf("\nDoubly linked list after deletion\n");fnDisplay(theHead);addLIst(atoi(pcArgv[4]),atoi(pcArgv[5]));
printf("\nDoubly linked list after insertion\n");//fnDisplay(theHead);return (0);
FILE* fp;fp=fopen(pcArgv[1],"r");fnCreateList(fp);printf("Created linked list is \n");fnDisplay(theHead);fnSwap(theHead);printf("Swapped linked list is \n");fnDisplay(theHead);return (0);
}}---------------------------------------------------------------------------------------------------------/*Write a program to swap only the first and last node in the Doubly Linked List */#include<stdio.h> #include<stdlib.h> typedef struct SDll theNode;
struct SDll {int iData; struct SDll* psLeft; struct SDll* psRight; };/* theHead will point to the start of the doubly linked list */
theNode *theHead=NULL;
/*Function Prototype */void fnCreateList(FILE*); void fnDisplay(theNode*); void fnSwapNode(theNode*); int main(int iArgc,char* pcArgv[]) {FILE* fpFile;fpFile=fopen(pcArgv[1],"r"); fnCreateList(fpFile);printf("Doubly linked list before Swaping the node\n"); fnDisplay(theHead);fnSwapNode(theHead);printf("\nDoubly linked list after Swap\n"); fnDisplay(theHead);return (0); }/* fnDisplay will display the doubly linked listed from start to end */void fnDisplay(theNode* psTraverse) {while(psTraverse!=NULL) {printf("%d ",psTraverse->iData); psTraverse=psTraverse->psRight;}}/* fnCreateList will read the data from the file and create a doubly linked list */void fnCreateList(FILE* fp) {
FILE *fpFilePointer; fpFilePointer=fopen(pcArgv[1],"r");fnCreateTree(fpFilePointer);printf("Inorder traversal of the Binary search tree is\n");
fnInorderTraversal(theRoot); printf("\n");fnPreorderTraversal(theRoot);printf("\n");fnPostorderTraversal(theRoot); printf("\n");int n=count(theRoot);printf("The total number of nodes:%d",n);return (0);
------------------------------------------------------COUNT NUMBER OF NODES IN TREE----------------------------------------------------- int count(theNode* psNode){
/* theRear is a pointer at the rear end of the queue */theQueue* theRear=NULL;/* theFront is a pointer at the front end of the queue */theQueue* theFront=NULL;
/* Function Prototypes */ void fnCreateQueue(FILE*);void fnDisplay(theQueue*);void fnDeleteQueue(theQueue*);
int n;n=psTraverse->iData;theFront=theFront->psLink;if(theFront==NULL)
theRear=NULL;psTraverse->psLink=NULL;
}------------------------------------------------------------------------------------------------------------------------------------SINGLY LINKED LIST
-----------------------------------------------------------------------------------------------C Prog With O/P----------------------------------------------------------------------------------------------
1.Write a C program to convert the given binary number into its equivalent decimal */
#include <stdio.h>
void main(){ int num, bnum, dec = 0, base = 1, rem ;
printf("Enter the binary number(1s and 0s)\n"); scanf("%d", &num); /*Enter maximum five digits or use long int*/
bnum = num;
while( num > 0) { rem = num % 10;
dec = dec + rem * base; num = num / 10 ;
base = base * 2; }
printf("The Binary number is = %d\n", bnum); printf("Its decimal equivalent is =%d\n", dec);
} /* End of main() */
OutpputEnter the binary number(1s and 0s)1010The Binary number is = 1010Its decimal equivalent is =10
---------------------------------------------------------------------------------------------------2.Write a c program to multifly given number by 4 using bitwise operators
#include <stdio.h>
void main() {
long number, tempnum;
printf("Enter an integer\n");scanf("%ld",&number);tempnum = number;number = number << 2; /*left shift by two bits*/
printf("%ld x 4 = %ld\n", tempnum,number);}
OutputEnter an integer1515 x 4 = 60
RUN2Enter an integer262262 x 4 = 1048---------------------------------------------------------------------------------------------------
3.Write a C program to find the sum of cos(x) series
#include<stdio.h>#include<math.h>
void main(){ int n,x1,i,j; float x,sign,cosx,fact;
printf("Enter the number of the terms in aseries\n"); scanf("%d",&n);
printf("Enter the value of x(in degrees)\n");scanf("%f",&x);
x1=x;
x=x*(3.142/180.0); /* Degrees to radians*/
cosx=1;
sign=-1;
for(i=2;i<=n;i=i+2) {
fact=1;
for(j=1;j<=i;j++) {
fact=fact*j; }
cosx=cosx+(pow(x,i)/fact)*sign; sign=sign*(-1);
}
printf("Sum of the cosine series = %7.2f\n",cosx); printf("The value of cos(%d) using library function = %f\n",x1,cos(x));
} /*End of main() */
OutputEnter the number of the terms in aseries5Enter the value of x(in degrees)60Sum of the cosine series = 0.50The value of cos(60) using library function = 0.499882------------------------------------------------------------------------------------------------------------------4.Writ a C programme to cyclically permute the elements of an array A
i.e. the content of A1 become that of A2.And A2 contains that of A3 & so on as An contains A1
#include <stdio.h>
void main (){
int i,n,number[30];printf("Enter the value of the n = ");
scanf ("%d", &n);printf ("Enter the numbers\n");for (i=0; i<n; ++i){
scanf ("%d", &number[i]);}number[n] = number[0];
for (i=0; i<n; ++i){
number[i] = number[i+1];}printf ("Cyclically permted numbers are given below \n");for (i=0; i<n; ++i)printf ("%d\n", number[i]);
}
OutputEnter the value of the n = 5Enter the numbers1030204518Cyclically permted numbers are given below3020451810---------------------------------------------------------------------------------------------------------------------------5.Write a C program to accept a set of numbers and compute mean,varience and standard deviation
#include <stdio.h>#include <math.h>
void main(){ float x[10];
int i, n; float avrg, var, SD, sum=0, sum1=0;
printf("Enter howmany elements\n");
scanf("%d", &n);
for(i=0; i<n; i++) {
scanf("%f", &x[i]); }
/* Compute the sum of all elements */ for(i=0; i<n; i++)
{ sum = sum + x[i];
} avrg = sum /(float) n;
/* Compute varaience and standard deviation */for(i=0; i<n; i++)
{ sum1 = sum1 + pow((x[i] - avrg),2);
}
var = sum1 / (float) n;SD = sqrt(var);
printf("Avrage of all elements =%.2f\n", avrg); printf("Varience of all elements =%.2f\n", avrg);
printf("Standard deviation of all elements =%.2f\n", SD);
}
OutputEnter howmany elements51021325917Avrage of all elements =27.80Varience of all elements =27.80Standard deviation of all elements =17.15-----------------------------------------------------------------------------------------------------------------------6. Write a C program to accept N numbers and arrange them in an asceding order
#include <stdio.h>
void main (){ int i,j,a,n,number[30];
printf ("Enter the value of N\n"); scanf ("%d", &n);
printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]);
for (i=0; i<n; ++i) { for (j=i+1; j<n; ++j)
{if (number[i] > number[j]){
a= number[i];number[i] = number[j];number[j] = a;
} }
}
printf ("The numbers arrenged in ascending order are given below\n"); for (i=0; i<n; ++i) printf ("%d\n",number[i]);} /* End of main() */
OutputEnter the value of N5Enter the numbers8020671045The numbers arrenged in ascending order are given below1020456780
---------------------------------------------------------------------------------------------------------------------------------7.Write a C program to accept a set of numbers and arrange them in a descending order
#include <stdio.h>
void main (){ int number[30]; int i,j,a,n;
printf ("Enter the value of N\n"); scanf ("%d", &n);
printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]);
/* sorting begins ...*/ for (i=0; i<n; ++i) { for (j=i+1; j<n; ++j)
{if (number[i] < number[j]){
a = number[i];number[i] = number[j];number[j] = a;
} }
}
printf ("The numbers arranged in descending order are given below\n"); for (i=0; i<n; ++i) {
printf ("%d\n",number[i]); }
} /* End of main() */
Output Enter the value of N6Enter the numbers10
356710042688The numbers arranged in descending order are given below68810067423510
----------------------------------------------------------------------------------------------------------------------------------------8.Write a C program to accept a list of data items and find the second largest and second smallest elements in it. And also computer the average of both. And search for the average value whether it is present in the array or not. Display appropriate message on successful search.
#include <stdio.h>
void main (){ int number[30]; int i,j,a,n,counter,ave;
printf ("Enter the value of N\n"); scanf ("%d", &n);
printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]);
for (i=0; i<n; ++i) {
for (j=i+1; j<n; ++j){
if (number[i] < number[j]){
a = number[i];number[i] = number[j];number[j] = a;
}}
}
printf ("The numbers arranged in descending order are given below\n"); for (i=0; i<n; ++i) {
printf ("%d\n",number[i]); }
printf ("The 2nd largest number is = %d\n", number[1]); printf ("The 2nd smallest number is = %d\n", number[n-2]);
ave = (number[1] +number[n-2])/2; counter = 0;
for (i=0; i<n; ++i) {
if (ave == number[i]) {
++counter; }
} if (counter == 0 )
printf ("The average of %d and %d is = %d is not in the array\n", number[1], number[n-2], ave); else
printf ("The average of %d and %d in array is %d in numbers\n",number[1], number[n-2], counter);} /* End of main() */
OutputEnter the value of N6Enter the numbers308010407090The numbers arranged in descending order are given below908070403010
The 2nd largest number is = 80The 2nd smallest number is = 30The average of 80 and 30 is = 55 is not in the array----------------------------------------------------------------------------------------------------------------------------9.Write a C program to accept an array of integers and delete the specified integer from the list
#include <stdio.h>
void main(){ int vectx[10];
int i, n, pos, element, found = 0;
printf("Enter how many elements\n"); scanf("%d", &n);
printf("Enter the elements\n"); for(i=0; i<n; i++)
{ scanf("%d", &vectx[i]);
}
printf("Input array elements are\n");for(i=0; i<n; i++){
printf("%d\n", vectx[i]);}
printf("Enter the element to be deleted\n"); scanf("%d",&element);
for(i=0; i<n; i++) {
if ( vectx[i] == element) {
found = 1;pos = i;break;
}}
if (found == 1){
for(i=pos; i< n-1; i++)
{vectx[i] = vectx[i+1];
}
printf("The resultant vector is \n"); for(i=0; i<n-1; i++)
{printf("%d\n",vectx[i]);
} } else
printf("Element %d is not found in the vector\n", element);
} /* End of main() */
Output
Run 1Enter how many elements5Enter the elements3010502040Input array elements are3010502040Enter the element to be deleted35Element 35 is not found in the vector
Run 2Enter how many elements4Enter the elements23105581Input array elements are
23105581Enter the element to be deleted55The resultant vector is231081
----------------------------------------------------------------------------------------------------------------------------------10.Write a C programme (1-D Array) store some elements in it.Accept key & split from that point. Add the first half to the end of second half
#include <stdio.h>
void main (){ int number[30]; int i,n,a,j;
printf ("Enter the value of n\n"); scanf ("%d",&n);
printf ("enter the numbers\n"); for (i=0; i<n; ++i) scanf ("%d", &number[i]);
printf ("Enter the position of the element to split the array \n"); scanf ("%d",&a);
for (i=0; i<a; ++i) {
number[n] = number[0];
for (j=0; j<n; ++j) {
number[j] = number[j+1]; }
}
printf("The resultant array is\n"); for (i=0; i<n; ++i) {
printf ("%d\n",number[i]); }} /* End of main() */
OutputEnter the value of n5enter the numbers3010405060Enter the position of the element to split the array2The resultant array is4050603010------------------------------------------------------------------------------------------------------------------------------11.Write a C program to find the frequency of odd numbers and even numbers in the input of a matrix
#include <stdio.h>
void main (){ static int ma[10][10]; int i,j,m,n,even=0,odd=0;
printf ("Enter the order ofthe matrix \n"); scanf ("%d %d",&m,&n);
printf ("Enter the coefficients if matrix \n"); for (i=0;i<m;++i) {
for (j=0;j<n;++j){
scanf ("%d", &ma[i][j]);if ((ma[i][j]%2) == 0){
++even;
}else
++odd;}
} printf ("The given matrix is\n"); for (i=0;i<m;++i) {
for (j=0;j<n;++j){
printf (" %d",ma[i][j]);}printf ("\n");
}
printf ("\nThe frequency of occurance of odd number = %d\n",odd);printf ("The frequency of occurance of even number = %d\n",even);
} /* End of main() */
OutputEnter the order ofthe matrix3 3Enter the coefficients if matrix1 2 34 5 67 8 9The given matrix is 1 2 3 4 5 6 7 8 9
The frequency of occurance of odd number = 5The frequency of occurance of even number = 4
-----------------------------------------------------------------------------------------------------------------------------12.Write a C program to accept a matrix of order M x N and store its elements and interchange the main diagonal elements of the matrix with that of the secondary diagonal elements
#include <stdio.h>
void main (){
static int ma[10][10];int i,j,m,n,a;
printf ("Enetr the order of the matix \n");scanf ("%d %d",&m,&n);
if (m ==n ){
printf ("Enter the co-efficients of the matrix\n");for (i=0;i<m;++i){
for (j=0;j<n;++j){
scanf ("%dx%d",&ma[i][j]);}
}
printf ("The given matrix is \n");for (i=0;i<m;++i){
for (j=0;j<n;++j){
printf (" %d",ma[i][j]);}printf ("\n");
}
for (i=0;i<m;++i){
a = ma[i][i];ma[i][i] = ma[i][m-i-1];ma[i][m-i-1] = a;
}
printf ("THe matrix after changing the \n");printf ("main diagonal & secondary diagonal\n");for (i=0;i<m;++i){
for (j=0;j<n;++j){
printf (" %d",ma[i][j]);}printf ("\n");
}}
elseprintf ("The givan order is not square matrix\n");
} /* end of main() */
OutputEnetr the order of the matix3 3Enter the co-efficients of the matrix1 2 34 5 67 8 9The given matrix is 1 2 3 4 5 6 7 8 9THe matrix after changing themain diagonal & secondary diagonal 3 2 1 4 5 6 9 8 7--------------------------------------------------------------------------------------------------------------------------------------13.Write a C program to find the sum of first 50 natural numbers using for loop
#include <stdio.h>
void main(){
int num,sum=0;
for(num = 1; num <= 50; num++){
sum = sum + num;}
printf("Sum = %4d\n", sum);}
OutputSum = 1275---------------------------------------------------------------------------------------------------------------------------------------14. Write a C program to accept two integers and check if they are equal
#include <stdio.h>void main(){
int m,n;
printf("Enter the values for M and N\n");scanf("%d %d", &m,&n);
if(m == n )printf("M and N are equal\n");
elseprintf("M and N are not equal\n");
}
outputEnter the values for M and N34 45M and N are not equal
--------------------------------------------------------------------------------------------------------------------------------------15.Write a C program to accept the height of a person in centermeter and categorize the person based on height as taller, dwarf and average height person
#include <stdio.h>void main(){
float ht;
printf("Enter the Height (in centimetres)\n");scanf("%f",&ht);
OutputEnter the Height (in centimetres)45Dwarf------------------------------------------------------------------------------------------------------------------------------------16.Write a C program to accept a coordinate point in a XY coordinate system and determine its quadrant
*/
#include <stdio.h>
void main(){
int x,y;
printf("Enter the values for X and Y\n");scanf("%d %d",&x,&y);
if( x > 0 && y > 0) printf("point (%d,%d) lies in the First quandrant\n");else if( x < 0 && y > 0) printf("point (%d,%d) lies in the Second quandrant\n");else if( x < 0 && y < 0) printf("point (%d, %d) lies in the Third quandrant\n");else if( x > 0 && y < 0) printf("point (%d,%d) lies in the Fourth quandrant\n");else if( x == 0 && y == 0) printf("point (%d,%d) lies at the origin\n");
} /* End of main() */
OutputRUN 1Enter the values for X and Y3 5point (5,3) lies in the First quandrant
RUN 2Enter the values for X and Y-4-7
point (-7, -4) lies in the Third quandrant
------------------------------------------------------------------------------------------------------------------------------------------17.Write a C program to accept a figure code and find the ares of different geometrical figures such as circle, square, rectangle etc using switch
#include <stdio.h>
void main(){
int fig_code;float side,base,length,bredth,height,area,radius;
Enter the Figure code3 Enter the base and height57Area of a Triangle=17.500000
------------------------------------------------------------------------------------------------------------------------------------18. Write a C program to accept a string and find the sum of all digits present in the string
#include <stdio.h>void main()
{char string[80];int count,nc=0,sum=0;
printf("Enter the string containing both digits and alphabet\n");scanf("%s",string);
for(count=0;string[count]!='\0'; count++){
if((string[count]>='0') && (string[count]<='9')){
nc += 1;sum += (string[count] - '0');
} /* End of if */} /* End of For */
printf("NO. of Digits in the string=%d\n",nc);printf("Sum of all digits=%d\n",sum);
} /* End of main() */
OutputEnter the string containing both digits and alphabetgoodmorning25NO. of Digits in the string=2Sum of all digits=7
------------------------------------------------------------------------------------------------------------------------------------19.Write a C program to accept an integer find the sum of the digits in it
#include <stdio.h>
void main(){ long num, temp, digit, sum = 0;
printf("Enter the number\n"); scanf("%ld", &num);
temp = num;
while(num > 0) {
digit = num % 10;
sum = sum + digit; num /= 10; }
printf("Given number =%ld\n", temp); printf("Sum of the digits %ld =%ld\n", temp, sum);} /* End of main()*/
Enter the number123456Given number =123456Sum of the digits 123456 =21-----------------------------------------------------------------------------------------------------------------------------------20.Write a C program to accept a matrix of order M x N and find the sum of each row and each column of a matrix #include <stdio.h>
void main (){
static int m1[10][10];int i,j,m,n,sum=0;
printf ("Enter the order of the matrix\n");scanf ("%d %d", &m,&n);printf ("Enter the co-efficients of the matrix\n");
for (i=0;i<m;++i){
for (j=0;j<n;++j){
scanf ("%d",&m1[i][j]);}
}for (i=0;i<m;++i){
for (j=0;j<n;++j){
sum = sum + m1[i][j] ;}printf ("Sum of the %d row is = %d\n",i,sum);sum = 0;
}
sum=0;
for (j=0;j<n;++j){
for (i=0;i<m;++i){
sum = sum+m1[i][j];}printf ("Sum of the %d column is = %d\n", j,sum);sum = 0;
}}
OutputEnter the order of the matrix3 3Enter the co-efficients of the matrix1 2 34 5 67 8 9Sum of the 0 row is = 6Sum of the 1 row is = 15Sum of the 2 row is = 24Sum of the 0 column is = 12Sum of the 1 column is = 15Sum of the 2 column is = 18
--------------------------------------------------------------------------------------------------------------------------------------------21.Write a C program to find accept a matric of order M x N and find the sum of the main diagonal and off diagonal elements
#include <stdio.h>
void main (){
static int ma[10][10];int i,j,m,n,a=0,sum=0;
printf ("Enetr the order of the matix \n");scanf ("%d %d",&m,&n);
if ( m == n ){
printf ("Enter the co-efficients of the matrix\n");
for (i=0;i<m;++i){
for (j=0;j<n;++j){
scanf ("%d",&ma[i][j]);}
}
printf ("The given matrix is \n");for (i=0;i<m;++i){
for (j=0;j<n;++j){
printf (" %d",ma[i][j]);}printf ("\n");
}
for (i=0;i<m;++i){
sum = sum + ma[i][i];a = a + ma[i][m-i-1];
}
printf ("\nThe sum of the main diagonal elements is = %d\n",sum);printf ("The sum of the off diagonal elemets is = %d\n",a);
}else
printf ("The givan order is not square matrix\n");} /* End of main() */
OutputEnetr the order of the matix3 3Enter the co-efficients of the matrix1 2 34 5 67 8 9The given matrix is 1 2 3 4 5 6 7 8 9
The sum of the main diagonal elements is = 15The sum of the off diagonal elemets is = 15
Run 2Enetr the order of the matix2 3The givan order is not square matrix
--------------------------------------------------------------------------------------------------------------------------------------22. Write a C program to accept a matricx of order MxN and find the trcae and normal of a matrix HINT:Trace is defined as the sum of main diagonal elements and Normal is defined as squre root of the sum of all the elements
#include <stdio.h>#include <math.h>
void main (){
static int ma[10][10];int i,j,m,n,sum=0,sum1=0,a=0,normal;
printf ("Enter the order of the matrix\n");scanf ("%d %d", &m,&n);
printf ("Enter the ncoefficients of the matrix \n");for (i=0;i<m;++i){
23.Write a C program to accept a matric and determine whether it is a sparse matrix. A sparse martix is matrix which has more zero elements than nonzero elements
#include <stdio.h>
void main (){
static int m1[10][10];int i,j,m,n;int counter=0;
printf ("Enter the order of the matix\n");scanf ("%d %d",&m,&n);
printf ("Enter the co-efficients of the matix\n");for (i=0;i<m;++i){
for (j=0;j<n;++j){
scanf ("%d",&m1[i][j]);if (m1[i][j]==0){
++counter;}
}}if (counter>((m*n)/2))
{printf ("The given matrix is sparse matrix \n");
}else
printf ("The given matrix is not a sparse matrix \n");
printf ("There are %d number of zeros",counter);
} /* EN dof main() */
OutputEnter the order of the matix2 2Enter the co-efficients of the matix1 23 4The given matrix is not a sparse matrixThere are 0 number of zeros
Run 2Enter the order of the matix3 3Enter the co-efficients of the matix1 0 00 0 10 1 0The given matrix is sparse matrixThere are 6 number of zeros
------------------------------------------------------------------------------------------------------------------------------------------24.Write a C program to accept a amtric of order MxN and sort all rows of the matrix in ascending order and all columns in descendng order
#include <stdio.h>
void main (){
static int ma[10][10],mb[10][10];int i,j,k,a,m,n;
printf ("Enter the order of the matrix \n");scanf ("%d %d", &m,&n);
printf ("Enter co-efficients of the matrix \n");for (i=0;i<m;++i)
{for (j=0;j<n;++j){
scanf ("%d",&ma[i][j]);mb[i][j] = ma[i][j];
}}printf ("The given matrix is \n");for (i=0;i<m;++i){
for (j=0;j<n;++j){
printf (" %d",ma[i][j]);}printf ("\n");
}
printf ("After arranging rows in ascending order\n");for (i=0;i<m;++i){
for (j=0;j<n;++j){
for (k=(j+1);k<n;++k){
if (ma[i][j] > ma[i][k]){
a = ma[i][j];ma[i][j] = ma[i][k];ma[i][k] = a;
}}
}} /* End of outer for loop*/
for (i=0;i<m;++i){
for (j=0;j<n;++j){
printf (" %d",ma[i][j]);}printf ("\n");
}
printf ("After arranging the columns in descending order \n");for (j=0;j<n;++j){
for (i=0;i<m;++i){
for (k=i+1;k<m;++k){
if (mb[i][j] < mb[k][j]){
a = mb[i][j];mb[i][j] = mb[k][j];mb[k][j] = a;
}}
}} /* End of outer for loop*/
for (i=0;i<m;++i){
for (j=0;j<n;++j){
printf (" %d",mb[i][j]);}printf ("\n");
}
}
Enter the order of the matrix2 2Enter co-efficients of the matrix3 15 2The given matrix is 3 1 5 2After arranging rows in ascending order 1 3 2 5After arranging the columns in descending order 5 2 3 1----------------------------------------------------------------------------------------------------------------------------------------------------25.Write a C program to accept a set of names and sort them in an alphabetical order, Use structures to store the names
#include<stdio.h>
#include<conio.h>#include<string.h>
struct person{
char name[10];int rno;
};typedef struct person NAME;NAME stud[10], temp[10];
void main(){
int no,i;
void sort(int N); /* Function declaration */
clrscr(); fflush(stdin);
printf("Enter the number of students in the list\n"); scanf("%d",&no);
for(i = 0; i < no; i++) {
printf("\nEnter the name of person %d : ", i+1); fflush(stdin); gets(stud[i].name);
printf("Enter the roll number of %d : ", i+1); scanf("%d",&stud[i].rno); temp[i] = stud[i];
}
printf("\n*****************************\n"); printf (" Names before sorting \n"); /* Print the list of names before sorting */ for(i=0;i<no;i++) {
*****************************---------------------------------------------------------------------------------------------------------------------------------------26.Write a C Program to convert the lower case letters to upper case and vice-versa
Resultant sentence is :Good Morning-----------------------------------------------------------------------------------------------------------------------------27.This program accepts an array of N elements and a key.Then it searches for the desired element. If the search is successful, it displays "SUCCESSFUL SEARCH". * Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed.
#include <stdio.h>void main(){
int table[20];int i, low, mid, high, key, size;
printf("Enter the size of an array\n");scanf("%d", &size);
printf("Enter the array elements\n");for(i = 0; i < size; i++){
scanf("%d", &table[i]);}
printf("Enter the key\n");scanf("%d", &key);
/* search begins */
low = 0;high = (size - 1);
while(low <= high){
mid = (low + high)/2;if(key == table[mid]){
printf("SUCCESSFUL SEARCH\n");return;
}if(key < table[mid])
high = mid - 1;else
low = mid + 1;}
printf("UNSUCCESSFUL SEARCH\n");} /* End of main() */
OutputOutputEnter the size of an array5Enter the array elements1236457899Enter the key45SUCCESSFUL SEARCH-----------------------------------------------------------------------------------------------------------------------------------------------------------------