C language programming Chapter 4 example source code

Source: Internet
Author: User

[Example 4-1]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int denominator, flag;
Double item, PI;
Flag = 1;
Denominator = 1;
Item = 1.0;
Pi = 0;
While (FABS (item) >=0.0001)
{
Item = flag * 1.0/denominator;
Pi = PI item;
Flag =-flag;
Denominator = Denominator 2;
}
Pi = pI * 4;
Printf ("Pi = %. 4f \ n", Pi );
Return 0;
}
[Example 4-2]
# Include <stdio. h>
Int main (void)
{
Int num;
Double grade, total;
Num = 0;
Total = 0;
Printf ("enter grade :");
Scanf ("% lf", & grade );
While (grade> = 0)
{
Total = total grade;
Num;
Scanf ("% lf", & grade );
}
If (num! = 0)
{
Printf ("grade average is %. 2f \ n", total/num );
}
Else
{
Printf ("grade average is 0 \ n ");
}
Return 0;
}
[Example 4-3]
# Include <stdio. h>
Int main (void)
{
Int count, number;
Count = 0;
Printf ("enter a number :");
Scanf ("% d", & number );
If (number <0)
{
Number =-number;
}
Do
{
Number = Number/10;
Count;
}
While (number! = 0 );
Printf ("It contains % d digits. \ n", count );
Return 0;
}
[Example 4-4]
# Include <stdio. h>
Int main (void)
{
Int I, m;
Printf ("enter a number :");
Scanf ("% d", & M );
For (I = 2; I <= m/2; I)
{
If (M % I = 0)
{
Break;
}
}
If (I> M/2)
{
Printf ("% d is a prime number! \ N ", M );
}
Else
{
Printf ("No! \ N ");
}
Return 0;
}
[Example 4-5]
# Include <stdio. h>
Double fact (int n );
Int main (void)
{
Int I;
Double sum;
Sum = 0;
For (I = 1; I <= 100; I)
{
Sum = sum fact (I );
}
Printf ("1! 2! 3! .. 100! = % E \ n ", sum );
Return 0;
}
Double fact (int n)
{
Int I;
Double result;
Result = 1;
For (I = 1; I <= N; I)
{
Result = Result * I;
}
Return result;
}
[Example 4-6]
# Include <stdio. h>
Int main (void)
{
Int I, J;
Double item, sum;
Sum = 0;
For (I = 1; I <= 100; I)
{
Item = 1;
For (j = 1; j <= I; j)
{
Item = item * J;
}
Sum = sum item;
}
Printf ("1! 2! 3! .. 100! = % E \ n ", sum );
Return 0;
}
[Example 4-7-1]
# Include <stdio. h>
Int main (void)
{
Int I, Mark, Max, N;
Printf ("Enter N :");
Scanf ("% d", & N );
Printf ("Enter % d marks:", N );
Scanf ("% d", & mark );
Max = mark;
For (I = 1; I <n; I)
{
Scanf ("% d", & mark );
If (max <mark)
{
Max = mark;
}
}
Printf ("max = % d \ n", max );
Return 0;
}
[Example 4-7-2]
# Include <stdio. h>
Int main (void)
{
Int mark, Max;
Printf ("Enter marks :");
Scanf ("% d", & mark );
Max = mark;
While (Mark> = 0)
{
If (max <mark)
{
Max = mark;
}
Scanf ("% d", & mark );
}
Printf ("max = % d \ n", max );
Return 0;
}
[Example 4-8]
# Include <stdio. h>
Int main (void)
{
Int X;
Printf ("Enter X :");
Scanf ("% d", & X );
While (X! = 0)
{
Printf ("% d", X );
X = X/10;
}
Printf ("\ n ");
Return 0;
}
[Example 4-9]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int count, I, m, n;
Count = 0;
For (m = 2; m <= 100; m)
{
N = SQRT (m );
For (I = 2; I <= N; I)
{
If (M % I = 0)
{
Break;
}
}
If (I> N)
{
Printf ("% 6.d", M );
Count;
If (COUNT = 0)
{
Printf ("\ n ");
}
}
}
Printf ("\ n ");
Return 0;
}
[Example 4-10]
# Include <stdio. h>
Int main (void)
{
Int I, x1, x2, X;
X1 = 1;
X2 = 1;
Printf ("mm", x1, x2 );
For (I = 1; I <= 8; I)
{
X = x1 X2;
Printf ("M", X );
X1 = x2;
X2 = X;
}
Printf ("\ n ");
Return 0;
}
[Example 4-11-1]
# Include <stdio. h>
Int main (void)
{
Int children, women, men;
For (Men = 0; men <= 45; men)
{
For (Women = 0; Women <= 45; Women)
{
For (Children = 0; children <= 45; Children)
{
If (Men Women children = 45 & men * 3 women * 2 children * 0.5 = 45)
{
Printf ("Men = % d, women = % d, children = % d \ n", men, women, children );
}
}
}
}
Return 0;
}
[Example 4-11-2]
# Include <stdio. h>
Int main (void)
{
Int men, women, children;
For (Men = 0; men <= 15; men)
{
For (Women = 0; Women <= 22; Women)
{
Children = 45-men-women;
If (men * 3 women * 2 children * 0.5 = 45)
{
Printf ("Men = % d, women = % d, children = % d \ n", men, women, children );
}
}
}
Return 0;
}

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.