0% found this document useful (0 votes)
17 views

Codes' CNMC

Uploaded by

UTKARSH SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Codes' CNMC

Uploaded by

UTKARSH SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1-SQUARE-

// A C program to check if four given points form a square or not.

#include<stdio.h>

// Structure of a point in 2D space

struct Point

int x, y;

};

// A utility function to find square of distance

// from point 'p' to point 'q'

int distSq(struct Point p, struct Point q)

return (p.x - q.x)*(p.x - q.x) +

(p.y - q.y)*(p.y - q.y);

// This function returns true if (p1, p2, p3, p4) form a

// square, otherwise false

int isSquare(struct Point p1, struct Point p2, struct Point p3,struct Point p4)

int d2 = distSq(p1, p2); // from p1 to p2

int d3 = distSq(p1, p3); // from p1 to p3

int d4 = distSq(p1, p4); // from p1 to p4

// If lengths if (p1, p2) and (p1, p3) are same, then

// following conditions must met to form a square.

// 1) Square of length of (p1, p4) is same as twice

// the square of (p1, p2)

// 2) p4 is at same distance from p2 and p3

if (d2 == d3 && 2*d2 == d4)

int d = distSq(p2, p4);

return (d == distSq(p3, p4) && d == d2);

// The below two cases are similar to above case

if (d3 == d4 && 2*d3 == d2)

int d = distSq(p2, p3);

return (d == distSq(p2, p4) && d == d3);

}
if (d2 == d4 && 2*d2 == d3)

int d = distSq(p2, p3);

return (d == distSq(p3, p4) && d == d2);

return 0;

// Driver program to test above function

int main()

int n;

scanf("%d",&n);

for(int i=0;i<n;i++)

struct Point p1,p2,p3,p4;

scanf("%d%d%d%d%d%d%d%d",&p1.x,&p1.y,&p2.x,&p2.y,&p3.x,&p3.y,&p4.x,&p4.y);

isSquare(p1, p2, p3, p4)? printf("1"):printf("0");

printf("\n");

return 0;

2)Find the highest element-


#include<stdio.h>

int main()

int n,i,j,max=0,t,arr[200];

scanf("%d",&t); //t is no of test cases

for(j=0;j<t;j++) //this loop will run t times and accept t test cases

max=0;

scanf("%d",&n); //accepting size of array

for(i=0;i<n;i++)

scanf("%d",&arr[i]); //accepting array elements

for(i=0;i<n;i++)
{

if(arr[i]>max) //comparing for maximum value

max=arr[i];

printf("%d\n",max); //print the maximum element

return 0;

3)Given an array, print reverse-


#include <iostream>

using namespace std;

int main()

int t;

cin>>t;

int n,a[n];

for(int j=0;j<t;j++){

cin>>n;

for(int i=0;i<n;i++)

cin>>a[i];

for(int i=n-1;i>=0;i--)

cout<<a[i]<<" ";

return 0;

4)Merge two arrays A, B of size N and M –


#include<stdio.h>

void merge(int arr1[],int arr2[], int arr3[], int n1, int n2)

int i=0,j=0,k=0;

while (i<n1 && j <n2)

// Check if current element of first

// array is smaller than current element

// of second array. If yes, store first

// array element and increment first array

// index. Otherwise do same with second array


if (arr1[i] < arr2[j])

arr3[k++] = arr1[i++];

else

arr3[k++] = arr2[j++];

// Store remaining elements of first array

while (i < n1)

arr3[k++] = arr1[i++];

// Store remaining elements of second array

while (j < n2)

arr3[k++] = arr2[j++];

int main()

int n1,n2,i;

scanf("%d%d",&n1,&n2);

int arr1[n1],arr2[n2],arr3[n1+n2];

for( i=0;i<n1;i++)

scanf("%d",&arr1[i]);

for(i=0;i<n2;i++)

scanf("%d",&arr2[i]);

merge(arr1,arr2,arr3,n1,n2);

for(i=0; i<n1+n2; i++)

printf("%d\n",arr3[i]);

return 0;

5)sum is closest to zero-


#include <stdio.h>

#include<stdlib.h>

int main() {

int n,arr[n],i,j,k,sum=10000, x=0,y=0;

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)
{

if(abs(arr[i]+arr[j])<abs(sum))

sum = arr[i]+arr[j];

x=i;

y=j;

printf("%d %d",arr[x],arr[y]);

return 0;

6)replace every element with the next greatest element-


#include <stdio.h>

#include<stdlib.h>

int main() {

int n,i=0,j=0,max=0;

scanf("%d",&n);

int arr[n];

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

for(i=0;i<n-1;i++)

max=arr[i+1];

for(j=i+1;j<n;j++)

if(arr[j]>max)

max=arr[j];

arr[i]=max;

arr[n-1]=-1;

for(i=0;i<n;i++)

printf("%d ",arr[i]);

return 0;

7)given input integer is prime or not-


#include <stdio.h>

#include<math.h>

int main()

int n;

scanf("%d",&n);

int ctr=0;

for(int j=2;j<=n/2;j++)

if(n%j==0)

ctr++;

break;

if((ctr==0)&&(n!=1)&&(n!=0))

printf("%0.2f",sqrt(n));

else

printf("-1");

return 0;

8)program to find out the roots of a quadratic equation-


#include <stdio.h>

#include<math.h>

int main() {

int a,b,c,d;

float x1,x2;

scanf("%d%d%d",&a,&b,&c);

d=(b*b)-(4*a*c);

if(d>=0)

x1=(-b+sqrt(d))/(2*a);

x2=(-b-sqrt(d))/(2*a);

printf("%0.2f,%0.2f",x1,x2);

else
printf("UNREAL");

return 0;

9)Write a program in C++ to overload a function named "area"


