C language programming learning: 10 basic algorithms

Algorithm is the soul of a program and software. As an excellent programmer, only if he has a comprehensive grasp of some basic algorithms can he be handy in the process of designing programs and writing codes.

1. Calculate the Fibonacci sequence

The Fibonacci sequence is also known as the Fibonacci sequence and the golden section sequence. It refers to such a sequence: 1, 1, 2, 3, 5, 8, 13, 21.

2. Palindrome check

3. Prime number check

1 is neither a prime number nor a composite number.

4. Print pyramids and triangles

 Use * to create a triangle

5. Simple addition, subtraction, multiplication and division calculator

Source code:

# include <stdio.h>

int main()

{

char o;

float num1,num2;

printf("Enter operator either + or - or * or divide : ");

scanf("%c",&o);

printf("Enter two operands: ");

scanf("%f%f",&num1,&num2);

switch(o) {

case '+':

printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);

break;

case '-':

printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);

break;

case '*':

printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);

break;

case '/':

printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);

break;

default:

/* If operator is other than +, -, * or /, error message is shown */

printf("Error! operator is not correct");

break;

}

return 0;

}

6. Check whether a number can be expressed as the sum of two prime numbers

7. Recursively reverse the string

8. Realize the mutual conversion between binary and decimal

Source code:

#include <stdio.h>

#include <math.h>

int binary_decimal(int n);

int decimal_binary(int n);

int main()

{

int n;

char c;

printf("Instructions:n");

printf("1. Enter alphabet 'd' to convert binary to decimal.n");

printf("2. Enter alphabet 'b' to convert decimal to binary.n");

scanf("%c",&c);

if (c =='d' || c == 'D')

{

printf("Enter a binary number: ");

scanf("%d", &n);

printf("%d in binary = %d in decimal", n, binary_decimal(n));

}

if (c =='b' || c == 'B')

{

printf("Enter a decimal number: ");

scanf("%d", &n);

printf("%d in decimal = %d in binary", n, decimal_binary(n));

}

return 0;

}

int decimal_binary(int n) /* Function to convert decimal to binary.*/

{

int rem, i=1, binary=0;

while (n!=0)

{

rem=n%2;

n/=2;

binary+=rem*i;

i*=10;

}

return binary;

}

int binary_decimal(int n) /* Function to convert binary to decimal.*/

{

int decimal=0, i=0, rem;

while (n!=0)

{

rem = n%10;

n/=10;

decimal += rem*pow(2,i);

++i;

}

return decimal;

}

9. Use multidimensional arrays to add two matrices

10. Matrix transpose


In addition, if you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster! I may be able to help you here~

UP has uploaded some video tutorials on learning C/C++ programming on the homepage. Those who are interested or are learning must go and take a look! It will be helpful to you~

Sharing (source code, actual project video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning:

Programming learning:

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/114934390