0% found this document useful (0 votes)
73 views6 pages

Lovely Professional University: Mittal School of Business

This document contains a student's submission for an academic task in Programming in C at Lovely Professional University. It includes the student's responses to 5 questions. The questions ask about the differences between while and do-while loops, uses of break and continue statements with examples, how to write a program to check if a number is positive or negative, and the differences between compilation and execution in C. The student provides detailed answers explaining the key differences and examples of code.

Uploaded by

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

Lovely Professional University: Mittal School of Business

This document contains a student's submission for an academic task in Programming in C at Lovely Professional University. It includes the student's responses to 5 questions. The questions ask about the differences between while and do-while loops, uses of break and continue statements with examples, how to write a program to check if a number is positive or negative, and the differences between compilation and execution in C. The student provides detailed answers explaining the key differences and examples of code.

Uploaded by

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

LOVELY PROFESSIONAL UNIVERSITY

MITTAL SCHOOL OF BUSINESS


ACADEMIC TASK : 1

Course code: CAP100 Course title:Programming in C


Class: BBA SECTION: Q1915
SUBMITTED TO: Rohit Sethi
SUBMITTED BY: Esmail yaseen alaswani
Reg No:: 11916535
ROLL NO: B45
DATE OF ALLOTMENT: 14/08/2020 DATE OF SUBMISSION : 24/08/2020
Five Marks Questions
Q3: Give any 2 differences between while statement and a Do- while
statement in C?
While loop:
A while loop is a control flow statement that allows code to be executed
repeatedly based on a given Boolean condition. The while loop can be thought of
as a repeating if statement.
Syntax:
While (boolean condition)
{
loop statements …
}

Do – while loop:
do while loop is similar to while loop with the only difference that it checks for the
condition after executing the statements, and therefore is an example of Exit
Control Loop.
Syntax: Syntax
do
{
statements ..
}
while (condition);

Q9: Explain the use of break and continue statement in loops with
example.
1.Functionality
Break statement mainly used to terminate the enclosing loop such as while, do-
while, for or switch statement wherever break is declared.
Continue statement mainly skip the rest of loop wherever continue is declared
and execute the next iteration.
2. Executional flow Break statement resumes the control of the program to the
end of loop and made executional flow outside that loop.
Continue statement resumes the control of the program to the next iteration of
that loop enclosing continue and made executional flow inside the loop again.
3.Usage As mentioned break is used for the termination of enclosing loop.
On other hand continue causes early execution of the next iteration of the
enclosing loop.
4. Compatibility Break statement can be used and compatible with switch ,
label .
We can t use continue statement with switch , lablel as it is not compatible with
them.

Q13: Write a program to check whether a number is positive or


negative?
#include<stdio.h>
Main()
{
Int num;
Printf(“ Enter the number: “);
Scanf(“%d”,&num);
If(num>0)
{
Printf(“\n Positive number”);
}
Else if(num<0)
{
Printf(“\n Negative number”);
}
Else if(num===0)
Printer(“\n Given number is Zero”);
}

Q17: Difference between Compilation and Execution in C?

Compilation is a process undertaken by the compiler which compiles the code


(expands the entire source code including headers, pre processor definitions
which then goes on to translate the source code into platform specific assembly
code. Has some static “compile time” checks.) For a detailed step by step
explanation of compilation process, Compilation is a process of checking your
program for any syntax errors or missing braces or missing semicolon etc.

In simple words it checks for any grammatical errors.


After compilation a executable is created which gets executed when a program is
run
Assemblers and Linker creates an object file (in vc++ it goes on to create an .exe
file too ).
It is this file that you execute. So is the process of actually running the program is
called as execution.
If your code has mistakes you the executible will not be created.
Ten Marks Questions
Q28: Write a C program to swap two numbers without the usage of
third variable?
We can swap two numbers without using third variable. There are two
common ways to swap two numbers without using third variable
1. By + and-
2. By * and /
Program 1: using + and –
Let's see a simple C example:

#include <stdio.h> int main ()


{
int a = 19, b = 20;
printf (“Before swap a =% d b =% d”, a, b);
a = a + b; // a = 30 (10 + 20)
b = a – b; // b = 10 (30-20)
a = a – b; // a = 20 (30-10)
printf (“\ nAfter swap a =% d b =% d”, a, b);
return 0;
}
Output:
Before swap a = 10 b = 20
After swap a = 20 b = 10
2
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int a = 10, b = 20; printf ("Before swap a =% d b =% d", a, b);
a = a * b; // a = 200 (10 * 20)
b = a / b; // b = 10 (200/20)
a = a / b; // a = 20 (200/10)
system ("cls");
printf ("\ nAfter swap a =% d b =% d", a, b);
return 0;
}
Output:
Before swap a = 10 b = 20
After swap a = 20 b = 10

You might also like