0% found this document useful (0 votes)
11 views9 pages

CMP 201 Codes

The document contains multiple C programs demonstrating various programming concepts, including checking for palindrome and Armstrong numbers, calculating permutations and combinations, reversing an integer, finding the largest number in a set, calculating factorials, solving simultaneous linear equations, transposing an array, and more. Each program is structured with input prompts, calculations, and output statements. The document serves as a practical guide for learning C programming through examples.
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)
11 views9 pages

CMP 201 Codes

The document contains multiple C programs demonstrating various programming concepts, including checking for palindrome and Armstrong numbers, calculating permutations and combinations, reversing an integer, finding the largest number in a set, calculating factorials, solving simultaneous linear equations, transposing an array, and more. Each program is structured with input prompts, calculations, and output statements. The document serves as a practical guide for learning C programming through examples.
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/ 9

CMP 201

1. C-PROGRAM TO CHECK IF A NUMBER IS A PALINDROME NUMBER

#include <stdio.h>

int main() {
int number, originalNumber, reversal = 0;

printf("Enter a number: ");


scanf("%d", &number);

originalNumber = number;

while (number > 0) {


reversal = (reversal * 10) + (number % 10);
number = number / 10;
}

if(originalNumber == reversal) {
printf("\n %d is a palindrome number", originalNumber);
} else {
printf("\n %d is not a palindrome number", originalNumber);
}
return 0;
}

2. C-PROGRAM TO CHECK IF A NUMBER IS AN ARMSTRONG NUMBER

#include <stdio.h>
#include <math.h>

int main() {
int number, originalNumber, sum = 0;

printf("Enter a number: ");


scanf("%d", &number);

originalNumber = number;

while (number > 0) {


sum = sum + pow((number % 10), 3);
number = number / 10;
}

if(originalNumber == sum) {
printf("\n %d is an armstrong number", numberClone);
} else {
printf("\n %d is not an armstrong number", numberClone);
}
return 0;
}

3. C-PROGRAM TO CALCULATE PERMUTATION AND COMBINATION

PERMUTATION (LONG)

#include <stdio.h>

int main() {
int n, r, factorialOfN = 1, factorialOfDiff = 1, permutation,
iteration = 1, difference;

printf("\n\nWhat is your value for n? ");


scanf("%d", &n);

printf("What is your value for r? ");


scanf("%d", &r);

difference = n - r;

while (iteration <= n) {


factorialOfN = factorialOfN * iteration;
iteration += 1;
}

iteration = 1;

while (iteration <= difference) {


factorialOfDiff = factorialOfDiff * iteration;
iteration += 1;
}

permutation = factorialOfN / factorialOfDiff;

printf("\nThe permutation of %d from %d is %d", r, n,


permutation);
return 0;
}

PERMUTATION (SHORT)

#include <stdio.h>

int main() {
int n, r, permutation = 1, difference;
printf("\n\nWhat is your value for n? ");
scanf("%d", &n);

printf("What is your value for r? ");


scanf("%d", &r);

difference = n - r;

while (n > difference) {


permutation = permutation * n;
n = n - 1;
}

printf("\nThe permutation is %d", permutation);


return 0;
}

COMBINATION

#include <stdio.h>

int combination() {
int n, r, factorialOfN = 1, factorialOfDiff = 1, factorialOfR =
1, combination, iteration = 1, difference;

printf("\n\nWhat is your value for n? ");


scanf("%d", &n);

printf("What is your value for r? ");


scanf("%d", &r);

difference = n - r;

while (iteration <= n) {


factorialOfN = factorialOfN * iteration;
iteration += 1;
}

iteration = 1;

while (iteration <= difference) {


factorialOfDiff = factorialOfDiff * iteration;
iteration += 1;
}

iteration = 1;

while (iteration <= r) {


factorialOfR = factorialOfR * iteration;
iteration += 1;
}

combination = factorialOfN / (factorialOfDiff * factorialOfR);

printf("\nThe combination of %d from %d is %d", r, n,


combination);
return 0;
}

ANOTHER COMBINATION AND PERMUTATION

4. C-PROGRAM TO REVERSE AN INTEGER

#include <stdio.h>

int main(){
int number, reversal = 0;

printf("Enter a number: ");


scanf("%d", &number);

while(number > 0) {
reversal = (reversal * 10) + (number % 10);
number = number / 10;
}

printf("\nYour number reversed is %d", reversal);


return 0;
}

5. C-PROGRAM TO CALCULATE THE LARGEST OF A SET OF INTEGERS

#include <stdio.h>

int main() {
int amount, number, max = 0, i;
printf("How many numbers do you want to calculate the max of? ");
scanf("%d", &amount);

for(i = 1; i <= amount; i++) {


printf("Enter number: ");
scanf("%d", &number);
if(number > max) {
max = number;
}
}

printf("\nThe largest of your set of numbers is %d", max);


return 0;
}

6. C-PROGRAM TO CALCULATE THE FACTORIAL OF A NUMBER

#include <stdio.h>

int main() {
int number, i, factorial = 1;
printf("Enter number: ");
scanf("%d", &number);

for(i = 1; i <= number; i++) {


factorial = factorial * i;
}

printf("\nFactorial of %d is %d", number, factorial);


return 0;
}
7. C-PROGRAM TO SOLVE SIMULTANEOUS LINEAR EQUATION

#include <stdio.h>

int main() {
int a, b, m, c, d, n;
float x, y;

printf("Enter the coefficients of x: \n");


scanf("%d\n%d", &a, &c);
printf("Enter the coefficients of y: \n");
scanf("%d\n%d", &b, &d);
printf("Enter the results of the equations: \n");
scanf("%d\n%d", &m, &n);

x = ((d * m) - (b * n)) / ((a * d) - (b * c));


y = ((a * n) - (c * m)) / ((a * d) - (c * b));

printf("\nx is %.2f and y is %.2f", x, y);


return 0;
}

8. C-PROGRAM TO TRANSPOSE AN ARRAY

#include <stdio.h>

int main() {
int i, i2, row, column;
printf("What are the dimensions of your array?\n");
printf("Row: ");
scanf("%d", &row);
printf("Column: ");
scanf("%d", &column);

int normalArr[row][column];
int transposeArr[column][row];
for (i = 0; i < row; i++) {
for(i2 = 0; i2 < column; i2++) {
printf("Enter entry: ");
scanf("%d", &normalArr[i][i2]);
}
}
for (i = 0; i < column; i++) {
for(i2 = 0; i2 < row; i2++) {
transposeArr[i][i2] = normalArr[i2][i];
}
}
return 0;
}

9. CODE TO ADD ELEMENTS OF AN ARRAY (OF SIZE 5)

10. CODE TO DISPLAY MULTIPLICATION TABLE (1-20)


11. C-PROGRAM TO CALCULATE AREA OF TRIANGLE

USING 1/2 X B X H
USING HERON’S FORMULA

You might also like