Practical 6

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

PRACTICAL-7

I)Objective
Write a program using nested if-else to find whether an
entered number is negative, positive or zero.

II)Requirement

 Hardware Required:
A computer or device capable of running a C compiler.

 Software Required:
A C compiler installed on the computer, such as GCC (GNU
Compiler Collection) or any suitable Integrated
Development Environment (IDE) that supports C
programming.Ex Replit,Online C compliler etc.

III)Theory

This program talks to the user and asks them to type in a


number. To make sure the program understands that the user
is typing a number (with or without decimal points), it uses a
special way called `float(input(...))`.
Then, it looks at the number you typed and does one of these
three things:

 If the number is more than zero, it says, "That's a


positive number."
 If the number is less than zero, it says, "That's a
negative number."
 If the number is neither more nor less than zero (it's
exactly zero), it says, "That's zero."

IV)Syntax

#include <stdio.h>

int main() {
int number;
// Take user input to enter a number
printf("Enter a number: ");
scanf("%d", &number);

// Check if the number is negative, positive, or zero


if (number > 0) {
printf("The entered number is positive.\n");
} else if (number < 0) {
printf("The entered number is negative.\n");
} else {
printf("The entered number is zero.\n");
}

return 0;
}

Output

Observation
The C program takes user input, classifies it as positive,
negative, or zero using if-else statements, and displays
the result clearly.
Student Summary

Student Faculty
Name:Yogyta Singh Name:Mr. Rakesh Kumar

Enrollment no.:22013I04070 Signature


Signature

Given Date: Submit Date:


Remark:
PRACTICAL-8

Objective
The objective of this program is to find the Greatest
Common Divisor (GCD) of two input numbers using both
a while loop and a for loop.

II)Requirement

 Hardware Required:
A computer or device capable of running a C compiler.

 Software Required:
A C compiler installed on the computer, such as GCC (GNU
Compiler Collection) or any suitable Integrated
Development Environment (IDE) that supports C
programming.Ex Replit,Online C compliler etc.

Theory
The GCD (Greatest Common Divisor) of two numbers can
be efficiently found using the Euclidean algorithm. Starting
with two input numbers, it involves repeatedly taking the
remainder of dividing the larger number by the smaller one
and swapping them until the remainder becomes zero. The
last non-zero remainder obtained in this process represents
the GCD of the two numbers, signifying the largest integer
that can evenly divide both numbers without leaving a
remainder.

Syntax

#include <stdio.h>

int main() {
int num1, num2, gcd;

// Input two numbers from the user


printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);

// Find GCD using a while loop


int temp1 = num1;
int temp2 = num2;
while (temp2 != 0) {
int remainder = temp1 % temp2;
temp1 = temp2;
temp2 = remainder;
}
gcd = temp1;

printf("GCD using a while loop: %d\n", gcd);

// Find GCD using a for loop


int smaller = (num1 < num2) ? num1 : num2;
for (int i = smaller; i >= 1; i--) {
if (num1 % i == 0 && num2 % i == 0) {
gcd = i;
break;
}
}

printf("GCD using a for loop: %d\n", gcd);

return 0;
}

Output

Observation
The program effectively accomplishes its objective of
finding the GCD of two numbers using different methods
and presents the results in a user-friendly manner.
Student Summary

Student Faculty
Name:Yogyta Singh Name:Mr. Rakesh Kumar

Enrollment no.:22013I04070 Signature


Signature

Given Date: Submit Date:


Remark:
PRACTICAL-9

Objective
Write a program to find the largest among the three
numbers using:
(a) if and logical and (&&) operator
(b) Nested if-else statement

II)Requirement
 Hardware Required:
A computer or device capable of running a C compiler.

 Software Required:
A C compiler installed on the computer, such as GCC (GNU
Compiler Collection) or any suitable Integrated
Development Environment (IDE) that supports C
programming.Ex Replit,Online C compliler etc.

Theory

(a) Using `if` and `&&` Operator:


- The program takes three numbers as input.
- It utilizes the `if` statement and the logical '&&' (AND)
operator to compare the numbers and identify the largest
one.
- Finally, it presents the largest number to the user.

(b) Using Nested `if-else` Statement:


- The program also takes three numbers as input.
- It employs a nested `if-else` statement structure to
compare the numbers and determine the largest among
them.
- It then communicates the largest number to the user as
the final output.
Syntax

(a) Using `if` and `&&` Operator:

#include <stdio.h>

int main() {
int num1, num2, num3;

// Input three numbers from the user


printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);

// Find the largest number using if and logical '&&'


operator
if (num1 >= num2 && num1 >= num3) {
printf("The largest number is: %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The largest number is: %d\n", num2);
} else {
printf("The largest number is: %d\n", num3);
}

return 0;
}

(b) Using Nested `if-else` Statement:

#include <stdio.h>

int main() {
int num1, num2, num3;

// Input three numbers from the user


printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);

// Find the largest number using nested if-else statements


if (num1 >= num2) {
if (num1 >= num3) {
printf("The largest number is: %d\n", num1);
} else {
printf("The largest number is: %d\n", num3);
}
} else {
if (num2 >= num3) {
printf("The largest number is: %d\n", num2);
} else {
printf("The largest number is: %d\n", num3);
}
}

return 0;
}

Output

Using `if` and `&&` Operator:


Using Nested `if-else` Statement:

Observation

(a) Using `if` and `&&` Operator:


- This program directly picks the largest number from
three options.
(b) Using Nested `if-else` Statement:
- This program takes a step-by-step approach to find the
largest number among three.

Student Summary

Student Faculty
Name:Yogyta Singh Name:Mr. Rakesh Kumar

