0% found this document useful (0 votes)
4 views8 pages

C Programming Examples and Errors

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

C Programming Examples and Errors

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Some examples

in c programming
Part 1
Example No.1
•#include <stdio.h>
•int main( void )
•{
• int c; /* define variable */
• /* demonstrate postincrement */
• c = 5;
• printf( "%d\n", c );
• printf( "%d\n", c++ );
• printf( "%d\n\n", c );
• c = 5;
• printf( "%d\n", c );
• printf( "%d\n", ++c );
• printf( "%d\n", c );
• return 0; /* indicate program ended successfully */
•} /* end function main */
Example No.2
Example No.3
What is wrong with the following while repetition statement (assume z
has value 100),
which is supposed to calculate the sum of the integers from 100 down
to 1:

while ( z >= 0 )
Example
sum += z;
No.4
- Determine the values of variables product and x after the following calculation is
performed.
Assume that product and x each have the value 5 when the statement begins
executing.
Example No.5
product *= x++;
- Write a C program to calculate x raised to the y power. The program should
have a while repetition control statement.
#include <stdio.h>
int main( void )
{
int x, y, i, power; /* define variables */
i = 1; /* initialize i */
power = 1; /* initialize power */
scanf( "%d", &x ); /* read value for x from user */
scanf( "%d", &y ); /* read value for y from user */
while ( i <= y )
{ /* loop while i is less than or equal to y */
power *= x; /* multiply power by x */
++i; /* increment i */
} /* end while */
printf( "%d", power ); /* display power */
return 0;
} /* end main function */
Example No.6
Identify and correct the errors in each of the following. [Note: There may be
more than one
error in each piece of code.]
Example No.7
What does the following program print?

#include <stdio.h>

int main( void )


{
int x = 1, total = 0, y;
while ( x <= 10 )
{
y = x * x;
printf( "%d\n", y );
total += y;
++x;
} /* end while *
return 0;
} /* end main */
Example No.7
What does the following program print?

#include <stdio.h>

int main( void )


{
int x = 1, total = 0, y;
while ( x <= 10 )
{
y = x * x;
printf( "%d\n", y );
total += y;
++x;
} /* end while *
return 0;
} /* end main */

You might also like