isRightangle triangle
isRight angle triangle program by giving 3 points (Point structure already given)
(assume Point structure already given)
double point_calculatedistance(Point *p1,Point *p2)
{
int gdistance = ((p2->x2-p1->x1)*(p2->x2-p1->x1))+((p2->y2-p1->y1)*(p2->y2-p2->y1));
double val=sqrt(gdistance);
return val;
}
int isRightangleTriangle(Point *p1,Point *p2,Point*p3)
{
double a,b,c;
a=point_calculatedistance(p1,p2);
b=point_calculatedistance(p2,p3);
acpoint_calculatedistance(p3,p1);
if (a + b <= c || a + c <= b || b + c <= a)
return 0;
else
return 1;
}
-----------------------------------------------------------
manchester program
January 20, 2019
#include<stdio.h>
#include<stdlib.h>
int* manchester(int *arr,int len)
{
int* res=(int*)malloc(sizeof(int)*len);
arr[-1]=0;
for(int i=0;i<len;i++)
{
res[i]=arr[i]==arr[i-1]?0:1;
}
return res;
}
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int* x=manchester(a,n);
for(i=0;i<n;i++)
printf("%d ",x[i]);
}
input:
8
0 1 0 0 1 1 1 0
output:
0 1 1 0 1 0 0 1
-----------------------------------------------------------------
check weather sum of digits of minimum element in the array is even or odd
January 20, 2019
// if even print 1 otherwise print 0
#include<stdio.h>
int get(int *arr,int n){
int min,i,rem,c=0;
min=arr[0];
for(i=1;i<n;i++)
if(arr[i]<min)
min=arr[i];
while(min!=0){
rem=min%10;
c=c+rem;
min=min/10;
}
if(c%2==0)
return 1;
else
return 0;
}
void main(){
int arr[100],n,i,j,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
sum=get(arr,n);
printf("%d",sum);
}
Input:
6
62 13 21 55 61 72
Output:
1
-----------------------------------
pattern program
January 20, 2019
void main()
{
int r,c,n,i;
scanf("%d",&n);
for(r=1;r<=n;r++)
{
for(c=1;c<=r;c++)
{
if(r%2==0)
//printf("%d",r/2);
printf("0");
else
//printf("%c",'A'+r/2);
printf("1");
}
printf("\n");
}
}
input: 5
output:
1
00
111
0000
11111
-----------------------------------------------------------
find pythagorean triplets
January 20, 2019
#include<stdio.h>
void pytriplets(int);
int main()
{
int limit;
scanf("%d",&limit);
pytriplets(limit);
return 0;
}
void pytriplets(int limit)
{
int a,b,c=0,n;
int m=2;
while(c<limit)
{
for(n=1;n<m;n++)
{
a=m*m-n*n;
b=2*m*n;
c=m*m+n*n;
if(c>limit)
break;
printf("%d %d %d\n",a,b,c);
}
m++;
}
}
input:
10
output
3 4 5
8 6 10
-------------------------------------------------------------
getday of the week
January 20, 2019
#include <stdio.h>
char *getday(int num);
int main()
{
int num=4;
char* res;
res=getday(num);
printf("%s",res);
}
char *getday(int num)
{
char *str;
switch(num)
{
case 1:str="monday";
break;
case 2:str="monday";
break;
case 3:str="monday";
break;
case 4:str="monday";
break;
case 5:str="monday";
break;
case 6:str="monday";
break;
case 7:str="monday";
break;
default: printf("invalid");
break;
}
return str;
}
output:
monday
-----------------------------------------------------------
print prime numbers in the range
January 20, 2019
#include<stdio.h>
void printPrime(int,int);
int main()
{
int num,digit;
scanf("%d %d",&num,&digit);
printPrime(num,digit);
return 0;
}
void printPrime(int num,int digit)
{
int i;
while(num>=digit)
{
int flag=0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
printf(" %d",num);
num--;
}
}
input:
25 3
output:
23 19 17 13 11 7 5 3
------------------------------------------------------------
print even or odd pattern
January 20, 2019
#include <stdio.h>
void printpattern(int);
int main()
{
int num;
scanf("%d",&num);
printpattern(num);
}
void printpattern(int num)
{
int i,print=0;
if(num%2==0)
{
print=0;
for(i=0;i<num;i++)
{
printf("%d ",print);
print+=2;
}
}
else
{
print=1;
for(i=0;i<num;i++)
{
printf("%d ", print);
print+=2;
}
}
}
input:
11
ouput:
1 3 5 7 9 11 13 15 17 19 21
--------------------------------------------------------------------
print palindrome in the range
January 20, 2019
#include <stdio.h>
void printpalindrome(int,int);
int main()
{
int num,digit;
scanf("%d%d",&num,&digit);
printpalindrome(num,digit);
}
void printpalindrome(int num,int digit)
{
int i,ds=1;
for(i=1;i<=digit;i++)
ds*=10;
for(i=num;i<ds;i++)
{
int temp=i,rem;
int rev=0;
while(temp)
{
rem=temp%10;
rev=(rev*10)+rem;
temp=temp/10;
}
if(i==rev)
printf("%d ",i);
}
}
input:
793 3
output:
797 808 818 828 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979 989 999
----------------------------------------------------------------
find LCM of n numbers
January 20, 2019
#include<stdio.h>
int caluculategenerallcm(int arr[],int);
int caluculatelcm(int,int);
int main()
{
int arr[50],n,res,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
res=caluculategenerallcm(arr,n);
printf("%d",res);
}
int caluculatelcm(int a,int b)
{
int lcm=a;
while(1)
{
if(lcm%b==0&&lcm%a==0)
{
break;
}
lcm++;
}
return lcm;
}
int caluculategenerallcm(int *arr,int len)
{
int i,m;
int l=arr[0];
for(i=1;i<len;i++)
{
m=arr[i];
l=caluculatelcm(l,m);
}
return l;
}
input:
5
10 20 30 40 50
output:
600
---------------------------------------------------------
find greatest factor of a given number
January 20, 2019
#include <stdio.h>
int caluculatefactor(int number);
int checkgreatestfactor(int num);
int main()
{
int num,res;
scanf("%d",&num);
res=checkgreatestfactor(num);
printf("%d ",res);
}
int caluculatefactor(int number)
{
int i=0,maxfactor=0;
for(i=1;i<=number/2;i++)
{
if(number%i==0)
maxfactor=i;
}
return maxfactor;
}
int checkgreatestfactor(int num)
{
if(num==0)
return 0;
else
return (caluculatefactor(num));
}
input:
100
output:
50
-----------------------------------------------------------
removal vowels in the string
January 20, 2019
#include <stdio.h>
#include<string.h>
int main()
{
char str[100];
int i;
gets(str); // fgets(str,100,stdin);
for(i=0;str[i]!='\0';i++)
{
if(str[i]!='U'&&str[i]!='O'&&str[i]!='I'&&str[i]!='E'&&str[i]!='A'&&str[i]!='a'&&str[i]!='e'&&str[i]!='o'&&str[i]!='i'&&str[i]!='u')
{
printf("%c",str[i]);
}
}
getchar();
}
input:
good morning
output:
gd mrnng
-------------------------------------------------------
sum of squares of elements in the given array
January 20, 2019
#include<stdio.h>
int sqarr(int);
int main()
{
int a[50],n,i,j,res,sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
{
res=sqarr(a[i]);
sum=sum+res;
}
printf("%d ",sum);
}
int sqarr(int n)
{
int a;
a=n*n;
return a;
}
input:
5
1 2 3 4 5
output:
55
-------------------------------------------------------
exponent of a number
January 20, 2019
#include<stdio.h>
int positiveexponent(int base,int exponent);
float allexponent(int base,int exponent);
int main() {
int base, exponent;
scanf("%d%d",&base,&exponent);
allexponent(base,exponent);
}
float allexponent(int base,int exponent){
if(exponent>0){
printf("%d",positiveexponent(base,exponent));
}
else{
printf("%f",1/(float)positiveexponent(base,exponent*-1));
}
}
int positiveexponent(int base,int exponent){
if(exponent==1){
return base*exponent;
}
else{
return base*positiveexponent(base,exponent-1);
}
}
input:
2 3
output:
8
-----------------------------------------------------------
reverse of a sentence
January 20, 2019
#include <stdio.h>
#include<string.h>
int main()
{
char str[100];
int i,len;
gets(str);
len=strlen(str);
for(i=len-1;i>=0;i--)
{
printf("%c", str[i]);
}
}
input:
good morning
output:
gninrom doog
isRight angle triangle program by giving 3 points (Point structure already given)
(assume Point structure already given)
double point_calculatedistance(Point *p1,Point *p2)
{
int gdistance = ((p2->x2-p1->x1)*(p2->x2-p1->x1))+((p2->y2-p1->y1)*(p2->y2-p2->y1));
double val=sqrt(gdistance);
return val;
}
int isRightangleTriangle(Point *p1,Point *p2,Point*p3)
{
double a,b,c;
a=point_calculatedistance(p1,p2);
b=point_calculatedistance(p2,p3);
acpoint_calculatedistance(p3,p1);
if (a + b <= c || a + c <= b || b + c <= a)
return 0;
else
return 1;
}
-----------------------------------------------------------
manchester program
January 20, 2019
#include<stdio.h>
#include<stdlib.h>
int* manchester(int *arr,int len)
{
int* res=(int*)malloc(sizeof(int)*len);
arr[-1]=0;
for(int i=0;i<len;i++)
{
res[i]=arr[i]==arr[i-1]?0:1;
}
return res;
}
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int* x=manchester(a,n);
for(i=0;i<n;i++)
printf("%d ",x[i]);
}
input:
8
0 1 0 0 1 1 1 0
output:
0 1 1 0 1 0 0 1
-----------------------------------------------------------------
check weather sum of digits of minimum element in the array is even or odd
January 20, 2019
// if even print 1 otherwise print 0
#include<stdio.h>
int get(int *arr,int n){
int min,i,rem,c=0;
min=arr[0];
for(i=1;i<n;i++)
if(arr[i]<min)
min=arr[i];
while(min!=0){
rem=min%10;
c=c+rem;
min=min/10;
}
if(c%2==0)
return 1;
else
return 0;
}
void main(){
int arr[100],n,i,j,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
sum=get(arr,n);
printf("%d",sum);
}
Input:
6
62 13 21 55 61 72
Output:
1
-----------------------------------
pattern program
January 20, 2019
void main()
{
int r,c,n,i;
scanf("%d",&n);
for(r=1;r<=n;r++)
{
for(c=1;c<=r;c++)
{
if(r%2==0)
//printf("%d",r/2);
printf("0");
else
//printf("%c",'A'+r/2);
printf("1");
}
printf("\n");
}
}
input: 5
output:
1
00
111
0000
11111
-----------------------------------------------------------
find pythagorean triplets
January 20, 2019
#include<stdio.h>
void pytriplets(int);
int main()
{
int limit;
scanf("%d",&limit);
pytriplets(limit);
return 0;
}
void pytriplets(int limit)
{
int a,b,c=0,n;
int m=2;
while(c<limit)
{
for(n=1;n<m;n++)
{
a=m*m-n*n;
b=2*m*n;
c=m*m+n*n;
if(c>limit)
break;
printf("%d %d %d\n",a,b,c);
}
m++;
}
}
input:
10
output
3 4 5
8 6 10
-------------------------------------------------------------
getday of the week
January 20, 2019
#include <stdio.h>
char *getday(int num);
int main()
{
int num=4;
char* res;
res=getday(num);
printf("%s",res);
}
char *getday(int num)
{
char *str;
switch(num)
{
case 1:str="monday";
break;
case 2:str="monday";
break;
case 3:str="monday";
break;
case 4:str="monday";
break;
case 5:str="monday";
break;
case 6:str="monday";
break;
case 7:str="monday";
break;
default: printf("invalid");
break;
}
return str;
}
output:
monday
-----------------------------------------------------------
print prime numbers in the range
January 20, 2019
#include<stdio.h>
void printPrime(int,int);
int main()
{
int num,digit;
scanf("%d %d",&num,&digit);
printPrime(num,digit);
return 0;
}
void printPrime(int num,int digit)
{
int i;
while(num>=digit)
{
int flag=0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
printf(" %d",num);
num--;
}
}
input:
25 3
output:
23 19 17 13 11 7 5 3
------------------------------------------------------------
print even or odd pattern
January 20, 2019
#include <stdio.h>
void printpattern(int);
int main()
{
int num;
scanf("%d",&num);
printpattern(num);
}
void printpattern(int num)
{
int i,print=0;
if(num%2==0)
{
print=0;
for(i=0;i<num;i++)
{
printf("%d ",print);
print+=2;
}
}
else
{
print=1;
for(i=0;i<num;i++)
{
printf("%d ", print);
print+=2;
}
}
}
input:
11
ouput:
1 3 5 7 9 11 13 15 17 19 21
--------------------------------------------------------------------
print palindrome in the range
January 20, 2019
#include <stdio.h>
void printpalindrome(int,int);
int main()
{
int num,digit;
scanf("%d%d",&num,&digit);
printpalindrome(num,digit);
}
void printpalindrome(int num,int digit)
{
int i,ds=1;
for(i=1;i<=digit;i++)
ds*=10;
for(i=num;i<ds;i++)
{
int temp=i,rem;
int rev=0;
while(temp)
{
rem=temp%10;
rev=(rev*10)+rem;
temp=temp/10;
}
if(i==rev)
printf("%d ",i);
}
}
input:
793 3
output:
797 808 818 828 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979 989 999
----------------------------------------------------------------
find LCM of n numbers
January 20, 2019
#include<stdio.h>
int caluculategenerallcm(int arr[],int);
int caluculatelcm(int,int);
int main()
{
int arr[50],n,res,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
res=caluculategenerallcm(arr,n);
printf("%d",res);
}
int caluculatelcm(int a,int b)
{
int lcm=a;
while(1)
{
if(lcm%b==0&&lcm%a==0)
{
break;
}
lcm++;
}
return lcm;
}
int caluculategenerallcm(int *arr,int len)
{
int i,m;
int l=arr[0];
for(i=1;i<len;i++)
{
m=arr[i];
l=caluculatelcm(l,m);
}
return l;
}
input:
5
10 20 30 40 50
output:
600
---------------------------------------------------------
find greatest factor of a given number
January 20, 2019
#include <stdio.h>
int caluculatefactor(int number);
int checkgreatestfactor(int num);
int main()
{
int num,res;
scanf("%d",&num);
res=checkgreatestfactor(num);
printf("%d ",res);
}
int caluculatefactor(int number)
{
int i=0,maxfactor=0;
for(i=1;i<=number/2;i++)
{
if(number%i==0)
maxfactor=i;
}
return maxfactor;
}
int checkgreatestfactor(int num)
{
if(num==0)
return 0;
else
return (caluculatefactor(num));
}
input:
100
output:
50
-----------------------------------------------------------
removal vowels in the string
January 20, 2019
#include <stdio.h>
#include<string.h>
int main()
{
char str[100];
int i;
gets(str); // fgets(str,100,stdin);
for(i=0;str[i]!='\0';i++)
{
if(str[i]!='U'&&str[i]!='O'&&str[i]!='I'&&str[i]!='E'&&str[i]!='A'&&str[i]!='a'&&str[i]!='e'&&str[i]!='o'&&str[i]!='i'&&str[i]!='u')
{
printf("%c",str[i]);
}
}
getchar();
}
input:
good morning
output:
gd mrnng
-------------------------------------------------------
sum of squares of elements in the given array
January 20, 2019
#include<stdio.h>
int sqarr(int);
int main()
{
int a[50],n,i,j,res,sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
{
res=sqarr(a[i]);
sum=sum+res;
}
printf("%d ",sum);
}
int sqarr(int n)
{
int a;
a=n*n;
return a;
}
input:
5
1 2 3 4 5
output:
55
-------------------------------------------------------
exponent of a number
January 20, 2019
#include<stdio.h>
int positiveexponent(int base,int exponent);
float allexponent(int base,int exponent);
int main() {
int base, exponent;
scanf("%d%d",&base,&exponent);
allexponent(base,exponent);
}
float allexponent(int base,int exponent){
if(exponent>0){
printf("%d",positiveexponent(base,exponent));
}
else{
printf("%f",1/(float)positiveexponent(base,exponent*-1));
}
}
int positiveexponent(int base,int exponent){
if(exponent==1){
return base*exponent;
}
else{
return base*positiveexponent(base,exponent-1);
}
}
input:
2 3
output:
8
-----------------------------------------------------------
reverse of a sentence
January 20, 2019
#include <stdio.h>
#include<string.h>
int main()
{
char str[100];
int i,len;
gets(str);
len=strlen(str);
for(i=len-1;i>=0;i--)
{
printf("%c", str[i]);
}
}
input:
good morning
output:
gninrom doog
No comments:
Post a Comment