#include <iostream>

using namespace std;

float area(int);

float area(int,int);

float area(int,int,int);

int main() {

int k, a=5,b=10,c=15;

scanf("%d",&k);

if(k==1)

cout<<area(a);

else if(k==2)

cout<<area(a,b);

else if(k==3)

cout<<area(a,b,c);

else

cout<<"INVALID";

return 0;

float area(int a)

return (3.14*a*a);

float area(int a, int b)

return a*b;

float area(int a,int b,int c)

return ((0.5)*a*(b+c));

Q21—Merge two arrays A, B of size N and M respectively into the third array C of size N+M. Arrays are assumed to be sorted

in ascending order.

#include<iostream>
#include<algorithm>
using namespace std;
main(){
    int n,m, i=0;
    cin>>n>>m;
    int a[n+m];
    for(; i<n+m; i++)
        cin>>a[i];
    sort(a,a+n+m);
    for(i=0; i<n+m; i++)
        cout<<a[i]<<" ";
}
Q22-Find the two elements from the given array A of size N such that their sum is closest to zero.

#include<iostream>
#include<stdlib.h>
using namespace std;
main(){
    int i=0, j, n, s, h=0, k=1;
    cin>>n;
    int a[n];
    for(; i<n; i++)
        cin>>a[i];
    s=abs(a[0]+a[1]);
    for(i=0; i<n-1; i++)
        for(j=i+1; j<n; j++)
        if(s>abs(a[i]+a[j])){
            s=abs(a[i]+a[j]);
            h=i;
            k=j;
    }
    cout<<a[h]<<" "<<a[k];
}
Q23-Given an array A of size N, replace every element with the next greatest element (greatest element on the right side) in the array.
If there are no numbers to the right of an element, or there are no greater number to the right of the element,

then replace it with -1.

#include<iostream>
#include<algorithm>
using namespace std;
main(){
    int n, i=0;
    cin>>n;
    int a[n];
    for(; i<n; i++)
        cin>>a[i];
    for(i=1; i<n; i++)
        cout<<*max_element(a+i,a+n)<<" ";
    cout<<-1;
}

PRINTING STARS
#include<iostream.h>
#include<conio.h>

void main()
{
int i,j,k;
clrscr();
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
cout<<" ";
}
for(k=1; k<=(2*i-1); k++)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}

NUMBER PATTERN
#include<iostream.h>
#include<conio.h>

void main()
{
int i,j, k=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<i;j++)
{
cout<<k;
k++
}
cout<<"\n";
}
getch();
}

OUTPUT
1
23
456
78910

DIAMOND STAR
#include<iostream.h>
#include<conio.h>

void main()
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
cout<<" ";
}
for(k=1;k<(i*2);k++)
{
cout<<"*";
}
cout<<"\n";
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
cout<<" ";
}
for(k=1;k<(i*2);k++)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}
JOIN 2 STRINGS
#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
char *ch1="john";
char *ch2="porter";
char *ptr;
clrscr();
cout<<"1 st String: "<<ch1;
cout<<"\n 2 nd String: "<<ch2;
strcat(ch1,ch2);
cout<<"\nCombined string is: "<<ch1;
getch();
}
OUTPUT JOHNPORTER

NO OF CONSONENTS AND VOWELS


#include<stdio.h>
#include<iostream.h>
#include<conio.h>

int main()
{
char line[150];
int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
clrscr();
cout<<"Enter a line of string:\n";
gets(line);
for(i=0;line[i]!='\0';++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' ||
line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' ||
line[i]=='O' || line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&&
line[i]<='Z'))
++c;
else if(line[i]>='0'&& line[i]<='9')
++d;
else if (line[i]==' ')
++s;
}
cout<<"Vowels: "<<v;
cout<<"\nConsonants: "<<c;
cout<<"\nDigits: "<<d;
cout<<"\nWhite spaces: "<<s;
getch();
}
FIBONACCI
#include<iostream.h>
#include<conio.h>

void main()
{
int i,no, first=0, second=1, next;
clrscr();
first=0;
second=1;
cout<<"Enter nubmer of terms for Series: ";
cin>>no;
cout<<"Fibonacci series are: \n";
for(i=0; i<no; i++)
{
cout<<"\n"<<first;
next = first + second;
first = second;
second = next;
}
getch();
}
ARMSTRONG
#include<iostream.h>
#include<conio.h>

void main()
{
int arm=0,a,b,c,d,no;
clrscr();
cout<<"Enter any num: ";
cin>>no;
d=no;
while(no>0)
{
a=no%10;
no=no/10;
arm=arm+a*a*a;
}
if(arm==d)
{
cout<<"Armstrong";
}
else
{
cout<<"not Armstrong";
}
getch();
}
FACTORIAL
#include<iostream.h>
#include<conio.h>

void main()
{
int i, no, fact=1;
clrscr();
cout<<"Enter the any no. : ";
cin>>no;
for(i=1;i<=no;i++)
{
fact=fact*i;
}
cout<<"Factorial: "<<fact;
getch();
}
ASCII
#include<iostream.h>
#include<ctype.h>
#include<conio.h>

int main(void)
{
int number, result;
clrscr();
cout<<"Enter any Character/Symbol/Digits: ";
number = getch();
result = toascii(number);
cout<<"Ascii value: "<<result;
getch();
}
PALINDROME
#include<iostream.h>
#include<conio.h>

void main()
{
int a,no,b,temp=0;
clrscr();
cout<<"Enter any num: ";
cin>>no;
b=no;
while(no>0)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
cout<<"Palindrome";
}
else
{
cout<<"Not Palindrome";
}
getch();
}

You might also like