DS Practicals
DS Practicals
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, fact=1,i;
printf("Enter a Number:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The factorial of %d is %d \n",n,fact);
return 0;
}
OUTPUT
Enter a Number:
7
The factorial of 7 is 5040
Process returned 0 (0x0) execution time : 2.121 s
Press any key to continue.
PRIME NUMBERS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j,flag=0;
printf("Enter a range:\n");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{flag=1;
break;
}
}
if(flag==0)
{
printf("The Prime number is:%d\n",i);
}
}
return 0;
}
OUTPUT
Enter a range:20
The Prime number is:2
The Prime number is:3
The Prime number is:5
The Prime number is:7
The Prime number is:11
The Prime number is:13
The Prime number is:17
The Prime number is:19
The Prime number is:23
The Prime number is:29
The Prime number is:31
The Prime number is:37
The Prime number is:37
The Prime number is:41
The Prime number is:43
The Prime number is:47
The Prime number is:53
The Prime number is:59
The Prime number is:61
The Prime number is:67
The Prime number is:71
The Prime number is:73
The Prime number is:79
The Prime number is:83
REVERSED NUMBER
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,rem,rev=0;
printf("Enter a Number:\n");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf("The reversed number is %d\n",rev);
return 0;
}
OUTPUT
Enter a Number:
52
The reversed number is 25
Process returned 0 (0x0) execution time : 2.301 s
Press any key to continue.
PALINDROME
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,p,rem,rev=0;
printf("Enter a number:\n");
scanf("%d",&n);
p=n;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(rev==p)
{
printf("The number %d is palindrome\n",p);
}
else{
printf("The number %d is not palindrome\n",p);
}
return 0;
}
OUTPUT
Enter a number:151
The number is 151 palindrome
PYRAMID
#include <stdlib.h>
int main()
{
int i,j,k;
for(i=1;i<=3;i++)
{
for(j=1;j<=(3-i);j++)
{
printf(" ");
}
for(k=1;k<=((2*i)-1);k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
OUTPUT
*
***
*****
SUM OF DIGITS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,sum,rem;
printf("Enter a Number:\n");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("Sum of digits is %d\n",sum);
return 0;
}
OUTPUT
Enter a Number:
2573
Sum of digits is 17
Swapping of numbers:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
printf("enter two numbers");
scanf("%d %d",&a,&b);
printf("before swaping a=%d and b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nafter swaping a=%d and b=%d",a,b);
return 0;
}
Output:
enter two numbers56
67
before swaping a=56 and b=67
after swaping a=67 and b=56
Process returned 0 (0x0) execution time : 4.888 s
Press any key to continue.
Armstrong number:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,n1,rem,result=0;
printf("enter a number");
scanf("%d",&n);
n1=n;
while(n!=0)
{
rem=n%10;
result=rem*rem*rem+result;
n=n/10;
}
if(result==n1)
{
printf("%d is an armstrong number",n1);
}
else
{
printf("%d is not an armstrong number",n1);
}
return 0;
}
Output:
enter a number153
153 is an armstrong number
enter a number
111
111 is not an armstrong number
Process returned 0 (0x0) execution time : 2.866 s
Press any key to continue.