English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

#include

int main()
{
int i;
int count = 0;

for(i = 1; i <= 10; i = i + 1)
{
if(i > 3)
count = count + 1;
}

printf("%d numbers were greater than 3\n", count);

return 0;
}

2007-11-07 20:18:11 · 3 answers · asked by hisluv 1 in Computers & Internet Programming & Design

3 answers

This is a simple c program.

it gives the answer as 7.
#include for 'printf'

As printf is outside the for loop it will not print all the number greater than 3 to 10. It will add count to the previous value of count which is greater or increments by 1 for all the i values greater than 3 and lesser than equal to 10.

initially i =1 < 3 count=0 // doesnt satisfy if condition
i = 4 count =1
i= 5 count =2
till i =10 count =7
then i= 11 comes out of for loop and prints count i.e 7.

2007-11-07 21:05:00 · answer #1 · answered by nandavkumar 3 · 0 0

basically it's gonna print all numbers up to 10 that are greater than 3

int main() is the function
int i is the first variable
int count = 0 is the second variable and that intilizes it at 0

for(i = 1; i <=10; i = i +1) is the loop where as long as i is less than or equal to 10 it will increase the variable i by 1

count = count +1 in that if statment increases the count variable by 1 if the variable i is greater than 3 in the if statement and won't print anything, otherwise it prints the numbers greater than 3 with the printf statement..

oh and the include header file statement at the beginning just includes the code used for printf I believe. may also want to includ just incase =P

if you have any more questions please feel free to either e-mail me or post most onto this question


- my bad, I thought the print statement was in the loop

2007-11-08 04:25:00 · answer #2 · answered by []D[][]\/[][]D 2 · 0 0

//Bring the standard i/o library into the program
#include

//Start the program. Group all commands together
int main()
{
//Declare an integer variable named i
int i;
//Declare an integer variable named count and set its value
//to zero
int count = 0;

//for loop start at 1, increment i until i is greater than 10
for(i = 1; i <= 10; i = i + 1)
{
//test the value of i.
if(i > 3)
//i is greater than 3 so add 1 to count
count = count + 1;
//End for loop
}

//Print to standard output the value of count which will be 7
printf("%d numbers were greater than 3\n", count);

//Return a value of 0 to the system to end the program
return 0;
//End of program section
}

2007-11-08 04:32:16 · answer #3 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers