Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Algorithm and Programs

C

Uploaded by

jimitmehta2006
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 views

Algorithm and Programs

C

Uploaded by

jimitmehta2006
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/ 25

Algorithm and Programs

Here are the algorithms paired with their respective C programs:

1. Hello, World!
Algorithm:
1. Start
2. Print "Hello, World!" to the console
3. End

C Code:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}

2. Basic Arithmetic Operations


Algorithm:
1. Start
2. Initialize two integers `a` and `b`
3. Print the sum of `a` and `b`
4. Print the difference between `a` and `b`
5. Print the product of `a` and `b`
6. Print the quotient of `a` divided by `b`
7. End

C Code:
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("Sum: %d\n", a + b);
printf("Difference: %d\n", a - b);
printf("Product: %d\n", a * b);
printf("Quotient: %d\n", a / b);
return 0;
}
3. Finding the Largest of Two Numbers
Algorithm:
1. Start
2. Read two integers `a` and `b`
3. If `a` is greater than `b`, print `a`
4. Otherwise, print `b`
5. End
C Code:
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

if (a > b) {
printf("Largest number is: %d\n", a);
}
else {
printf("Largest number is: %d\n", b);
}
return 0;
}
4. Checking Even or Odd
Algorithm:
1. Start
2. Read an integer `num`
3. If `num` is divisible by 2, print that it is even
4. Otherwise, print that it is odd
5. End
C Code:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}

5. Print Numbers 1 to 10
Algorithm:
1. Start
2. For `i` from 1 to 10:
- Print `i`
3. End
C Code:
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
return 0;
}
6. Sum of First N Natural Numbers
Algorithm:
1. Start
2. Read an integer `n`
3. Initialize `sum` to 0
4. For `i` from 1 to `n`:
- Add `i` to `sum`
5. Print `sum`
6. End
C Code:
#include <stdio.h>

int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("Sum of first %d natural numbers is %d\n", n, sum);
return 0;
}
7. Reverse a Number
Algorithm:
1. Start
2. Read an integer `num`
3. Initialize `reversed` to 0
4. While `num` is not 0:
- Find the last digit of `num` and store it in `remainder`
- Update `reversed` by adding `remainder`
- Remove the last digit from `num`
5. Print `reversed`
6. End

C Code:
#include <stdio.h>
int main() {
int num, reversed = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
printf("Reversed number is %d\n", reversed);
return 0;
}
8. Factorial of a Number
Algorithm:
1. Start
2. Read an integer `num`
3. Initialize `factorial` to 1
4. For `i` from 1 to `num`:
- Multiply `factorial` by `i`
5. Print `factorial`
6. End

C Code:
#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial of %d is %d\n", num, factorial);
return 0;
}
9. Simple Interest Calculation
Algorithm:
1. Start
2. Read `principal`, `rate`, and `time`
3. Compute `interest` using the formula: `interest = (principal * rate *
time) / 100`
4. Print `interest`
5. End

C Code:
#include <stdio.h>
int main() {
float principal, rate, time, interest;
printf("Enter principal, rate and time: ");
scanf("%f %f %f", &principal, &rate, &time);
interest = (principal * rate * time) / 100;
printf("Simple Interest: %.2f\n", interest);
return 0;
}
10. Fibonacci Sequence (First 10 Numbers)
Algorithm:
1. Start
2. Initialize `n1` to 0 and `n2` to 1
3. For `i` from 0 to 9:
- If `i` is less than or equal to 1, set `next` to `i`
- Otherwise, set `next` to `n1 + n2`
- Update `n1` to `n2` and `n2` to `next`
- Print `next`
4. End

