0% found this document useful (0 votes)
6 views16 pages

D0.docx

Uploaded by

Anushka Shivade
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)
6 views16 pages

D0.docx

Uploaded by

Anushka Shivade
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/ 16

Data Structures and Algorithms

U.no : UEC2023156

#Practice program in C:
1. If number is prime or not
2. Calc area of circle
3. Find square root of a number
4. Calc sum of first 50 prime numbers
5. Factorial of n
6. Generate Fibonacci series
7. Swap two variables without using temporary variable
8. Print the patterns

9. Calculator.
10. Find count of numbers
11. Check palindrome
Q.1 to Q.7
#include<stdio.h>
#include<math.h>
#include <stdbool.h>
bool is_prime(int n){
if (n%2==0){
return false;
}
else{
return true;
}
}
int factorial(int n){
if (n==1|| n==0){
return 1;
}
return n*factorial(n-1);
}
void printFibonacci(int n) {
int a = 0, b = 1, c;

if (n < 1) {
return;
}

printf("%d", a);

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


printf(", %d", b);
c = a + b;
a = b;
b = c;
}

printf("\n");
}
int main(){
//Print Even or Odd
printf("even or odd: \n");
int a;
printf("Enter a number:");
scanf("%d",&a);

if(a%2==0){
printf("Even\n");
}else{
printf("Odd\n");
}

//Find if the number is prime or not


printf("Prime or not\n");
int b, count=0;
printf("Enter a number: ");
scanf("%d",&b);

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


if (i%b==0){
count++;
}
}
if(count==1){
printf("Prime\n");
}else{
printf("Not Prime\n");
}
//Find the area of a circle
printf("Calculate area of circle\n");
float r;
printf("Enter the radius: ");
scanf("%f",&r);

float area = 3.14*r*r;


printf("Area = %.2f\n",area);

//Find Square root of a number


printf("Find square root\n");
int c;
printf("Enter a number: ");
scanf("%d",&c);
float square_root = sqrt(c);
printf("Square Root = %.2f\n",square_root);

//Find the sum of first 50 prime numbers


printf("Sum of first 50 prime numbers\n");
int i =2;
int sum =0;
int counter =0;
while(counter<=50){
if(is_prime(i)){
counter++;
}
sum+=i;
i++;

}
printf("Sum= %d\n",sum);

//Find the factorial of a number


printf("Find factorial of:\n");
int n;
printf("Enter a number: ");
scanf("%d",&n);

//Fibbo Series
int factorial_of_n = factorial(n);
printf("Factorial = %d",factorial_of_n);

printf("Fibbonacci Series\n")
int n1;
printf("Enter a number: ")
scanf("%d",&n1)
printFibonacci(n1)
return 0;
}
OUTPUT
even or odd:
Enter a number:458
Even
Prime or not
Enter a number: 954
Not Prime
Calculate area of circle
Enter the radius: 6.4
Area = 128.61
Find square root
Enter a number: 25
Square Root = 5.00
Sum of first 50 prime numbers
Sum= 5355
Find factorial of:
Enter a number:
8
Factorial = 40320
Fibbonacci Series
Enter a number:
7
0, 1, 1, 2, 3, 5, 8

=== Code Execution Successful ===


Q8. Printing Patterns
#printing patterns1
#include<stdio.h>

int main(){
for(int i=0; i<5; i++){
for(int j =1; j<=i+1; j++){
printf("%d\t",j);
}
printf("\n");
}
return 0;
}

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#printing pattern 2
#include<stdio.h>

int main(){
int k=1;
for(int i=0; i<4; i++){
for(int j =1; j<=i+1; j++){
printf("%d\t",k++);
}
printf("\n");
}
return 0;
}

1
2 3
4 5 6
7 8 9 10
#printing pattern3
#include<stdio.h>

int main(){
char k = 'A';
for(int i=0; i<4; i++){
for(int j =1; j<=i+1; j++){
printf("%c\t",k++);
}
printf("\n");
}
return 0;
}

A
B C
D E F
G H I J
#printing pattern4
#include<stdio.h>

int main(){
int n = 3;
for(int i=0; i<3; i++){
for(int spaces =0;spaces< n-i; spaces++){
printf(" ");
}
for(int j=0; j<=i;j++){
printf("* ");

}
printf("\n");

}
return 0;
}

*
* *
* * *
Q. 9
#include<stdio.h>

#include<stdlib.h>

int main(){

//Calculator

int a ,b, choice;

printf("Enter first number: ");

scanf("%d", &a);

printf("\n");

printf("Enter second number: ");

scanf("%d", &b);

while(1){

printf("MENU\n1. Add\n2. Subtract\n3. Multiply\n4.


Divide\n5.Exit\n");

scanf("%d", &choice);

switch(choice){

case 1:{

int sum = a+b;

printf("Sum = %d\n", sum);

break;

case 2:{

int sub = a-b;

printf("Subtraction = %d\n", sub);

break;

case 3:{
float mul = a*b;

printf("Multiply = %f\n", mul);

break;

case 4:{

float div = a/b;

printf("Division = %f\n", div);

break;

case 5:{

printf("Exiting program...\n");

exit(0);

default:{

printf("Invalid choice\n");

return 0;

}
Enter first number: 56

Enter second number: 4


MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5.Exit
1
Sum = 60
MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5.Exit
2
Subtraction = 52
MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5.Exit
3
Multiply = 224.000000
MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5.Exit
4
Division = 14.000000
MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5.Exit
5
Exiting program…
Q.10
#include<stdio.h>

#include<stdlib.h>

int main(){

//calculate digit count

int number, count =0;

printf("Enter the number: ");

scanf("%d", &number);

printf("\n");

if(number == 0){

count = 1;

printf("Count = %d", count);

while(number != 0){

number = number/10;

count++;

printf("Count = %d", count);

return 0;

Enter the number: 5463


Count = 4
Q.11
#include<stdio.h>

#include <stdbool.h>

int main(){

//Palindrome

int n;

bool match = true;

printf("Enter the length of word: ");

scanf("%d", &n);

char word[n], rev_word[n];

printf("Enter the word: \n");

scanf("%s",word);

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

rev_word[i] = word[n-i-1];

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

if(rev_word[i] == word[i]){

continue;

else if(rev_word[i] != word[i]){

match = false;

}
if(match == true){

printf("Is a Palindrome\n");

else{

printf("Is not a Palindrome\n");

return 0;

Enter the length of word: 5


Enter the word:
madam
Is a Palindrome

Enter the length of word: 6


Enter the word:
bottle
Is not a Palindrome

You might also like