C Program To Find The Third Largets Number in An Array Using Sorting

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

C program to find the third largets number in an array using

sorting

#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("Third Largest number is %d\n", number[n-3]);
}

C program to find the third largets number in an array using


sorting without duplication

#include <stdio.h>
void main()
{
int i, j, a, n, number[30],m=0,b[30];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
for(i=0;i<n;i++)
{
if(a[i]!=a[i+1])
{
b[m++]=number[i];
}
}
printf("Third Largest number is %d\n", b[m-3]);
}

C program to find the third largets number in an array without


using sorting

#include <stdio.h>
void main()
{
int i, j, max,n,a[30],count=0;
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
l1: max=a[0];
for (i = 1; i < n; i++)
{
if(max<a[i])
{
max=a[i];
j=i;
}
}
for(i=j+1;j<n;j++)
{
a[i-1]=a[i];
}
n--;
count++;
if(count<3)
goto l1;
printf("Third Largest number is %d\n", max);
}
Palindrome number program c

#include <stdio.h>
int main()
{
int n, reverse = 0, temp;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}

String reverse in c without using string function

#include<stdio.h>
int main()
{
char str[50], rev[50];
int i=-1,j=0;
printf("Enter any string : ");
scanf("%s",str);
while(str[++i]!='\0');
while(i>=0)
rev[j++] = str[--i];
rev[j]='\0';
printf("Reverse of string is : %s",rev);
return 0;
}

C program to find the number of days between two dates

#include<stdio.h>
#include<conio.h>
void days(int,int,int,int,int,int);
int month(int,int);
int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};

void main()
{
int a1,b1,c1,a2,b2,c2;
clrscr();
printf("Enter first date(dd mm yyyy) : ");
scanf("%d%d%d",&a1,&b1,&c1);
printf("\nEnter second date(dd mm yyyy) : ");
scanf("%d%d%d",&a2,&b2,&c2);
if(c2>=c1)
days(c1,c2,b1,b2,a1,a2);
else
days(c2,c1,b2,b1,a2,a1);
getch();
}

void days(int y1,int y2,int m1,int m2,int d1,int d2)


{
int count=0,i;
for(i=y1;i<y2;i++)
{
if(i%4==0)
count+=366;
else
count+=365;
}
count-=month(m1,y1);
count-=d1;
count+=month(m2,y2);
count+=d2;
if(count<0)
count=count*-1;
printf("The no. of days b/w the 2 dates = %d days",count);
}
int month(int a,int yy)
{
int x=0,c;
for(c=0;c<a-1;c++)
{
if(c==1)
{
if(yy%4==0)
x+=29;
else
x+=28;
}
else
x+=mon[c];
}
return(x);
}
How to write power in c

#include<stdio.h>
int main()
{
int pow,num,i=1;
long int sum=1;
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
while(i<=pow)
{
sum=sum*num;
i++;
}
printf("\n%d to the power %d is: %ld",num,pow,sum);
return 0;
}

COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM

#include<stdio.h>
int main()
{
FILE *p,*q;
char file1[20],file2[20];
char ch;
p=fopen(input.txt,"r");
q=fopen(output.txt,"w");
while((ch=getc(p))!=EOF)
putc(ch,q);
fclose(p);
fclose(q);
return 0;
}

C Program to count number of vowels in a string.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch[10];
int i,count=0;
clrscr();
printf("Enter the string\n");
gets(ch);
for(i=0;ch[i]!='\0';i++)
{
if(ch[i]=='a' || ch[i]=='e' || ch[i]=='i' || ch[i]=='o' || ch[i]=='u')
{
count++;
}
}
printf("Vowels = %d",count);
getch();
}

C program to print Pascal triangle using for loop

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

int main()
{
int a[10][10];
int no;
printf("Enter the number of rows for pascal triangle : ");
scanf("%d",&no); //taking input
for(int i=1; i<=no; i++)
{
for(int j=1; j<=i; j++)
{
if(j==1||j==i)
{
a[i][j]=1;
}
else // taking arrays value
{
a[i][j]=a[i-1][i-j]+a[i-1][i-j+1];
}
}
}
for(int i=1; i<=no; i++)
{
for(int k=1; k<=no-i; k++)
{
printf(" ");
}
for(int j=1; j<=i; j++)
{
printf("%d ",a[i][j]); //printing pascal triangle
}
printf("\n");
}
getch();
}
Sample output:

Enter the no. of lines: 8


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

You might also like