Escolar Documentos
Profissional Documentos
Cultura Documentos
#include <stdio.h>
int main() {
int a=0,b=0;
char c;
printf("Assign a character:");
scanf("%c",&c);
if (c>101)
{
a=10;
b=10;
}
printf("Summation of a and b is %d",a+b);
}
1
BIL-200 TUTORIAL FOR LAB-3 Group A&B&C
the returning value of the condition written in the “If (condition)” statement. The
first expression is written after the “If(condition)” statement, and if the condition
is satisfied then this expression is executed and the program then by-pass the ex-
pression written in the “Else” statement. If the condition fails, then the expression
in the else statement is executed.
What may be the outputs of the following program?
Don’t write the code to find the exact solution
#include <stdio.h>
int main() {
int a,b;
a=50;
b=100;
if (a+b>150)
{ printf("\n Summation is greater than 150" );
printf("\n that’s all" );
}
else
{ printf("\n Summation is less than (or equal ) 150");
printf("\n that’s all" );
}
}
#include<stdio.h>
int main(){
int a;
printf("Input an Integer:\n");
scanf("%d",&a);
if(a%2==0 && a<0)
{printf("%d is even and less than zero",a); }
else if(a%2!=0 && a<0)
{printf("%d is odd and less than zero",a); }
else if(a%2==0 && a>0)
{printf("%d is even and strictly positive",a); }
else if(a%2!=0 && a>0)
{printf("%d is odd and strictly positive",a);}
else
printf("You Entered Zero");
}
2
BIL-200 TUTORIAL FOR LAB-3 Group A&B&C
#include <stdio.h>
int main() {
int a=0,b=0;
char c;
printf("This program increases the values of a and b depends on the choice\n");
printf("If users assigns ‘a’ then a is increased 1\n");
printf("If users assigns ‘A’ then a is increased 2\n");
printf("If users assigns ‘b’ then b is increased 1\n");
printf("If users assigns ‘B’ then b is increased 2\n");
printf("If users assign ‘c’ or ’C’, then both a and b are increased 1\n");
printf("If users assigns ‘E’ or ’e’, the program terminates\n");
printf("\n\nAssign a character:");
scanf("%c",&c);
switch(c)
{
case ’a’:
++a;
break;
case ’A’:
a=a+2;
break;
case ’b’:
++b;
break;
case ’B’:
b=b+2;
break;
case ’c’:
++a;
++b;
break;
case ’C’:
++a;
++b;
break;
case ’E’:
printf("The program terminates ");
break;
case ’e’:
printf("The program terminates ");
break;
}
printf("a=%d b=%d\n",a,b);
}
3
BIL-200 TUTORIAL FOR LAB-3 Group A&B&C
’While’ loop: This structure is another conditional loop. In this loop, there exists
a condition to initialize the while loop. If the initialization condition fails, then
program skips the while loop and executes the next following statements of the
while loop. If the condition is satisfied, the program enters the loop and it executes
all the statements continuously in the loop up to condition fails. Simple “while”
examples are listed below; Examine the following programs;
#include <stdio.h>
int main() {
int i,sum;
i=0;
sum=0;
while (i<=10)
{ sum=sum+i;
i++; }
printf("Summation 1+2+...+10 equals %d\n",sum);}
Home Exercise 1: This examples also provides you to be experienced with usage
of “while” and “if”. Write a program that asks to user assign a value to n and then the
following output is resulted at the screen. If n = 10, then the output is following:
|10 − 0| = 10
|9 − 1| = 8
|8 − 2| = 6
. . .
|.. − ..| = ..
|1 − 9| = 8
|0 − 10| = 0
Do not use the abs() command. The | · | can be printed by using the printf function.
Home Exercise 2: Write a program to compute the roots of a second order equation
ax2 + bx + c. The program repeatedly asks the user to assign a value to the variables a,
b, and c. Then, it calculates the roots of the second order equation.
Usage of getchar and putchar The functions getchar and putchar are macros defined
in stdio.h. getchar reads a character from the keyboard and putchar prints a character
to the screen. In a memory, a char is stored in 1 byte, and an int is stored typically in 2
bits or 4 bits (according to compiler). Let us consider the following example.
#include<stdio.h>
int main() {
char x=’a’;
int a=0;
while (x!=’e’)
{
a=a+1;
x=getchar();
}
putchar(x);
puts("Bye Bye \n");
printf("a is %d ",a);
}
4
Project 2: You will write a calculation program that recieves two parameters from the user.
First parameter(SEL_PAR) identifies the type of the calculation. It should be an integer and
can take the values from 1 to 4.
Second parameter(X) should also be a positive integer which is used in calculation.
i. If SEL_PAR is equal to 1, program should calculate the factorial of X (X!) and print the
result.
ii. If SEL_PAR is equal to 2, program should calculate the summation: ∑ and print
the result.
iii. If SEL_PAR is equal to 3, program should give the Xth element of the Fibonacci series.
For example: If X is 7,
The first seven elements of the Fibonacci series are 1, 1, 2, 3, 5, 8, 13.
So the result should be 13.
iv. If SEL_PAR is equal to 4, program should calculate XX . (Do not use math library!
Calculate by using for or while cycle.)