0% found this document useful (0 votes)
2 views8 pages

(DS) Data Structure Practical File

The document contains multiple C programs that demonstrate various functionalities including checking if a number is prime, palindrome, Armstrong, finding the largest among three numbers, and determining if a number is even or odd. Each section includes code examples for both default values and user input. The programs utilize basic control structures and mathematical operations to achieve their objectives.

Uploaded by

krishsetia67
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)
2 views8 pages

(DS) Data Structure Practical File

The document contains multiple C programs that demonstrate various functionalities including checking if a number is prime, palindrome, Armstrong, finding the largest among three numbers, and determining if a number is even or odd. Each section includes code examples for both default values and user input. The programs utilize basic control structures and mathematical operations to achieve their objectives.

Uploaded by

krishsetia67
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

Q1. Write a C program to check the number is prime or not .

A. Check prime using a default number.


B. Check prime using a user input .

Code:
#include <stdio.h>
int main() {
int n = 11;
int i, prime = 0;
if (n <= 1)
prime = 1;
for (i = 2; i <= n / 2; i++) {
if (n % i == 0) {
prime = 1;
break;
}}
if (prime == 0)
printf("%d is a Prime Number\n", n);
else
printf("%d is Not a Prime Number\n", n);
return 0;
}

Output:
code:
#include <stdio.h>
#include <math.h>
int main()
{
int num;
int i;
float x=0.0;
int limit;

printf("enter your number=");


scanf("%d",&num);
limit=sqrt(num);
for(i=2;i<=limit;i=i+1){
if(num%i==0){
x=x+1;
}
}
if(x==0){
printf("the number is prime");
}
else if(x>0){
printf("the number is not prime");
}
else{
printf("there is some error");
}
return 0;
}

Output:
Q2. Write a C program to check the number is palindrome.
A. Check palindrome using a default number.
B. Check palindrome using a user input .

Code:
#include <stdio.h>
int main() {
int n = 121; // default number
int i, rev = 0, rem;
i = n; // store original value
while (n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}

if (i == rev)
printf("%d is a Palindrome Number\n", i);
else
printf("%d is Not a Palindrome Number\n", i);

return 0;
}

Output:
Code:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, len, flag = 1;

printf("Enter a string: ");


scanf("%s", str);

len = strlen(str);

for(i = 0; i < len/2; i++) {


if(str[i] != str[len - i - 1]) {
flag = 0;
break;
}
}

if(flag == 1)
printf("Palindrome");
else
printf("Not Palindrome");

return 0;
}

Output:
Q3. Write a program to check whether a number is armstrong or not .

Code:
#include <stdio.h>
int main() {
int n, r, sum = 0, temp;
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while (n > 0) {
r = n % 10;
sum += r * r * r;
n /= 10;
}
if (sum == temp)
printf("Armstrong number");
else
printf("Not Armstrong number");
return 0;
}

Output:
Q4. Write a program to check the largest number among three numbers.

Code:

#include <stdio.h>
int main()
{
float num1,num2,num3;
printf("enter num1=");
scanf("%f",&num1);
printf("enter num2=");
scanf("%f",&num2);
printf("enter num3=");
scanf("%f",&num3);
if(num1>num2&&num1>num3){
printf("%.2f"" is the greatest number among ""%.2f"" and ""%.2f",num1,num2,num3);
}
else if(num2>num1&&num2>num3){
printf("%.2f"" is the greatest number among ""%.2f"" and ""%.2f",num2,num1,num3);
}
else if(num3>num2&&num3>num1){
printf("%.2f"" is the greatest number among ""%.2f"" and ""%.2f",num3,num2,num1);
}
else{
printf("there is some error");
return 1;
}
return 0;
}

Output:
Q5. Write a program to check whether a number is even or odd.
A. Check the number is even.
B. Check the number is odd.

Code:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0)
printf("Number is Even");
else
printf("Number is Odd");
return 0;
}

Output:
Code:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 != 0)
printf("Number is Odd");
else
printf("Number is Even");
return 0;
}

Output:

You might also like