Enrollment no.:22013I04070 Signature


Signature

Given Date: Submit Date:


Remark:
PRACTICAL-6

Objective:
The objective of this program is to interact with the user,
collect an integer number as input, and then perform the
following operations on the input number:
(a) Count the total number of digits in the number.
(b) Count the sum of even digits in the number.
(c) Count the sum of odd digits in the number.
(d) Count the sum and average of all digits in the number.

II)Requirement
 Hardware Required:
A computer or device capable of running a C compiler.

 Software Required:
A C compiler installed on the computer, such as GCC (GNU
Compiler Collection) or any suitable Integrated
Development Environment (IDE) that supports C
programming.Ex Replit,Online C compliler etc.

III)Theory

This program asks the user for a number and then does some
math with its digits. It counts how many digits there are, adds
up all the digits, figures out the sum of the even digits, and
calculates the sum of the odd digits. Finally, it works out
the average of all the digits. The program shows these
results to the user, so they can see how many digits were in
their number, what the sums are, and the average of all the
digits.

IV)Syntax

#include <stdio.h>

int main() {
int n, d, total = 0, evenSum = 0, oddSum = 0, sum = 0;
float avg;
printf("Enter an integer number: ");
scanf("%d", &n);

while (n != 0) {
d = n % 10;
total++;
sum += d;

if (d % 2 == 0) {
evenSum += d;
} else {
oddSum += d;
}

n /= 10;
}

avg = (float)sum / total;

printf("Total number of digits: %d\n", total);


printf("Sum of even digits: %d\n", evenSum);
printf("Sum of odd digits: %d\n", oddSum);
printf("Sum of all digits: %d\n", sum);
printf("Average of all digits: %.2f\n", avg);

return 0;
}

Output
Student Summary

Student Faculty
Name:Yogyta Singh Name:Mr. Rakesh Kumar

Enrollment no.:22013I04070 Signature


Signature

Given Date: Submit Date:


Remark:
PRACTICAL-10

Objective
Write a program to display the following patterns. In this
program user will enter the size of
the pattern in terms of the rows, and based on that, the
pattern will adjust.

II)Requirement
 Hardware Required:
A computer or device capable of running a C compiler.

 Software Required:
A C compiler installed on the computer, such as GCC (GNU
Compiler Collection) or any suitable Integrated
Development Environment (IDE) that supports C
programming.Ex Replit,Online C compliler etc.

III)Theory
A `for` loop is a control structure in programming that allows
you to repeatedly execute a block of code a specific number of
times.
Pattern Printing Using Nested `for` Loops:
1. Outer Loop(s): Each outer loop runs from a starting value
(usually 1) to an ending value that corresponds to the desired
number of rows.
2. Inner Loop(s): These inner loops typically run from a
starting value (usually 1) to an ending value that depends on
the row number.

3. Printing Characters: Inside the innermost `for` loop, you


specify the characters or symbols to be printed

4. Line Break: After printing the characters for a row using


the inner `for` loop(s), you often add a line break (`\n`) to
move to the next row

Syntax Output
#include <stdio.h>
int main() {
int rows;
printf("Enter the number
of rows: ");
scanf("%d", &rows);
for (int i = 1; i <=
rows; i++) {
for (int j = 1; j <= i;
j++) {
printf("* ");
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main() {
int rows = 5;
for (int i = rows; i >=
1; i--) {
for (int j = 1; j <=
i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int rows = 5;
int stars = 1;
for (int i = 1; i <= rows;
i++) {
for (int j = 1; j <=
rows - i; j++) {
printf(" ");
}
for (int j = 1; j <=
stars; j++) {
printf("*");
}
printf("\n");
stars += 2;
}
return 0;
}

#include <stdio.h>
int main() {
int rows = 5;
for (int i = rows; i >= 1;
i--) {
for (int j = 1; j <=
rows - i; j++) {
printf(" ");
}
for (int j = 1; j <= 2
* i - 1; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows;
i++) {
for (int j = 1; j <= i;
j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main() {
int rows = 5;
for (int i = rows; i >= 1;
i--) {
for (int j = 1; j <= i;
j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows;
i++) {
for (int j = 1; j <= rows
- i; j++) {
printf(" ");
int num = i;
for (int j = 1; j <=
i; j++) {
printf("%d", num);
if (j < i) {
printf(" ");
}
num++;
}
num = 2 * i - 2;
for (int j = i - 1; j
>= 1; j--) {
printf(" %d", num);
num--;
}
printf("\n");
}
return 0;
}

int main() {
int rows = 6;
for (int i = 0; i < rows;
i++) {
int num = 1;
for (int j = 1; j <=
rows - i; j++) {
printf(" ");
}
for (int j = 0; j <= i;
j++) {
printf("%2d", num);
if (j < i) {
num = num * (i
- j) / (j + 1);
printf(" ");
}
}
printf("\n");
}
return 0;
}
int main() {
int rows = 6;
for (int i = 0; i < rows;
i++) {
int num = 1;
for (int j = 1; j <=
rows - i; j++) {
printf(" ");
}
for (int j = 0; j <= i;
j++) {
printf("%2d", num);
if (j < i) {
num = num * (i
- j) / (j + 1);
printf(" ");
}
}
printf("\n");
}
return 0;
}

Observation
This code uses nested loops to create an triangle pattern
of asterisks and numbers with proper formatting and size
control.

Student Summary

Student Faculty
Name:Yogyta Singh Name:Mr. Rakesh Kumar

Enrollment no.:22013I04070 Signature


Signature

Given Date: Submit Date:


Remark:

You might also like