You are on page 1of 8

C PROGRAMMING & DATA STRUCTURE

ASSIGNMENT-1

Name: K. SHIVA RANJINI


Roll.No:18691A05D0
Branch&Sec: CSE-C
Name of the faculty:
Dr.Mehaboob Basha
Department: CSE

1. Explain the difference between for loop, while loop, and do..while loop.
Ans:
for loop:
Syntax:
for (initialization; terminal; updation) /*header*/
{
Statements /*body of the loop*/
}
In for loop the header of the loop consists of all the 3 statements related with
control variables.

To start the execution of the loop the initialization statement is executed first,
then the terminal statement is executed.

If the terminal statement is true then body of the loop is executed then update
statement is executed. If terminal statement is false then loop is
terminated.

The iteration does not occur if the controlling condition is false at the first
iteration itself.
While loop:
Syntax:
Initialization
while(terminal)
{
Body
Update statement
}
As for loop, the while loop also has 3 statements related to control variables.
In while loop the initialization statement will be before the loop and the update
statement is as a part of the body of the loop.
As for loop, the iteration does not occur if the condition is false at the first
iteration itself.
do..while loop:
Syntax:
Initialization
do
{
Update

} while (terminal)

In do..while loop the terminal condition is checked at the end of the body of the
loop. So, the loop is executed at least once.

Mainly do..while is preferred for menu driven programs.

2. Compare ladder if and switch statement.


Ans:
Ladder if:
Syntax:
if (exp1)
{
statement set 1
}
else if (exp2)
{
statement set 2
}
.
.
.
else if (exp n)
{
statement set n
}
else
{
statement set n+1 /*It is executed when no exp is
true*/
}
In ladder if starting from exp1 each exp is checked till one of the exp becomes
true.
When any exp becomes true then the remaining part of ladder if is skipped.
When no exp is true then the last block which is else block is executed.

Switch:
Syntax:
switch (exp)
{
case value 1: statements
case value 2: statements
.
.
.
case value n: statements
default: statements
}

It is an alternate tool to ladder if statement.


Every ladder if cannot be replaced with switch but every switch can be replaced
with ladder if.

When execution is started, it executes the case which is matched with the result
of expression and continues executing case one after another till a break
statement is found or end of the switch is reached.

When no match is found, then default statement is executed.


3. Implement the sum of the digits of a number using recursion.
Ans:

#include<stdio.h>
int main( )
{
int num, result;
printf(“enter the number”);
scanf(“%d”, &sum);
result = sum(num);
printf(“sum of the number is %d”, result);
}
/*function definition*/

int sum(int n)
{
if (n==0)
return 0;
else
return((n%10)+sum(n/10));
}
Output:
Enter the number:2468
Sum of the number:10
4. Give the output of the following
i) char ch=127;
ch+=2;
printf(“%d”,ch);
Output:
-127
ii) char ch=’Z’;
ch+=7;
printf(“%c”,ch);
Output:
a
iii) char a,b;
a=25;
printf(“%d”,~a);
Output:
-26
iv) char a,b;
a=-27;
printf(“%d”,a>>2);

Output:
-7
v) d=(a=10,b=20,c=30)
printf(“%d”,d);
Output:
30
vi) a=0; b=(a=0?10:20);
printf(“%d”,b);
Output:
20
vii) int a;
a=-25;
printf(“%d”,a/2);
Output:
-12
viii) int m,n=1;
for(m=n-2;m<5;i++)
j+=j;
printf(“%d”,j);
Output:
No output i.e., error (i, j is not declared)
ix) int a,b,c;
a=b=c=-1;
c=++a&&++b||++c;
printf(“%d-%d-%d”,a,b,c);
Output:
0 - -1 - 0
x) int a,b,c,d,n;
a=5,b=10,c=3,d=8;

n=a%b/d*c+10;
printf(“%d”,n);
Output:
10

You might also like