0% found this document useful (0 votes)
3 views

c-to-python-programs

The document contains a series of C programming examples, each demonstrating a fundamental programming concept such as printing 'Hello, World!', calculating the sum of two numbers, and checking for prime numbers. Each example includes the C code along with a commented-out equivalent in Python. The document serves as a basic introduction to programming concepts for beginners.

Uploaded by

dhruvvasvani624
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

c-to-python-programs

The document contains a series of C programming examples, each demonstrating a fundamental programming concept such as printing 'Hello, World!', calculating the sum of two numbers, and checking for prime numbers. Each example includes the C code along with a commented-out equivalent in Python. The document serves as a basic introduction to programming concepts for beginners.

Uploaded by

dhruvvasvani624
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// 1.

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

// 2. Sum of Two Numbers


#include <stdio.h>
int main() {
int a = 5, b = 10, sum;
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
// Python: a, b = 5, 10; sum = a + b; print(f"Sum: {sum}")
}

// 3. Area of a Circle
#include <stdio.h>
#define PI 3.14159
int main() {
float radius = 5.0;
float area = PI * radius * radius;
printf("Area: %.2f\n", area);
return 0;
// Python: import math; radius = 5.0; area = math.pi * radius ** 2;
print(f"Area: {area:.2f}")
}

// 4. Simple Interest Calculation


#include <stdio.h>
int main() {
float principal = 1000.0;
float rate = 5.0;
float time = 2.0;
float interest = (principal * rate * time) / 100;
printf("Simple Interest: %.2f\n", interest);
return 0;
// Python: principal, rate, time = 1000.0, 5.0, 2.0; interest = (principal *
rate * time) / 100; print(f"Simple Interest: {interest:.2f}")
}

// 5. Swap Two Numbers Without Third Variable


#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
// Python: a, b = 10, 20; print(f"Before swap: a = {a}, b = {b}"); a, b = b, a;
print(f"After swap: a = {a}, b = {b}")
}

// 6. Check Even or Odd


#include <stdio.h>
int main() {
int num = 7;
if (num % 2 == 0)
printf("%d is even\n", num);
else
printf("%d is odd\n", num);
return 0;
// Python: num = 7; print(f"{num} is {'even' if num % 2 == 0 else 'odd'}")
}

// 7. Find Maximum of Two Numbers


#include <stdio.h>
int main() {
int a = 10, b = 20;
int max = (a > b) ? a : b;
printf("Maximum: %d\n", max);
return 0;
// Python: a, b = 10, 20; max = a if a > b else b; print(f"Maximum: {max}")
}

// 8. Factorial Calculation
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d: %d\n", num, factorial(num));
return 0;
// Python: def factorial(n): return 1 if n == 0 or n == 1 else n * factorial(n
- 1); num = 5; print(f"Factorial of {num}: {factorial(num)}")
}

// 9. Fibonacci Sequence
#include <stdio.h>
void fibonacci(int n) {
int first = 0, second = 1, next;
for (int i = 0; i < n; i++) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
}
int main() {
fibonacci(10);
return 0;
// Python: def fibonacci(n): a, b = 0, 1; [print(a, end=' ') or (a, b) := (b,
a+b) for _ in range(n)]
}

// 10. Prime Number Check


#include <stdio.h>
int isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return 0;
}
return 1;
}
int main() {
int num = 17;
if (isPrime(num))
printf("%d is prime\n", num);
else
printf("%d is not prime\n", num);
return 0;
// Python: def is_prime(num): return num > 1 and all(num % i != 0 for i in
range(2, int(num**0.5) + 1)); num = 17; print(f"{num} is {'prime' if is_prime(num)
else 'not prime'}")
}

// 11. Sum of Array Elements


#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
printf("Sum: %d\n", sum);
return 0;
// Python: arr = [1, 2, 3, 4, 5]; print(f"Sum: {sum(arr)}")
}

// 12. Linear Search


#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) return i;
}
return -1;
}
int main() {
int arr[] = {4, 2, 7, 1, 5};
int key = 7;
int result = linearSearch(arr, 5, key);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
// Python: arr = [4, 2, 7, 1, 5]; key = 7; print(f"Element found at index
{arr.index(key)}" if key in arr else "Element not found")
}

// 13. String Length


#include <stdio.h>
int stringLength(char *str) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
char str[] = "Hello";
printf("Length: %d\n", stringLength(str));
return 0;
// Python: str = "Hello"; print(f"Length: {len(str)}")
}

// 14. Palindrome Check


#include <stdio.h>
#include <string.h>
int isPalindrome(char *str) {
int left = 0;
int right = strlen(str) - 1;
while (left < right) {
if (str[left] != str[right]) return 0;
left++;
right--;
}
return 1;
}
int main() {
char str[] = "radar";
if (isPalindrome(str))
printf("%s is a palindrome\n", str);
else
printf("%s is not a palindrome\n", str);
return 0;
// Python: str = "radar"; print(f"{str} is {'a' if str == str[::-1] else 'not
a'} palindrome")
}

// 15. Armstrong Number


#include <stdio.h>
#include <math.h>
int isArmstrong(int num) {
int original = num, remainder, result = 0;
int n = log10(num) + 1;
while (num > 0) {
remainder = num % 10;
result += pow(remainder, n);
num /= 10;
}
return (result == original);
}
int main() {
int num = 153;
if (isArmstrong(num))
printf("%d is an Armstrong number\n", num);
else
printf("%d is not an Armstrong number\n", num);
return 0;
// Python: def is_armstrong(num): return sum(int(digit) ** len(str(num)) for
digit in str(num)) == num; num = 153; print(f"{num} is {'an' if is_armstrong(num)
else 'not an'} Armstrong number")
}

// More programs would follow in the same pattern...


// Would you like me to continue with more programs or is this a good start?

You might also like