PPS LAb EXP 7
PPS LAb EXP 7
Aim
To write a program in C to display the cube of the number up to an integer.
Algorithm
1. Start
2. Declare integer variables n and i.
3. Prompt the user to input the number of terms.
4. Read the value into variable n.
5. For each integer i from 1 to n:
a. Calculate the cube of i by computing i * i * i.
b. Display the value of i and its cube in the specified format.
6. End
Program
#include <stdio.h>
int main() {
int n, i;
return 0;
}
Output
Input number of terms: 5
Number is : 1 and cube of the 1 is : 1
Number is : 2 and cube of the 2 is : 8
Number is : 3 and cube of the 3 is : 27
Number is : 4 and cube of the 4 is : 64
Number is : 5 and cube of the 5 is : 125
Result
Thus the C program to display the cube of the number up to an integer is completed
successfully.
Aim
To Write a program in C to display the multiplication table for a given integer.
Algorithm
1. Start
2. Declare integer variables n and i.
3. Prompt the user to input the number for which the table is to be calculated.
4. Read the value into variable n.
5. For each integer i from 1 to 10:
a. Calculate the product of n and i (n * i).
b. Display the value of n, i, and the product in the specified format (n X i =
product).
6. End
Program
#include <stdio.h>
int main() {
int n, i;
printf("Input the number (Table to be calculated): ");
scanf("%d", &n);
for(i = 1; i <= 10; i++) {
printf("%d X %d = %d\n", n, i, n * i);
}
return 0;
}
Output
Input the number (Table to be calculated): 15
15 X 1 = 15
15 X 2 = 30
15 X 3 = 45
15 X 4 = 60
15 X 5 = 75
15 X 6 = 90
15 X 7 = 105
15 X 8 = 120
15 X 9 = 135
15 X 10 = 150
Result
Thus the C program to display the multiplication table for a given integer is done
successfully.
3. Write a program in C to display a pattern like a right angle triangle using an asterisk.
*
**
***
****
Aim
To write a C program to display a pattern like a right angle triangle using an
asterisk.
Algorithm
1. Start
2. Declare integer variables i, j, and rows.
3. Prompt the user to enter the number of rows for the triangle.
4. Read the value into variable rows.
5. For each integer i from 1 to rows (outer loop):
a. For each integer j from 1 to i (inner loop):
b. Print an asterisk (*).
c. Print a newline (\n) after each row to move to the next line.
6. End
Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 4
*
**
***
****
Result
Thus the C program for to displaying a pattern like a right angle triangle using an
asterisk was run successfully.
4.Write a C program to display a pattern like a right angle triangle with a number.
1
12
123
1234
Aim
To Write a C program to display a pattern like a right angle triangle with a number.
Algorithm
1. Start
2. Declare integer variables i, j, and rows.
3. Prompt the user to enter the number of rows for the triangle.
4. Read the value into the variable rows.
5. For each integer i from 1 to rows (outer loop):
a. For each integer j from 1 to i (inner loop):
b. Print the value of j without a newline.
c. Print a newline (\n) after each row to move to the next line.
6. End
Program
#include <stdio.h>
int main() {
int i, j, rows;
return 0;
}
Output
Result
Thus the C Program to display a pattern like a right angle triangle with a number
completed.
5.Write a program in C to make such a pattern like a right angle triangle with a
number which will repeat a number in a row.
1
22
333
4444
Aim
To write a to make such a pattern like a right angle triangle with a number which
will repeat a number in a row.
Algorithm
1. Start
2. Declare integer variables i, j, and rows.
3. Prompt the user to enter the number of rows for the triangle.
4. Read the value into the variable rows.
5. For each integer i from 1 to rows (outer loop):
a. For each integer j from 1 to i (inner loop):
b. Print the value of i without a newline.
c. Print a newline (\n) after each row to move to the next line.
6. End
Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
Output
Result
Thus the C program to make such a pattern like a right angle triangle with a number which
will repeat a number in a row completed.
1
23
456
7 8 9 10
Aim
To create a program to make a pyramid pattern with numbers increased by 1.
Algorithm
1. Start
2. Declare integer variables i, j, rows, and num (initialize num to 1).
3. Prompt the user to enter the number of rows for the pyramid.
4. Read the value into the variable rows.
5. For each integer i from 1 to rows (outer loop for rows):
a. For each integer j from 1 to rows - i (inner loop for spaces):
i. Print a space character to create the necessary indentation.
b. For each integer j from 1 to i (inner loop for numbers in the row):
i. Print the current value of num.
ii. Increment the value of num by 1.
c. Print a newline (\n) after each row to move to the next line.
6. End
Program
#include <stdio.h>
int main() {
int i, j, rows, num = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++) {
// Printing spaces to center-align the pyramid
for(j = 1; j <= rows - i; j++) {
printf(" ");
}
// Printing the numbers in the pyramid
for(j = 1; j <= i; j++) {
printf("%d ", num);
num++; // Incrementing the number for the next position
}
printf("\n");
}
return 0;
}
Output
Result
Thus the C program to make a pyramid pattern with numbers increased by 1
completed.
7.Write a C program to calculate the factorial of a given number.
Aim
Algorithm
1. Start
2. Declare integer variables n, i, and factorial. Initialize factorial to 1.
3. Prompt the user to enter a number (n).
4. Read the value of n.
5. Check if the number n is negative:
a. If n is negative, display a message: "Factorial is not defined for negative
numbers."
b. If n is zero or positive, proceed to step 6.
6. For each integer i from 1 to n:
a. Multiply factorial by i and store the result in factorial.
7. Display the result of factorial.
8. End
Program
#include <stdio.h>
int main() {
int n, i;
long long factorial = 1;
// Prompt user for input
printf("Enter a number: ");
scanf("%d", &n);
// Check if the number is negative, zero, or positive
if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate factorial
for(i = 1; i <= n; i++) {
factorial *= i; // Multiply factorial by i in each iteration
}
// Print the result
printf("Factorial of %d is %lld\n", n, factorial);
}
return 0;
}
Output
Enter a number: 5
Factorial of 5 is 120
Enter a number: 0
Factorial of 0 is 1
Result
Thus the C Program to calculate the factorial of a given number is done successfully.
1
01
101
0101
10101
Aim
To write a C program for Print Floyd's Triangle.
Algorithm
1. Start
2. Declare integer variables i, j, and rows.
3. Prompt the user to enter the number of rows for the triangle.
4. Read the value of rows.
5. For each integer i from 1 to rows (outer loop for rows):
a. For each integer j from 1 to i (inner loop for elements in the row):
i. If the sum of i and j is even ((i + j) % 2 == 0), print 1.
ii. Else, print 0.
b. Print a newline (\n) after each row to move to the next line.
6. End
Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
if ((i + j) % 2 == 0) {
printf("1");
} else {
printf("0");
}
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 5
1
01
101
0101
10101
Result
Thus the C program to print Floyd's Triangle completed.
Aim
To write a C program to check whether a given number is an Armstrong number or not.
Algorithm
1. Start
2. Declare integer variables n, originalNum, remainder, result, and numDigits.
3. Prompt the user to enter a number (n).
4. Read the value of n into the variable n.
5. Store the value of n in the variable originalNum (to use it later for comparison).
6. Calculate the number of digits in the number (numDigits) using the formula:
7. numDigits = (int)log10(n) + 1.
Initialize result to 0. This will store the sum of digits raised to the power of
numDigits.
8. While n is not equal to 0:
a. Extract the last digit of n using remainder = n % 10.
b. Add the remainder raised to the power of numDigits to result using result +=
pow(remainder, numDigits).
c. Remove the last digit of n using n /= 10.
9. If result is equal to originalNum:
a. Print that the number is an Armstrong number.
10. Else:
a. Print that the number is not an Armstrong number.
11. End
Program
#include <stdio.h>
#include <math.h>
int main() {
int n, originalNum, remainder, result = 0, numDigits;
// Input the number
printf("Input a number: ");
scanf("%d", &n);
// Store the original number to use for comparison later
originalNum = n;
// Find the number of digits in the number
numDigits = (int)log10(n) + 1;
// Calculate the sum of the digits raised to the power of numDigits
while (n != 0) {
remainder = n % 10; // Get the last digit
result += pow(remainder, numDigits); // Add the digit raised to the power
n /= 10; // Remove the last digit
}
// Check if the result is equal to the original number
if (result == originalNum) {
printf("%d is an Armstrong number.\n", originalNum);
} else {
printf("%d is not an Armstrong number.\n", originalNum);
}
return 0;
}
Output
Input a number: 153
153 is an Armstrong number.
Result
Thus the C program to check whether a given number is an Armstrong number or not
is done successfully.
10.Write a C program that prompts the user to enter a password. Use a do-while loop to keep
asking for the password until the correct one is entered.
Aim
To write a C program that prompts the user to enter a password. Use a do-while loop
to keep asking for the password until the correct one is entered.
Algorithm
1. Start
2. Declare a string password to store the user's input.
3. Define a constant string correctPassword to store the correct password.
4. Repeat the following steps until the correct password is entered:
a. Prompt the user to enter the password.
b. Read the user input into the password variable.
c. Compare the entered password with the correctPassword using strcmp():
d. If the passwords do not match, display "Incorrect password. Please try again."
and repeat.
5. When the correct password is entered, print "Password accepted. Welcome!"
6. End
Program
#include <stdio.h>
#include <string.h>
int main() {
char password[20]; // Array to store the password
const char correctPassword[] = "password123"; // Correct password
// Use a do-while loop to keep asking for the password
do {
printf("Enter the password: ");
scanf("%s", password); // Read the user's input
// Check if the entered password matches the correct one
if (strcmp(password, correctPassword) != 0) {
printf("Incorrect password. Please try again.\n");
}
} while (strcmp(password, correctPassword) != 0); // Repeat until the correct password is
entered
printf("Password accepted. Welcome!\n");
return 0;
}
Output
Enter the password: 12345
Incorrect password. Please try again.
Enter the password: password123
Password accepted. Welcome!
Result
Thus the C program to prompts the user to enter a password. Use a do-while loop to
keep asking for the password until the correct one is entered.