Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Assginment 1

1. Demonstrate Compilation and interpretation stages to for C, C++, JAVA.


 Compilation using Turbo C
 Launch the application.
 Select File, New to write a new file in C/C++.
 Save the file as .C/.CPP.
 To compile the file, ALT+F9.
 To run the file, CTRL+F9.
 Check output.
 Debugging steps for C/C++ using Turbo C.
 Open the program to be debugged in Turbo C++
 In the Windows tab, use Watch and Output to add the respective windows on the screen.
 To add the variables to be debugged, double click on the watch window to keep adding the
variables.
 Go to debugging press CTRL+F8.
 Keep pressing F7 for step wise output.
 Using Online IDE for C/C++/Java
 Log on to www.onlinegdb.com,/ www.jdoodle.com
 Select the programming language for coding
 Create new project.
 Save the project using CTRL+S.
 Run the program using F9.
 For debugging use F8, along with step-into function of onlinegdb
2.To execute the use of nested loops in patterns.

What is Loop? It is the ability to perform a set of instructions repeatedly. This involves repeating
some portion of the program either a specified number of times or until a particular condition is
being satisfied. This repetitive operation is done through a loop control instruction. There are
three methods by way of which we can repeat a part of a program. Using a for statement, while
statement or do-while statement.

Nesting of loops is the feature that allows the looping of statements inside
another loop. Any number of loops can be defined inside another loop, i.e., there
is no restriction for defining any number of loops. The nesting level can be
defined at n times.

Nested for loop

The nested for loop means any type of loop which is defined inside the 'for' loop.

Example of nested for loop

#include <stdio.h>

int main() {

int n;

printf("Enter the value of n :");

scanf("%d",&n);

for(int i=1;i<=n;i++) {

for(int j=1;j<=10;j++) {

printf("%d\t",(i*j));

printf("\n");

return 0;

}
Nested while loop

The nested while loop means any type of loop which is defined inside the
'while' loop.

Example of nested while loop:

#include <stdio.h>
int main()
{
int rows,columns,k=1;
printf("Enter the number of rows :");
scanf("%d",&rows);
printf("\nEnter the number of columns :");
scanf("%d",&columns);
int a[rows][columns],i=1,j=1;
while(i<=rows)
{
while(j<=columns)
{
printf("%d\t",k);
k++;
j++;
}
i++;
printf("\n");
}
}
OUTPUT:
Nested do..while loop

The nested do..while loop means any type of loop which is defined inside the
'do..while' loop.

Example of nested do..while loop:

#include <stdio.h>
int main()
{
int i=1,j=1;
do
{
int j=1;
do
{
printf("*");
j++;
}while(j<=8);
printf("\n");
i++;
}while(i<=4);
}
OUTPUT:
Exercise 2.2:

Suppose we are working with the following knowledge base:

wizard(ron).

hasWand(harry).

quidditchPlayer(harry).

wizard(X):- hasBroom(X), hasWand(X).

hasBroom(X):- quidditchPlayer(X).

How does Prolog respond to the following queries?

1. wizard(ron).--------------true.

2. witch(ron).-----------witch(A) doesnot exist.

3. wizard(hermione).---------------false.

4. witch(hermione).-------------witch(A) doesnot exist.

5. wizard(harry).---------------true

6. wizard(Y).-----------Y=RON

7. witch(Y).------------DOESNOT EXIST

Exercise 2.1:

Represent the following in Prolog:

1. Butch is a killer.killer(butch).

2. Mia and Marsellus are married. Married(mia,marsellus).

3. Zed is dead.dead(zed).

4. Marsellus kills everyone who gives Mia a footmassage.kills(marsellus,X):-givesfootmassage(X,mia).

5. Mia loves everyone who is a good dancer.loves(mia,X):-gooddancer(X).

6. Jules eats anything that is nutritious or tasty.eats(jules,X):-nutritious(X);tasty(X).


3.To execute a program to display PASCAL’S TRIANGLE.

Pascals triangle is an arrangement of binomial coefficients in triangular form. It


is named after the French mathematician Blaise Pascal. a triangular array of
numbers in which those at the ends of the rows are 1 and each of the others is
the sum of the nearest two numbers in the row above (the apex, 1, being at the
top). It is used widely in probability theory, combinatorics, and algebra.

#include <stdio.h>

int main() {

int r, f = 1, s, i, j;

printf("Enter the number of rows: ");

scanf("%d", &r);

for (i = 0; i < r; i++) {

for (s= 1; s <= r - i; s++)

printf(" ");

for (j = 0; j <= i; j++) {

if (j == 0 || i == 0)
f = 1;

else

f = f * (i - j + 1) / j;

printf("%4d", f); }

printf("\n");

return 0; }

You might also like