Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

Factorial----#include<stdio.

h>
#include<conio.h>
void main()
{
int n,i,multi=1;
printf("Enter the Number--");
scanf("%d",&n);
for(i=n;i>0;i--)
{
multi=multi*i;
}
printf("The Factorial of a given number is--%d",multi);
getch();
}
Output:Enter the Number5
The Factorial of a given number is120
Prime no in given range:--#include<stdio.h>
#include<conio.h>
void main()
{
int i, prime, lim_up, lim_low, n;
clrscr();
printf("\n\n\t ENTER THE LOWER LIMIT--:");
scanf("%d", &lim_low);
printf("\n\n\t ENTER THE UPPER LIMIT--: ");
scanf("%d", &lim_up);
printf("\n\n\t PRIME NUMBERS ARE--: ");
for(n=lim_low+1; n<lim_up; n++)
{
prime = 1;
for(i=2; i<n; i++)
if(n%i == 0)
{
prime = 0;
break;
}
if(prime)
printf("\n\n\t\t\t%d", n);
}
getch();
}

Output:ENTER THE LOWER LIMIT--: 1


ENTER THE UPPER LIMIT--: 20

PRIME NUMBERS ARE--:


2
3
5
7
11
13
17
19
Pascle Triangle:#include <stdio.h>
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows to see in pascal triangle:-\n");
scanf("%d",&n);
for ( i = 0 ; i < n ; i++ )
{
for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
printf(" ");
for( c = 0 ; c <= i ; c++ )
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )

result = result*c;
return ( result );
}

Output:Enter the number of rows to see in pascal triangle:5


1
1
1
1
1

1
2

3
4

1
3

1
4

String pallindrom:---#include <stdio.h>


#include <string.h>
#include<conio.h>
int main()
{
char a[100], b[100];
clrscr();
printf("Enter the string to check if it is a palindrome\n");
gets(a);
strcpy(b,a);
strrev(b);
if( strcmp(a,b) == 0 )
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");
getch();
}
Output: Enter the string to check if it is a palindrome

Madam
Entered string is a palindrome.

Hcf and lcm:-#include <stdio.h>


#include<conio.h>
void main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
getch();
}
Output:Enter two integers 4
8
Greatest common divisor of 4 and 8 = 4
Least common multiple of 4 and 8 = 8

BINARY TO DECIMAL
#include<stdio.h>
#include <conio.h>
Void main()
{
int num,bnum,dec=0,base=1,rem;
printf( Enter Binary number(1&0)\n);
scanf(%d,&num);
bnum=num;
while(num>0)
{
rem= num %10;
dec =dec+rem*base;
num=num/10;
base=base *2;
}
printf( The Binary number is =%d,bnum);
printf(Its decimal number is =%d,dec);
getch();
}
Output:Enter a Binary number (1&0)
10101
The Binary number is = 10101
Its decimal number is =21
FIBONACCI SERIES:#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES \n");

printf("\t%d\t%d",a,b);
for(i=0;i<n-2;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}
Output:enter n for how many times generate series 6
FIBONACCI SERIES
0

PERFECT NUMBER:#include<stdio.h>
int main()
{
int n,i,c=0;
printf("\nEnter a no:");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
c=c+i;
}
}
if(n==c)
printf("\nPerfect no.");
else
printf("\nNot a perfect no.");
}
Output:-

13

Enter a no: 6
Perfect no.

You might also like