Rajesh Kulkarni
Loops
Used to repeat the same instruction(s) over and over again.
Loops
C provides some flexible ways of deciding how many times to loop, or when to exit a loop.
for, while, do-while loops.
Entry controlled loops
Exit controlled loops
while loops
while (condition)
{
statement(s);
}
Compute 10!
What is 1*2*3*4*5*6*7*8*9*10 (ten factorial) ?
x = 1*2*3*4*5*6*7*8*9*10;
Tracing the loop
# i x i<9?
A ? 1
B 2 1
C 2 1 T
D 2 2
E 3 2
C 3 2 T
D 3 6
E 4 6
C 4 6 T
D 4 24
E 5 24
... ... ...
E 10 362880
C 10 362880 F
G (print 362880)
Double your money
Suppose your Rs 10000 is earning interest at 1% per month. How many months until you double your money ?
Maximum of inputs
printf (“Enter positive numbers to max, end with -1.0\n”);
max = 0.0;
count = 0;
scanf(“%f”, &next);
while (next != 1.0) {
if (next > max)
max = next;
count++;
scanf(“%f”, &next);
}
printf (“The maximum number is %f\n”, max) ;
Example - factorial
#include
int main()
{
int i, n, fact = 1;
printf("Enter a number\n");
scanf("%d", &n);
i=1; /* this is the counter */
while (i<=n)
{
fact = fact*i;
i++; /* equivalent to i = i+1 */
}
printf("the factorial is %d\n", fact);
return 0;
}
Example – fibonacci series
fibonacci.c
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
Fibonacci – step by step
fib1 = 0;
fib2 = 1;
printf("%d ", fib1);
while (fib2 < lim)
{
printf("%d ", fib2);
fib_next = fib1 + fib2;
fib1 = fib2;
fib2 = fib_next;
}
printf("\n");
getchar
getchar() gets a single character from the user.
Requires including stdio.h
Returns a non-positive number on failure.
Similar to scanf.
Putchar
putchar(char) prints out the character inside the brackets.
Requires including stdio.h
Similar to printf.
Example – lower-case to upper case.
low2up.c
Low2up – step by step
#include
int main()
{
char c;
char upper_c;
printf(“Enter a string: ");
c = getchar();
Low2up – step by step
#include
int main()
{
char c;
char upper_c;
printf(“Enter a string: ");
c = getchar();
Low2up – step by step
#include
int main()
{
char c;
char upper_c;
printf (“Enter a string: ");
c = getchar();
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Low2up – step by step
while (c != '\n' && c >= 0)
{
if (c >= 'a' && c <= 'z')
upper_c = c - 'a' + 'A';
else
upper_c = c;
/* Print the converted character.*/
putchar(upper_c);
/* Get the next character */
c = getchar();
}
putchar('\n');
Exercise
Input:
Two integers – A and B
Output:
How many times A contains B
This is the result of the integer division A/B
Note:
Do not use the division operator!
Solution
#include
int main()
{
int a, b, res;
printf("Please enter two numbers.\n");
scanf("%d%d", &a, &b);
res = 0;
while ( (res+1) * b <= a)
res = res + 1;
printf("%d / %d = %d", a, b, res);
return 0;
}
Printing a 2-D Figure
How would you print the following diagram?
* * * * *
* * * * *
* * * * *
Nested Loops
#define ROWS 3
#define COLS 5
...
row=1;
while (row <= ROWS) {
/* print a row of 5 *’s */
...
row++;
}
do while loops
do {
statement(s)
} while (expression);
Similar to while loops
Except the condition is evaluated after the loop body
The loop body is always executed at least once, even if the expression is never true
Example – waiting for legal input
#include
int main()
{
int i;
printf("Please enter a positive number.\n");
do {
scanf("%d", &i);
if (i <= 0)
printf("Try again.\n");
} while (i<=0);
/* The program continues.... */
return 0;
}
do-while statement
do statement while (expression)
int main () {
char echo ;
do {
scanf (“%c”, &echo);
printf (“%c”,echo);
} while (echo != ‘\n’) ;
}
for loops
for loops are controlled by a counter variable.
for (c = begin; c <= end; c += inc)
{
loop body
}
for loops
Absent of second expression is treated as true
for(;;) is infinite.
for loop
for ( expression1; expression2; expression3) statement
expression1 (init) : initialize parameters
expression2 (test): test condition, loop continues if satisfied
expression3 (reinit): used to alter the value of the parameters after each iteration
statement (body): body of the loop
for ( expression1; expression2; expression3) statement
expression1;
while (expression2) {
statement
expression3;
}
Counting in for loops
for (count=1; count <= n; count=count+1) {
printf(“*”);
}
More about for
Initialization and increment
Can be comma-separated lists
Example:
for (int i = 0, j = 0; j + i <= 10; j++, i++)
printf( "%d\n", j + i );
The for Structure: Notes and Observations
Arithmetic expressions
Initialization, loop-continuation, and increment can contain arithmetic expressions. If x equals 2 and y equals 10
for ( j = x; j <= 4 * x * y; j += y / x )
is equivalent to
for ( j = 2; j <= 80; j += 5 )
Notes about the for structure:
"Increment" may be negative (decrement)
If the loop continuation condition is initially false
The body of the for structure is not performed
Control proceeds with the next statement after the for structure
Control variable
Often printed or used inside for body, but not necessary
for loops
Equivalent to while. Any for loop can be converted to while loop and vice versa
Some applications are more natural to for, and others to while.
If we want to perform something for a predefined number of times, better use for.
If we just wait for something to happen (not after a certain number or iterations), better use while.
2-D Figure
* * * * *
* * * * *
* * * * *
Another 2-D Figure
*
* *
* * *
* * * *
* * * * *
Exercise
* * * * *
* * * *
* * *
* *
*
Exercise : solution
* * * * *
* * * *
* * *
* *
*
Examples : for loop
for (i=0; i<10; i++)
printf (“%d “,i);
output:
0 1 2 3 4 5 6 7 8 9
Nested for Loop
main ()
{
int multiplicand, multiplier;
for (multiplicand=0; multiplicand<10; multiplicand++) {
for (multiplier=0; multiplier<10; multiplier++) {
printf(“%d\t”, multiplier*multilicand);
}
printf(“\n”);
}
}
main ()
{
int sum=0;
int input, inner, outer;
printf(“Input an integer : “);
scanf (“%d”, &input) ;
for (outer=1; outer <= input; outer++)
for (inner=0; inner < outer; inner++)
sum += inner;
printf (“The result is %d\n”, sum) ;
}
The factorial example again, this time using for
Factorial with for – step by step
Factorial with for – step by step
Factorial with for – step by step
Factorial with for – step by step
Factorial with for – step by step
Factorial with for – step by step
Factorial with for – step by step
Factorial with for – step by step
Factorial with for – step by step
Example – fahrenheit-celsius conversion table
/* Print a Fahrenheit-to-Celsius conversion table */
#include
int main ( )
{
int fahr;
double celsius;
int lower = 0, upper = 300;
int step = 20;
for (fahr=lower; fahr<=upper; fahr += step)
{
celsius = 5.0*(fahr -32.0)/9.0;
printf("%d\t%g\n", fahr, celsius);
}
return 0;
}
Nested for loop – rectangle example
/* Print a rectangle of *. The height and width are defined by the user */
#include
int main( )
{
int i, j;
int height, width;
printf("Please enter the two box dimensions: \n");
scanf("%d%d", &height, &width);
for (i = 1; i <= height; i++)
{
for (j = 1; j <= width; j++)
printf("*");
printf("\n");
}
}
Exercise
Write a program that prints an upside-down half triangle of *.
The height of the pyramid is the input.
Solution
#include
int main()
{
int i, j, size;
printf(“Please enter a size:\n”);
scanf(“%d”,&size);
for (i = 1; i <= size; i++)
{
for (j = i; j <= size; j++)
printf("*");
printf("\n");
}
return 0;
}
Exercise
Write a program accepts a number from the user, and prints out all of the prime numbers up to that number.
Solution
#include
int main()
{
int i, j, last;
printf("enter a number\n");
scanf("%d", &last);
for (i = 2; i <= last; i++)
{
for (j = 2 ; j < i; j++)
{
if (i % j == 0)
break;
}
if (j == i)
printf("the number %d is prime\n", i);
}
return 0;
}
Exercise
Change the former program, so that is displays only the largest prime number which is smaller than or equal to the user’s input.
Solution 1
#include
int main()
{
int i, j, last;
int found = 0; /* This indicates if we found the largest prime */
printf("enter a number\n");
scanf("%d", &last);
i = last;
while (!found) /* Loop until we find our guy */
{
for (j = 2 ; j < i; j++)
if (i % j == 0)
break;
if (j == i) /* If this is true then i is prime */
found = 1;
else
i--;
}
printf("The largest prime not larger than %d is %d.\n", last, i);
return 0;
}
Solution 2 (with break)
#include
int main()
{
int i, j, last;
printf("enter a number\n");
scanf("%d", &last);
for (i=last; i>1; i--)
{
for (j = 2 ; j < i; j++)
if (i % j == 0)
break;
if (j == i) /* i is prime. We found our guy */
break;
}
printf("The largest prime not larger than %d is %d.\n", last, i);
return 0;
}
Some Loop Pitfalls
while (sum <= NUM) ;
sum = sum+2;
Doubles and floats
What you expect:
0.000000000000000000
0.200000000000000000
0.400000000000000000
.. .. ..
9.000000000000000000
9.200000000000000000
9.400000000000000000
9.600000000000000000
9.800000000000000000
Use ints as loop counters
int i;
double x;
for (i=0; i<50; i=i+1)
{
x = (double)i/5.0;
printf (“%.18f”, x);
}
Iteration Summary
General Pattern :
initialize
test
do stuff
update
go back to re-test, re-do stuff, re-update, ...
while and for are equally general in C
use for when initialize/test/update are simple, especially when counting.
Simple Command Interpreter
Read in “commands” and execute them.
Input - single characters
a - execute command Add by calling Add()
s - execute command Sub by calling Sub()
q - quit
Pseudocode for main loop:
get next command
if a, execute command Add()
if b, execute command Sub()
if q, signal quit
Command Interpreter
Loop Control
repeat until quit signal
use variable “done” to indicate when done
Command Interpreter program
#define FALSE 0
#define TRUE 1
int main (void)
{
char command;
int done = FALSE;
while (!done) {
printf (“Input command:”);
scanf(“%c”,&command);
Exercise a
Write a C program which accepts as input a single integer k, then writes a pattern consisting of a single 1 on the first line, two 2s on the 2nd line, three 3s on the 3rd line, until it writes k occurrences of k on the last line.
For example, if the input is 4, the output should be:
1
2 2
3 3 3
4 4 4 4
Exercise b
Write a C program which accepts as input a single integer k, then generates the following pattern of k lines:
For example, if the input is 5, the output should be:
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
Test if a number is prime
prime = 1;
for (i=2; i
if (num%i == 0)
prime=0;
}
if (prime == 1)
printf (“%d” is a prime number\n”);
Test if a number is prime
prime = 1;
limit = sqrt ((double)num);
for (i=2; i
if (num%i == 0) {
prime=0;
break;
}
}
if (prime == 1)
printf (“%d” is a prime number\n”);
The break and continue
break
Causes immediate exit from a while, for, do/while or switch structure
Program execution continues with the first statement after the structure
Common uses of the break statement
Escape early from a loop
Skip the remainder of a switch structure
The break and continue Statements
continue
Skips the remaining statements in the body of a while, for or do/while structure
Proceeds with the next iteration of the loop
while and do/while
Loop-continuation test is evaluated immediately after the continue statement is executed
for
Increment expression is executed, then the loop-continuation test is evaluated
Break and continue
These two statements are used in loop control
“break” exits the innermost current loop (for, while, do-while) and to exit from a switch
Control will be transferred out of the loop
“continue” starts the next iteration of the loop (for, while, do-while)
used to bypass the remainder of the current pass through a loop
do {
scanf (“%f”, &x);
if (x<0) {
printf(“Error, neg x”);
break;
}
. . .
/*process non-neg x */
} while (x<=100);
do {
scanf (“%f”, &x);
if (x<0) {
printf(“Neg value forx”);
continue;
}
. . .
/*process non-neg x */
} while (x<=100);
Ex: Write a loop that will calculate the sum of an AP series upto n terms
Sum= a + (a+d) +(a+2d) + . . . + (a+ (n-1)d)
Exercise c
Write a C program that takes as input a positive integer n, and prints all prime numbers between 2 and n.
Exercise d
Write a C program that calculates the sum of the first n odd numbers:
1 + 3 + 5 + . . . + 2*n-1
The sine of x can be calculated approximately by summin the first n terms of the infinite series:
sin x = x - x3 /3! + x5 /5! – x7 /7! + . . .
where x is expressed in radians ( radians = 180 degrees).
Write a C program that will read in a value for x and will calculate its sine.
(i) sum the first n terms
(ii)continue adding successive terms till the value of the next term becomes smaller (in magnitude) than 10-5
1. Int i=8, j=15, k=4
What is the output for:
2 * ( ( I % 5 ) * ( 4 + ( j – 3 ) / ( k + 2 ) ) )
2. int i= 7, float f=5.5, char c=’w’
(i>=6) && (c = = ‘w’)
(i>=6) || ( c = = ‘v’)
3. i=7, f= 5.5
!(f>5)
4. int a = float b
5. x=2, y=3,z=4
x * = -2 * ( y + z ) / 3
6. int i=7, float f=8.5
(i+f)%4
7. a=5, b= 3, c=8, d=7
b + c / 2 – (d * 4) % a
8. n1=25, n2=5, n3=6, n4=8
r = ( n1 + n2 ) / (n3 + n4 %4)
9. #include
void main()
{
int x = 5;
long int y=0x7ff;
int a=0x6db7,b=0xa726;
clrscr();
printf("%d\t",x);
printf("%d\t",(x<<2));/*left shift*/
printf("%d\t",(x>>2));/*right shift*/
printf("%d\n",(~x));/*one's complement*/
printf("%x\n",(~y));/*one's complement*/
printf("%x\n",a&b);
printf("%x\n",a|b);
printf("%x\n",a^b);
printf("%x\n",a<<6);
printf("%x",a>>6);
getchar();
}
10.
#include
int main()
{
int a=0;
if(a= 0)
printf("hello");
printf("AATT");
return 0;
}
11.
#include
void main()
{
char a[]="hello";
char b[]="hai";
a=b;
printf("%s,%s",a,b);
}
12. #include
void main()
{
char * p = "hello",ch;
ch = *++p;
clrscr();
printf("%c,%s",ch,p);
getchar();
}
13.#include
#define Stringizer(x) printf(#x)
void main()
{
Stringizer(hello);
getchar();
}
14. #include
#include
int main( )
{
char *ptr1,*ptr2;
ptr1 = "Hello AATT";
ptr2 = "Hai";
ptr1= strcat(ptr1,ptr2);
printf("\n Input string %s",ptr1);
return 1;
}
15. int main( )
{ int x=20,y=35;
x=y++ + x++;
y=++y + ++x;
printf("%d %d",x,y);
return 1;
}
16
/*
* database1.c
*
* Program to maintain a database of CDs for a CD shop.
* This is the Course Project for the Structured Programming Course
* This project is for WASE 2006 batch students .
* by Rajesh Kulkarni, 08-04-2007.
*/
#include
main()
{
char movie[61];
char director[61];
int songs; /* number of songs on the CD */
char type; /* used to read in hit/fail info */
int hit; /* boolean - is the movie a hit? */
float price;
printf("Welcome to the CD database.\n\n");
/*
* First, the movie name.
*/
printf("Please enter the details of the CD...\n");
printf("enter the name of the moviemovie? ");
scanf("%[^\n]", movie);
/*
* Next, the director.
*/
printf("enter the name of the director? ");
fflush(stdin);
scanf("%[^\n]", director);
/*
* Next, the number of songs
*/
printf("how many number of songs? ");
fflush(stdin);
scanf("%d", &songs);
/*
* Next, hit/fail.
*/
for (;;)
{
printf("hit or fail (h for hit, f for fail)? ");
fflush(stdin);
scanf("%c", &type);
if (type == 'h' || type == 'f' || type == 'H' || type == 'F')
break;
printf("Error - only 'h' or 'f' are allowed\n");
}
album = type == 'h'; /* if we get here it must be 'h' or 'f' */
/*
* Next, the price
*/
printf("enter the price of the CD? ");
fflush(stdin);
scanf("%f", &price);
/*
* Output the CD details
*/
printf("\nThe CD details you entered are:\n");
printf("============================\n");
printf("movie: %s\n", movie);
printf("director: %s\n", director);
printf("Number of songs: %d\n", songs);
if (hit)
printf("hit\n");
else
printf("fail\n");
printf("Price: %.2f\n", price);
printf("============================\n");
/*
* A user-friendly exit of the program
*/
printf("\nPress ENTER to exit the program: ");
fflush(stdin);
getchar();
}
17. /*
* database2.c
*
* Program to maintain a database of CDs for a CD shop.
* This is the Course Project for the Structured Programming Course
* This project is for WASE 2006 batch students .
* by Rajesh Kulkarni, 15-04-2007.
*/
#include
main()
{
char movie[100];
char director[100];
int songs[100]; /* number of songs on the CD */
char type; /* used to read in hit/single info */
int hit[100]; /* boolean - is the CD an hit? */
float price[100];
int count = 0; /* how many CDs are being tracked */
int i; /* loop counter */
printf("Welcome to the CD database.\n");
printf("You can store information of a maximum of 100 CDs.\n");
/*
* Loop until they no longer wish to enter any more CDs
*/
for (;;) /* forever loops are convenient for this sort of thing */
{
/*
* Ask them if they want to enter another CD
* Any answer other than y or Y will be treated as a NO
*/
printf("\nHave you any more CDs to enter (y/n)? ");
fflush(stdin);
scanf("%c", &type);
if (type != 'y' && type != 'Y')
break;
printf("\n"); /* for neat output */
/*
* First, the movie
*/
printf("Please enter the details of CD %d...\n\n", count+1);
printf("movie? ");
fflush(stdin);
scanf("%[^\n]", movie[count]);
/*
* Next, the director
*/
printf("director? ");
fflush(stdin);
scanf("%[^\n]", director[count]);
/*
* Next, the number of songs
*/
printf("Number of songs? ");
fflush(stdin);
scanf("%d", &songs[count]);
/*
* Next, hit/single
*/
for (;;)
{
printf("hit or fail (h for hit, f for fail)? ");
fflush(stdin);
scanf("%c", &type);
if (type == 'h' || type == 'f')
break;
printf("Error - only 'h' or 'f' are allowed\n");
}
hit[count] = type == 'h'; /* if we get here it must be 'h' or 'f' */
/*
* Next, the price
*/
printf(" price of the CD? ");
fflush(stdin);
scanf("%f", &price[count]);
count = count + 1;
/*
* Check if we have filled up the array
*/
if (count == 100)
{
printf("You have reached the limits of this program\n\n");
break;
}
}
/*
* Output the CD details
*/
for (i = 0; i < count; i = i + 1)
{
printf("\nThe details of CD %d are:\n", i+1);
printf("============================\n");
printf("movie: %s\n", movie[i]);
printf("director: %s\n", director[i]);
printf("Number of songs: %d\n", songs[i]);
if (hit[i])
printf("hit\n");
else
printf("fail\n");
printf("Price: %.2f\n", price[i]);
printf("============================\n");
if (i < count - 1) /* only do this if there are more CDs to see */
{
/*
* A user-friendly way to progress to the next CD
*/
printf("\nPress ENTER to see the next set of details: ");
fflush(stdin);
getchar();
}
}
/*
* A user-friendly exit of the program
*/
printf("\nPress ENTER to exit the program: ");
fflush(stdin);
getchar();
}
18. #include
main()
{
int i;
for (i=1;i<=10;)
printf("%d\n",i);
}
19. #include
main()
{
if(1)
printf("WASE");
else;
printf("BIET");
}
20. #include
main()
{
int i;
clrscr();
do
printf("%d",i);
while(i<1);
getchar();
}
21. #include
main()
{
int i,j;
clrscr();
for(i=1,j=2;i<=2;i++,j--)
{
printf("%d\t%d\n",i,j);
}
getchar();
}
22.#include
main()
{
int i=1,j=2;
clrscr();
for(;i
printf("%d",j);
}
getchar();
}
23. #include
main()
{
int i=1;
clrscr();
do
printf("%d",++i);
while(i<1);
getchar();
}
24. #include
main()
{
int a,b,c,d,e;
clrscr();
a=5,b=3,c=8,d=7;
e=b+c/2-(d*4)%a;
printf("%d",e);
getchar();
}
25. #include
main()
{
int n1,n2,n3,n4,r;
clrscr();
n1=25,n2=5,n3=6,n4=8;
r= (n1+n2)/(n3+n4%4);
printf("%d",r);
getchar();
}
26#include
main()
{
for(i=1;i<=10;i++)
{
printf("%d\n",i);
}
27. #include
main()
{
int i,n,p;
clrscr();
printf(" enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=i*n;
printf("\n%d\tx\t%d\t=%d\n",n,i,p);
}
fflush(stdin);
getchar();
}
28.#include
main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%d",j);
}
}
getchar();
}
29. #include
main()
{
int i,j;
clrscr();
for( i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getchar();
}
30.#include
main()
{
int i=11,k;
clrscr();
k=i%-2;
printf("%d",k);
getchar();
}
31. #include
main()
{
int j=65;
clrscr();
for(;j<68;)
printf("%c",j++);
getchar();
}
32. #include
main()
{
int a=10;
clrscr();
printf("%f",a);
getchar();
}
33. #include
main()
{
int j=5;
clrscr();
printf("%d\t%d\t%d",j,--j,j++);
getchar();
}
34. #include
main()
{
long n,rev=0;
clrscr();
printf("\nenter a number\n");
scanf("%ld",&n);
while(n!=0)
{
rev=rev*10+n%10;
n=n/10;
}
printf("reverse number is %ld",rev);
fflush(stdin);
getchar();
}
35. #include
main()
{
int n,tn,sum,rem;
clrscr();
printf("enter a number\n");
scanf("%d",&n);
sum=0;
tn=n;
while(tn)
{rem=tn%10;
sum=sum+rem;
tn=tn/10;
}
printf("\n%d\t%d",n,sum);
fflush(stdin);
getchar();
}
36. #include
main()
{
int first,second,new;
int count;
clrscr();
first=0;
second=1;
count=2;
printf("\n the output is\n");
printf("\n===================\n");
printf("%d\n%d",first,second);
new = first + second;
while(++count < 10)
{
printf("\n%d",new);
getchar();
first=second;
second=new;
new=first + second;
}}
37. #include
main()
{
int i;
clrscr();
i=0;
while(i<20)
{
i=i+2;
printf("\n the even seris is:");
printf("%d",i);
}
getchar();
}
38. #include
main()
{ int i;
int no,digit,sum;
clrscr();
for(i=1;i<3;i++)
{
sum=0;
no=i;
while(no)
{
digit=no%10;
no=no/10;
sum=sum+digit*digit*digit;
}
if(sum==i)
printf("%d",i);
getchar();
}}
39. #include
main()
{
int i;
clrscr();
for(i=1;i<=100;i++)
{
if(i%9==0) continue;
printf("\t%d",i);
}
getchar();
}
40. #include
main()
{ int x,i,j;
clrscr();
printf("\nenter a number \n");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
fflush(stdin);
getchar();
}
41. #include
main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
if(i>i++)
printf("%d\t",i);
}
getchar();
}
42. #include
#include
main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%d",j);
}}
getchar();
}
43.
#include
main()
{
long number;
clrscr();
for (number = 1; number <= 100; number = number + 1)
printf("%i\t", number);
fflush(stdin);
getchar();
}
#include
main()
{
long start; /* the first number */
long count; /* the number of numbers */
long number; /* each number to print */
clrscr();
printf("Please enter a number to start at: ");
scanf("%ld", &start);
printf("Please enter the number of numbers: ");
scanf("%ld", &count);
for (number = start; number < start + count; number = number + 1)
printf("%i\n", number);
fflush(stdin);
getchar();
}
1. 36
2. true
3. false
4. b converted to int
5.
6. invalid
7. 4
8. r=5
9. 4
1
1111 1010
f800
2526 is a&b
a^b is ca91
6dc0 is a<<6
10. AATT
11. error assignment can not be done with arrays.
12. e, ello
13. hello
14. input string Hell AATT hai
15. 57 94
16.
17.
18. infinite loop
19. OUTPUT IS WASEBIET
20. some address
21. /* output is 1 2 2 1*/
22. /* output is 2*/
23. /* output is 2*/
24. /* output is 4*/
25. /* output is 5*/
26. 1..10
27. table of 5
28. 1
12
123
1234
12345
29. 1
22
333
4444
55555
30. 1
31. /*output is ABC*/
32. /*error*/
33. /*output is 5 5 5 associativity si right to left*/
34. /*program that reverses the given number*/
/* program that calculates sum of digits of an integers*/
35.
36. Fibonacci series
37./*generate even series from 1 to 20*/
38.
/*sum of cube of digits*/
39.
prints 1...100 except 9,18..
40.
*
**
***
41. output is 2 4 6 8 10
42. pyramid
43
No comments:
Post a Comment