baby ni guddalo na sulli
1. Palindrome Number
A number is a palindrome if it reads the same backward as forward.
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed) printf("%d is a palindrome.", original);
else printf("%d is not a palindrome.", original);
return 0;
}
output
Enter an integer: 1
1 is a palindrome.
=== Code Execution Successful ===
2a Pattern 1: The Vertical Bar (Pipe) Pattern
This pattern uses the | character.
Plaintext|
| |
| | |
| | | |
| | | | |
C#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("| ");
}
printf("\n");
}
return 0;
}
output
|
| |
| | |
| | | |
| | | | |
2b Pattern 2: The Number Triangle
This pattern increments numbers horizontally in each row.
Plaintext1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
C#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
2c Pattern 3: The Star (Asterisk) Pattern
This is the most common pattern requested in exams.
Plaintext*
* *
* * *
* * * *
* * * * *
C#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
output
*
* *
* * *
* * * *
* * * * *
7th Program: Swap Two Numbers using Call by Reference
C#include <stdio.h>
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;
printf("Enter two numbers (a and b): ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping: a = %d, b = %d", a, b);
printf("\nAfter Swapping: a = %d, b = %d\n", a, b);
return 0;
}
output
Enter two numbers (a and b): 10 20
Before Swapping: a = 10, b = 20
After Swapping: a = 20, b = 10
8 , Program: Fibonacci Series using Recursion
C#include <stdio.h>
int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return (fibonacci(n - 1) + fibonacci(n - 2));
}
}
int main() {
int limit, i;
printf("Enter the number of terms: ");
scanf("%d", &limit);
printf("Fibonacci Series: ");
for (i = 0; i < limit; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
output
Enter the number of terms: 6
Fibonacci Series: 0 1 1 2 3 5
9 wap to calculate factorial of a number using recursion
#include <stdio.h>
long int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial of a negative number doesn't exist.");
} else {
printf("Factorial of %d = %ld", num, factorial(num));
}
return 0;
}
output
Enter a positive integer: 5
Factorial of 5 = 120
Comments
Post a Comment