Learning Log No. 6

1. By programming, count the number of 9 in 1~n
#include <stdio.h>
 
void fun(int n)
{
	int i;
	int temp;
	int count = 0;
	int b;
 
	for(i = 1;i <= n;i++)
	{
		temp = i;
		while(temp != 0)
		{
			b=temp % 10;
			temp=temp / 10;
			if(b == 9)
			{
				count++;
			}
		}
	}
 
	printf("The result is: %d\n",count);
}
 
int main()
{
	int n;
 
	printf("Enter a number:\n");
	scanf("%d",&n);
 
	fun(n);
 
    return 0;
}

2. There are n people in a circle, numbering in sequence, starting with the first report (from 1 to 3). Everyone who reports to 3 quits the circle and asks the last one who left behind the original number.

#include <stdio.h>

int main()
{
	int n;
	printf("Enter number of people:\n");
	scanf("%d",&n);
	int a[1000];
	int i,temp;
	int count = 0;
  	temp = n; 
	for(i = 0;i < n;i++)
	{
		a[i] = i+1;
	}
	i = 0;
	while (n>1)
	{
		if(a[i] != 0)
		{
			count++;
		}
		if(count == 3)
		{
			a[i] = 0;
			count = 0;
			n--;
		}
		i++;
		if(i == temp)
		{
			i = 0;
		}
	}
	for(i = 0;i < temp;i++)
	{
		if(a[i] != 0)
		{
			printf("The rest are:%d Number\n",a[i]);
		}
	}
	return 0;
}

3. Enter 5 numbers (negative, decimal) to arrange them in order from smallest to largest

#include <stdio.h>

float  paixu(float b[5]);
int main()
{
	float a[5];
	int i;

	printf("Please enter 5 numbers:\n");

	for(i = 0;i < 5;i++)
	scanf("%f",&a[i]);
	paixu(a);

	printf("After sorting, the result is:\n");
	for(i = 0;i < 5;i++)
	printf("%2.1f\t",a[i]);
	printf("\n");

    return 0;
}

float  paixu(float b[])
{
	int i, j;
	float t = 0.0;

	for(i = 0;i < 5;i++)
	{
		for(j = i + 1; j < 5;j++)
		{
			if(b[j] < b[i])
			{
				t = b[i];
				b[i] = b[j];
				b[j] = t;
			}
		}
	}
}

4. Find prime numbers within 100 and print them all out

#include <stdio.h>
 
int main()
{
    int i,j;
    int temp;
 
    for(i = 2;i <=100;i++)
    {
        int temp = 1;
        for(j = 2; j < i;j++)
		{
	    	if(i % j == 0)
	  		{
	        	temp = 0;
	       		break;
	    	}
		}
		if(temp == 1)
		{
	    	printf("%d ",i);
		}
    }
    return 0;
}

5. If a number is exactly equal to the sum of its factors, it is called a "complete number". For example: 6=1+2+3. Program to find the complete number within 1000

#include<stdio.h>
 
int main()
{
	printf("1000 Number of completions within: \n");
	int i = 0, j = 0, sum = 0;
	for(i = 1; i <= 1000; ++i)
	{
		for(j = 1; j < i; ++j)
		{
			if(0 == i % j)
			{
				sum = sum + j;
			}
		}
	
		if(sum == i)
		{
			printf("%d ",i);
			
		}
		sum =0;
	}
	printf("\n");
	return 0;
}

Tags: Programming REST

Posted on Fri, 14 Feb 2020 08:34:30 -0800 by jtapoling