Wednesday, January 30, 2019

30-01-2019 CTS Practice Coding Programs


  1. lcm,
  2. gcd,
  3. armstrong numbe.
    1. 153370          1^3+5^3+3^3  is 153
  4. ,pallendrome no,
  5. pallendrome string
  6. perfect number.
    1.  6 is perfect number       6 divisible by 1,2,3   so 1+2+3  is 6  
  7. Amicable Number
    1. 321   and 123   are amicable numbers
  8. Amicable string
    1.   abc  and   bca   are amicable strings.



Monday, January 21, 2019

Find max of two integer arrays in C






int findMaxElement(int *arr1,int len1,int *arr2, int len2)
{
    int m1=arr1[0];
    for(int i = 1; i < len1; i++)
    {
        if(m1<arr1[i])
        m1=arr1[i];
       
    }
   
    for(int i = 0; i < len2; i++)
    {
        if(m1<arr2[i])
        m1=arr2[i];
       
    }
         return m1;
               
}

Date Difference program in c


const int monthDays[12] = {31, 28, 31, 30, 31, 30,
                           31, 31, 30, 31, 30, 31};
int countLeapYears(Date d)
{
    int years = d.year;
  
    // Check if the current year needs to be considered
    // for the count of leap years or not
    if (d.month <= 2)
        years--;
  
    return years / 4 - years / 100 + years / 400;
}


int difference_in_dates(Date *date1, Date *date2)
{
  
    long int n1 = date1->year*365 + date1->day;
  
    for (int i=0; i<date1->month - 1; i++)
        n1 += monthDays[i];
  
    n1 += countLeapYears(date1);
  

    long int n2 = date2->year*365 + date2->day;
    for (int i=0; i<date2->month - 1; i++)
        n2 += monthDays[i];
    n2 += countLeapYears(date2);
  
    // return difference between two counts
    return (n2 - n1);
}
  

Sunday, January 20, 2019

Right angle triangle test with three points


#include<stdio.h>
# include <stdlib.h>
# include <math.h>
int test(struct Point *p1,struct Point *p2,struct Point*p3)
{
    double x1,x2,x3,y1,y2,y3;
    x1=p1->x;
                y1=p1->y;
                x2=p2->x;
                y2=p2->y;
                x3=p3->x;
                y3=p3->y;
double a=sqrt(pow((y2-y1),2.0)+pow(x2-x1,2.0));
double b=sqrt(pow((y3-y2),2.0)+pow(x3-x2,2.0));
double c=sqrt(pow((y3-y1),2.0)+pow(x3-x1,2.0));
 if(a<(b+c)&&b<(a+c)&&c<(a+b))
    {
      
        if((a*a)==(b*b)+(c*c)||(b*b)==(a*a)+(c*c)||(c*c)==(a*a)+(b*b))
        return 1;
    }
    else
    return 0;
  
}
main()
{
   struct  Point *p1,*p2,*p3;
    p1->x=6.0;
    p2->x=4.0;
    p3->x=10.0;
    p1->y=1.0;
     p2->y=5.0; p3->y=3.0;
   
    printf("%d",test(p1,p2,p3));
}

Manchestor program

#include<stdio.h>
#include<stdlib.h>
int* mac(int *a,int l)
{
    int* res=(int*)malloc(sizeof(int)*l);
    a[-1]=0;
    for(int i=0;i<l;i++)
    {
        res[i]=a[i]==a[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=mac(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 

read two strings and test whether all chars of first string present in second string

#include<stdio.h>
#include<string.h>
int test(char*str1,char *str2)
{
    int ctr1=0,ctr2=0,i,j;
for(j=0;str1[j]!=0;j++)
{
    for(i=0;i<strlen(str1);i++)
    if(str1[i]==str1[0])
    ctr1++;
   
    for(i=0;i<strlen(str2);i++)
    if(str2[i]==str1[0])
    ctr2++;
   
 
    if(ctr1==ctr2)
    {
        j++;
   
    continue;
    }
    else
    return -1;
   
}
return 1;
}

int main() {
   char*s1="ab";
   char*s2="aa";
   printf("%d",test(s1,s2));
}

input :
abc      cba
1

ab  aa
-1






For Training Test Program solutions Talentio click

Exponent Value program

#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
--------------
2  -3

1/8



Saturday, January 19, 2019

usage of poi array in functions

#include<stdio.h>
#include<stdlib.h>
/* diaogonal   sum  */

int multiply(int (*a)[3],int n){
int i,j,sum=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i==j)
if((a[i][j])%2!=0)
sum=sum+(*(*(a+i)+j));
return sum;
}
void main(){
int arr[3][3],i,j,n,sum;
scanf("%d",&n);

for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",(*(arr+i)+j));
}
}
sum=multiply(arr,n);
printf("%d",sum);

}

Friday, January 11, 2019

C pattern

o/p
5
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15






#include ”stdio.h”
int main()
{
int i, j, k, l=1, N, d, r, count=0;
scanf(“%d”, &N);
for(i=1; i<=N; i++)
{
k=1;
d=i%2;
r=l+i-1;
for(j=0;j<i;j++)
{
if(d==0)
{
printf(“%d”,r);
r–;
if(k<i)
{
printf(“*”);
k=k+1;
}
l++;
continue;
}
printf(“%d”,l);
l++;
if(k<i)
{
printf(“*”);
k=k+1;
}
}
printf(“\n”);
}
return 0;

A Pythagorean triplet is a set of three integers a, b and c such that a2 + b2 = c2. Given a limit, generate all Pythagorean Triples with values smaller than given limit.

First Solution
#include<stdio.h>
main(){
    int initial,final,a,b,c;
    printf("Enter the range in which you want to search for Pythagorean Triplets:\nInitial: ");
    scanf("%d",&initial);
    printf("\nFinal: ");
    scanf("%d",&final);
    printf("The Pythogorean Triplets in the given range are as follows:\n____________________________________________________________\n");
    for(a=initial;a<=final;a++){
        for(b=a;b<=final;b++){
            for(c=b;c<=final;c++){
                if(c*c==a*a+b*b){
                    printf("%d , %d , %d\n",a,b,c);
                }
            }
        }
    }
}
second solution
void pythagoreanTriplets(int limit)
{
  
    // triplet: a^2 + b^2 = c^2
    int a, b, c = 0;
  
    // loop from 2 to max_limitit
    int m = 2;
  
    // Limiting c would limit
    // all a, b and c
    while (c < limit) {
  
        // now loop on j from 1 to i-1
        for (int n = 1; n < m; ++n) {
  
            // Evaluate and print triplets using
            // the relation between a, b and c
            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++;
    }
}
  
// Driver Code
int main()
{
    int limit = 20;
    pythagoreanTriplets(limit);
    return 0;
}
o/p
3 , 4 , 5
5 , 12 , 13
6 , 8 , 10
8 , 15 , 17
9 , 12 , 15
12 , 16 , 20