Practical 6
Practical 6
Practical 6
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
IV)Syntax
#include <stdio.h>
int main() {
int number;
// Take user input to enter a number
printf("Enter a number: ");
scanf("%d", &number);
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
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;
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
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
#include <stdio.h>
int main() {
int num1, num2, num3;
return 0;
}
#include <stdio.h>
int main() {
int num1, num2, num3;
return 0;
}
Output
Observation
Student Summary
Student Faculty
Name:Yogyta Singh Name:Mr. Rakesh Kumar
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;
}
return 0;
}
Output
Student Summary
Student Faculty
Name:Yogyta Singh Name:Mr. Rakesh Kumar
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.
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