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

C Programs

c programming solutions

Uploaded by

pullagalk
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)
20 views

C Programs

c programming solutions

Uploaded by

pullagalk
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/ 5

1.

Write a function in C and Python to calculate the Fibonacci sequence up to the


7th term.

#include <stdio.h>

int main(void) {
int term = 7;
printf(" %d ", fibonocci_term(term));
}

int fibonocci_term(int term){


int n1 = 0, n2 = 1;
int i;
int n3;
if(term == 1){
return 0;
}
if(term == 2){
return 1;
}
for (i = 3; i <= term; i++)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
}
return n3;
}

2. Write a program in both C and Python to perform bitwise manipulation, such as


checking if a number is even or odd, without using conditional statements.
#include <stdio.h>

int main(){
int num;
int no = 1;
printf("enter a number :");
scanf("%d", & num);

if(num & no == 1){


printf("%d is odd", num);
}
else{
printf("%d is even", num);
}
return 0;
}

OUTPUT
enter a number :7
7 is odd
3. Implement a function in C that uses pointers to reverse an array in place.
Then, write an equivalent function in Python.

#include <stdio.h>
void reverse_array(int numbers[], int length);

int main(){
int nums[12] = {1,3,5,6,7,8,11,22,33,44,55,66};
int num = 12;
int i = 0;
printf("Array contents before reverse is :\n");
for (i = 0; i < num; i++){
printf("%d \n", nums[i]);
}
reverse_array(nums, num);

printf("Array contents after reverse function is called :\n");


for (i = 0; i < num; i++){
printf("%d \n", nums[i]);
}

void reverse_array(int numbers[], int length){


int temp;
int st = 0;
int end = length - 1;
while ( st < end){
temp = numbers[st];
numbers[st] = numbers[end];
numbers[end] = temp;
st++;
end--;
}
return;
}

OUTPUT
Array contents before reverse is :
1
3
5
6
7
8
11
22
33
44
55
66
Array contents after reverse function is called :
66
55
44
33
22
11
8
7
6
5
3
1

4. Implement the Armstrong number check using both a recursive and an iterative
approach in both C and Python.

https://www.geeksforgeeks.org/program-for-armstrong-numbers/

5. Write a program in C and Python to compare two strings without using string
library functions.
#include <stdio.h>
int my_strcmp(char *s1, char *s2);
int main(){
char str1[] = "aello";
char str2[] = "abcd";

int m = my_strcmp(str1, str2);


printf("%d", m);

}
int my_strcmp(char *s1, char *s2){
while (*s1 == *s2){
s1++;
s2++;

if(*s1 == '\0')
return 0;
}

return *s1 - *s2;


}

OUTPUT
3

8 .Implement a program in C to count the number of vowels in a given string


using pointers. Write an equivalent function in Python without using the
count method.

9. Check whether a given number is super prime? By using C and Python ( For example
7331 is super prime because 7331 is prime, 733 is prime, 73 is prime,7 is prime, Such
numbers are called super prime)

// super prime
#include <stdio.h>
#include <math.h>

int is_prime(int n);

int main(){
int num = 120;
int flag = 0;
while( num > 0){
if(is_prime(num) == 0){
flag = 1;
break;
}
num = num / 10;
}

if(flag == 1){
printf("number is not super prime");
}
else
printf("number is super prime");
}

int is_prime(int n){


int i = 0;
int flag = 0;
for (i = 2; i < sqrt(n); i ++){

if(n % i == 0)
{
flag = 1;
break;
}
}
if (flag ==1)
return 0;
else
return 1;
}

10. Implement the Sieve of Eratosthenes algorithm in both C and Python to generate
prime numbers up to a large value
https://www.geeksforgeeks.org/c-program-to-implement-sieve-of-eratosthenes/

19. Write a program in both C and Python to implement a recursive function that
calculates the factorial of a number. Compare the memory usage and execution speed
of both programs when calculating large factorials (e.g., factorial of 100). Why might C
handle this task differently from Python, and how does memory management in each
language affect the result?

#include <stdio.h>
int main()
{
int num = 5;
printf("%d", factorial(num));
}

int factorial(int num){


if (num == 1)
return 1;

return num * factorial (num - 1);


}

You might also like