C Code:
#include <stdio.h>
int main() {
int n1 = 0, n2 = 1, next, i;
printf("Fibonacci Series: \n");
for (i = 0; i < 10; i++) {
if (i <= 1) {
next = i;
} else {
next = n1 + n2;
n1 = n2;
n2 = next;
}
printf("%d ", next);
}
printf("\n");
return 0;
}
11. Calculate Power of a Number
Algorithm:
1. Start
2. Read the base `x` and exponent `n`
3. Initialize `result` to 1
4. For `i` from 1 to `n`:
- Multiply `result` by `x`
5. Print `result`
6. End
C Code:
#include <stdio.h>
int main() {
int x, n;
long long result = 1;
printf("Enter base and exponent: ");
scanf("%d %d", &x, &n);
for (int i = 1; i <= n; i++) {
result *= x;
}
printf("%d^%d = %lld\n", x, n, result);
return 0;
}
12. Check Prime Number
Algorithm:
1. Start
2. Read an integer `num`
3. If `num` is less than 2, print "Not prime"
4. For `i` from 2 to `num/2
- If `num` is divisible by `i`, print "Not prime" and exit
5. Print "Prime"
6. End
C Code:
#include <stdio.h>
int main() {
int num, is_prime = 1;
printf("Enter an integer: ");
scanf("%d", &num);
if (num < 2) {
is_prime = 0;
} else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
is_prime = 0;
break;
}
}
}
if (is_prime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
```

13. Count Digits in a Number


Algorithm:
1. Start
2. Read an integer `num`
3. Initialize `count` to 0
4. While `num` is greater than 0:
- Divide `num` by 10
- Increment `count`
5. Print `count`
6. End

C Code:
#include <stdio.h>

int main() {
int num, count = 0;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0) {
num /= 10;
count++;
}
printf("Number of digits: %d\n", count);
return 0;
}

14. Swap Two Numbers Without Temp Variable


Algorithm:
1. Start
2. Read two integers `a` and `b`
3. Use arithmetic operations to swap `a` and `b`:
- `a = a + b`
- `b = a - b`
- `a = a - b`
4. Print `a` and `b`
5. End
C Code:
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

a = a + b;
b = a - b;
a = a - b;

printf("After swapping: a = %d, b = %d\n", a, b);


return 0;
}

15. Find GCD of Two Numbers


Algorithm:
1. Start
2. Read two integers `a` and `b`
3. While `b` is not 0:
- Compute `a % b` and assign to `b`
- Swap `a` and `b`
4. Print `a` (which is the GCD)
5. End

C Code:
#include <stdio.h>

int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}

printf("GCD is %d\n", a);


return 0;
}
16. Print Multiplication Table
Algorithm:
1. Start
2. Read an integer `num`
3. For `i` from 1 to 10:
- Print the product of `num` and `i`
4. End

C Code:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

for (int i = 1; i <= 10; i++) {


printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
17. Check Palindrome Number
Algorithm:
1. Start
2. Read an integer `num`
3. Initialize `reversed` to 0 and `original` to `num`
4. While `num` is not 0:
- Find the last digit of `num` and store it in `remainder`
- Update `reversed` by adding `remainder`
- Remove the last digit from `num`
5. If `reversed` equals `original`, print "Palindrome"
6. Otherwise, print "Not Palindrome"
7. End
C Code:
#include <stdio.h>

int main() {
int num, reversed = 0, original, remainder;
printf("Enter an integer: ");
scanf("%d", &num);

original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

if (reversed == original) {
printf("%d is a palindrome.\n", original);
} else {
printf("%d is not a palindrome.\n", original);
}
return 0;
}
18. Sum of Digits of a Number
Algorithm:
1. Start
2. Read an integer `num`
3. Initialize `sum` to 0
4. While `num` is greater than 0:
- Find the last digit of `num` and add it to `sum`
- Remove the last digit from `num`
5. Print `sum`
6. End

C Code:
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0) {
sum += num % 10;
num /= 10;
}
printf("Sum of digits: %d\n", sum);
return 0;
}

19. Find Largest Element in an Array


Algorithm:
1. Start
2. Read the number of elements `n` and the array `arr`
3. Initialize `largest` to the first element of `arr`
4. For each element in `arr`:
- If the element is greater than `largest`, update `largest`
5. Print `largest`
6. End
C Code:
#include <stdio.h>

int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int largest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
printf("Largest element is %d\n", largest);
return 0;
}
20. Bubble Sort Algorithm
Algorithm:
1. Start
2. Read the number of elements `n` and the array `arr`
3. For each pass from 0 to `n-1`:
- For each element from 0 to `n-i-2`:
- If the current element is greater than the next element, swap
them
4. Print the sorted array
5. End

C Code:
#include <stdio.h>

int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
https://www.youtube.com/watch?v=9I2oOAr2okY

You might also like