i have difficulty to write algorithm to following programs in c language any one can help me ?? PROGRAM TO FIND SUM OF INDIVIDUAL DIGITS OF A NUMBER PHP: #include <stdio.h> #include <conio.h> void main() { int a,n,final,sum=0; clrscr(); printf("SUM OF DIGITS OF A NUMBER\n"); printf("-------------------------\n\n"); printf("Enter the number : "); scanf("%d",&a); final=a; do { n=a%10; sum=sum+n; a=a/10; } while(a!=0); printf("\nThe sum of digits of the number %d is %d",final,sum); printf("\n\nPRESS ANY KEY TO EXIT..."); getch(); } PROGRAM TO FIND SUM AND AVERAGE OF GIVEN NUMBERS PHP: #include <stdio.h> char input[1000]; int num_count; float total, average, temp; int main() { printf("FIND SUM & AVERAGE OF GIVEN NUMBERS\n"); printf("-----------------------------------\n\n\n"); printf("Enter first number (00 to quit): "); fgets(input, sizeof(input), stdin); sscanf(input, "%f", &temp); num_count = 0; total = 0.0; average = 0.0; while ( temp != 00.00 ) { ++num_count; total += temp; temp = 0; printf("Enter next number (00 to quit): "); fgets(input, sizeof(input), stdin); sscanf(input, "%f", &temp); } average = ( total / num_count ); printf("\nYou have entered %d numbers. \n\nThe SUM of those numbers is %f. \nThe AVERAGE of those numbers is %f.\n", num_count,total, average); printf("\n\n PRESS ANY KEY TO EXIT..."); getch(); return 0; } third program witch FIND LCM AND GCD OF TWO INTEGERS PHP: #include <stdio.h> int gcd(int m,int n) { while( m!= n) { if( m > n) m= m - n; else n= n - m; } return (m); } int lcm(int m,int n) { return( m * n / gcd (m , n)); } main() { int m,n; //int lcm(int,int); //int gcd(int,int); printf("\n LCM & GCD FINDER \n"); printf(" -----------------\n\n"); printf("\nEnter the first number : "); scanf("%d", &m); printf(" \nEnter the second number : "); scanf("%d", &n); printf("\n\nGCD of %d and %d is %d\n",m,n, gcd(m,n)); printf("LCM of %d and %d is %d\n",m,n, lcm(m,n)); printf("\n\nPRESS ANY KEY TO EXIT..."); getch(); } PROGRAM TO FIND THE REVERSE OF A NUMBER PHP: #include<stdio.h> #include<conio.h> void main() { int x,y,z; printf("REVERSING A NUMBER \n"); printf("------------------\n\n"); printf("Enter the number to be reversed : "); scanf("%d",&x); if(x<0) { x=x*(-1); } printf("\n\nThe reversed number is: "); while(x>10) { y=x/10; z=x%10; x=y; printf("%d",z); } printf("%d",x); printf("\n\nPRESS ANY KEY TO EXIT..."); getch(); } PROGRAM TO FIND SUM OF ROW< COLUMN ELEMENTS AND ALL ELEMENTS OF A 3x3 MATRIX PHP: #include<stdio.h> void main() { int matrix[3][3]; int i,j,x,y,total; int sum=0; printf("MATRIX OPERATIONS \n"); printf("----------------- \n\n"); printf("Enter the 3x3 Matrix elements: \n"); for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { scanf("%d", &matrix[i][j]); } } clrscr(); printf("The matrix you entered is : \n"); for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { printf("%d ", matrix[i][j]); } printf("\n\n"); } for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { sum = sum + matrix[i][j]; } printf("Sum of Row %d is : %d\n", i,sum); sum=0; } printf("\n\n"); for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { sum = sum + matrix[j][i]; } printf("Sum of Column %d is : %d\n", i,sum); sum=0; } printf("\n\n"); for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { total = total + matrix[i][j]; } } printf("Sum of all elements of the matrix is : %d\n", total); printf("\n\nPRESS ANY KEY TO EXIT..."); getch(); exit(0); }
Well, let's start with the first one. What does it do, and how does it do it? Can you explain in English how it works? From that we can then go on to write the algorithm.
this is explanation for each program 1. To find the sum of individual digits of a given integer number . 2. To find the sum and average of a given array of integer numbers . 3. To find the GCD and LCD of given two integer numbers . 4. To find the reverse of a given integer number . 5. To find the sum of row elements , column elements and all elements of given 3x3 matrix of non-zero integers . . also i attached word file contain the program i want the algorithm for each one
writing algorithm isn't that much difficult. for e.g for the first program step1: initialize the variables step2: modulo divide the number by 10 so as to get the individual digits. step3: save the number in a variable. step4: divide the number by 10. ...... Like this u can move on and since it is in a loop, U specify that "follow these steps until u get a single digit" r u can even use goto <step number>. This may not be the actual or proper algorithm for ur program. I just wrote it for an example.
I'd say the algorithm is at a higher level than that, hence the suggestion to start with an English description of what it does. Note by "an English description of what it does" I don't mean just rehash the title. "To find the sum of individual digits of a given integer number" is just a title rehash, but the algorithm is how it does that.
hnaya, an observation. You opened with "i have difficulty to write algorithm to following programs in c language ". I asked: "What does it do, and how does it do it?" and in answer you said "i want the algorithm for each one" Yes, I know you want the algorithm for each one. No, I'm not going to write it for you. (Someone else might though.) But I will help you write it, if you're willing to learn. So please answer my first question: "What does it do, and how does it do it?" (You may need to mention if you haven't got a clue how it works though; if you don't know that, you're definitely not going to be able to write the algorithm.) Lots of people post homework assignments expecting us (a) to write it for them and (b) not to be smart enough to realise that's what we're doing and (c) trustworthy enough that if we do realise, we won't write something for you that is plausible but significantly wrong. If that's what you're doing, you're not the first, you won't be the last, and it's very easy to spot (the key giveaways are the lack of willingness to learn and a "just give me the fscking answer" attitude).
The best way to learn algorithm is I think, to work it out in your khata.... At first, I also emphasised on writing the entire program...but what is more essential is to understand writing the algorithm....the other parts will come automatically... Just as xpi0t0s said...what will you do if you are not provided with a pc, just a notebook, a pen and you have been asked to draw a flow-chart of the key algorithm. Note that in the first program, the key algorithm is to distinguish the digits of a number....now how many ways can you do that.....draw all the possible flow-charts...and always stay greedy of drawing one more...then I think it will be perfect. Similarly, try to find out what the other key algorithms are in the remaining programs.