Some simple C-language algorithms

Source: Internet
Author: User

1.

Enter a number to print the following graphic

Input 5

Print:

*

**

***

****

*****

#include <stdio.h>

int main (int argc, const char * argv[]) {

int num,i,j;

scanf ("%d", &num);

for (i=1;i<=num;i++)

{

for (j=1;j<i+1;j++)

{

printf ("*");

}

printf ("\ n");

}

return 0;

}

2.

Enter a number to print the following graphic

Input 5

Print:

*

**

***

****

*****

#include <stdio.h>

int main (int argc, const char * argv[]) {

int num,i,j,k;

scanf ("%d", &num);

for (i=1;i<=num;i++)

{

for (j=1;j<=num-i;j++)

{

printf ("");

}

for (k=1;k<i+1;k++)

{

printf ("*");

}

printf ("\ n");

}

return 0;

}

3.

Enter a number to print the following graphic

Input 5

Print:

*

***

*****

*******

*********

#include <stdio.h>

int main (int argc, const char * argv[]) {

int num,i,j,k,l;

scanf ("%d", &num);

for (i=1;i<=num;i++)

{

for (j=1;j<=num-i;j++)

{

printf ("");

}

for (k=1;k<i*2-1+1;k++)

{

printf ("*");

}

for (l=1;l<=num-i;l++)

{

printf ("");

}

printf ("\ n");

}

return 0;

}

4.

Enter a number to print the following graphic

Input 5

Print

abbbb

aabbb

Aaabb

Aaaab

AAAAA

#include <stdio.h>

int main (int argc, const char * argv[]) {

int num,i,j,k,l;

scanf ("%d", &num);

for (i=1;i<=num;i++)

{

for (j=1;j<=num-i;j++)

{

printf ("");

}

for (k=1;k<i+1;k++)

{

printf ("A");

}

for (l=1;l<=num-i;l++)

{

printf ("B");

}

printf ("\ n");

}

return 0;

}

5.

Enter a number to determine whether it is prime (prime) (* * *)

#include <stdio.h>

int main (int argc, const char * argv[]) {

int nu,i;

scanf ("%d", &nu);

for (i=2; i<nu; i++)

{

if (nu%i = = 0)

{

Break It's not a prime number to be able to do it.

}

}

if (i = = nu)//If equal to Nu proves that no factor is encountered

{

printf ("is prime number");

}

Else

{

printf ("Not prime number");

}

return 0;

}

6.

Enter two numbers for two-digit greatest common divisor (* * * * *)

The largest common factor, also known as greatest common divisor, is the largest of two or more integers.

#include <stdio.h>

int main (int argc, const char * argv[]) {

int i,numa,numb,mix;

scanf ("%d,%d", &numa,&numb);

mix= (Numa<numb) numa:numb;

for (I=mix; i>=1; i--)

{

if (numa%i = = 0 && numb%i = = 0)

{

Break

}

}

printf ("%d", I);//Get I result is greatest common divisor

return 0;

}

7.

Enter two numbers for two-digit least common multiple (* * * *)

#include <stdio.h>

int main (int argc, const char * argv[]) {

int Numa,numb,max;

scanf ("%d,%d", &numa,&numb);

max= (Numa<numb) numa:numb;

while (1)//Find out the minimum number of just divisible two numbers is least common multiple

{

if (Max%numa = = 0 && Max%numb = = 0)

{

Break

}

max++;

}

printf ("%d", max);

return 0;

}

8.

Enter a number to decompose factorization (* * * * *)

Decomposition quality factor: the decomposition of a composite into a number of prime numbers product.

#include <stdio.h>

int main (int argc, const char * argv[]) {

int nu;

scanf ("%d", &nu);

for (int i=2; i<=nu; i++) {

if (nu%i = = 0) { //use to find the smallest factor (i)

printf ("%d", I);

Nu/=i; //Get

i--;

}

}

return 0;

}

9.

Enter two number n,a, if n==3, a = = 2;

Outputs a value of 2 + 22 + 222. (Do not output the formula) (* * *)

M=5,n=a

3+33+333+3333+33333

10*3+3

10*33+3

Ret=10*ret+a

Sum=sum+ret

*/

#include <stdio.h>

int main (int argc, const char * argv[]) {

Insert code here ...

int m,a;

scanf ("%d%d", &m,&a);

int sum=0;

int ret=0;

for (int i=1; i<=m; i++)

{

Ret=10*ret+a; RET is the current value, such as 3,33,333,3333

Sum=sum+ret;

}

printf ("%d\n", sum);

return 0;

}

10.

In five digits, the symmetric number is called a palindrome number, and all the palindrome numbers are found.

such as 12321 (* * *)

#include <stdio.h>

int main (int argc, const char * argv[]) {

Insert code here ...

for (int i=10000; i<=99999; i++)

{

int bita,bitb,bitc,bitd;

12345% 10=5

bita=i%10;

12345/10=1234 1234%10=4

bitb=i/10%10;

12345/1000=123 12%10=2

bitc=i/1000%10;

12345/10000=1 1%10=1

bitd=i/10000%10;

if (BITA==BITD && BITB==BITC)

{

printf ("%d", I);

}

}

return 0;

}

11.

Enter any majority, the last number is 0, and the maximum number of these numbers is output. (**)

Thought: Enter a number at a time to compare, save the current maximum number each time.

#include <stdio.h>

int main (int argc, const char * argv[]) {

Insert code here ...

int Nu,max;

scanf ("%d", &nu);

Max=nu; Tentative first number is maximum, very good thought

while (1)

{

scanf ("%d", &nu);

if (Nu = = 0)

{

Break

}

if (Max < NU)

{

Max=nu;

}

}

printf ("%d", max);

return 0;

}

12.

Enter a number to find the factorial of the number. (*)

#include <stdio.h>

int main (int argc, const char * argv[]) {

int num1,i,sum=1;

scanf ("%d", &num1);

for (i=1;i<=num1;i++)

{

Sum*=i;

}

printf ("%d\n", sum);

return 0;

}

13.

Enter an integer (int range) number and output 10 in reverse order.

#include <stdio.h>

int main (int argc, const char * argv[]) {

int num1,i=0,shu[64],j;

scanf ("%d", &num1);

Take out every single one of these numbers and just put them in the right order.

while (1)

{

shu[i]=num1%10;

NUM1=NUM1/10;

i++;

if (NUM1 = = 0)

{

Break

}

}

Print out every bit of the number that's been put in order.

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

{

printf ("%d", shu[j]);

}

printf ("\ n");

return 0;

}

Some simple C-language algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.