-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from rishitbhojak/main
C Program to find largest element in an array #88
- Loading branch information
Showing
32 changed files
with
867 additions
and
3 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
int num,i,tmp,d,sumOfCubes=0; | ||
printf("Enter the number : "); | ||
scanf("%d",&num); | ||
tmp = num; | ||
while(num!=0) | ||
{ | ||
d = num%10; | ||
sumOfCubes = sumOfCubes + (d*d*d); | ||
num = num/10; | ||
} | ||
if(tmp == sumOfCubes) | ||
printf("It is a Armstrong Number"); | ||
else | ||
printf("It is not a Armstrong Number"); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
int start,end,i,tmp,d,sumOfCubes=0,num; | ||
printf("Enter the interval : "); | ||
scanf("%d ",&start); | ||
scanf("%d",&end); | ||
for(num = start;num<=end;num++) | ||
{ | ||
sumOfCubes=0; | ||
tmp = num; | ||
while(tmp!=0) | ||
{ | ||
d = tmp%10; | ||
sumOfCubes = sumOfCubes + (d*d*d); | ||
tmp = tmp/10; | ||
} | ||
if(num == sumOfCubes) | ||
printf("%d is a Armstrong Number\n",num); | ||
else | ||
printf("%d is not a Armstrong Number\n",num); | ||
|
||
|
||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <math.h> | ||
#include <stdio.h> | ||
int main() { | ||
int low, high, number, originalNumber, rem, count = 0; | ||
double result = 0.0; | ||
printf("Enter two numbers(intervals): "); | ||
scanf("%d %d", &low, &high); | ||
printf("Armstrong numbers between %d and %d are: ", low, high); | ||
|
||
// iterate number from (low + 1) to (high - 1) | ||
// In each iteration, check if number is Armstrong | ||
for (number = low + 1; number < high; ++number) { | ||
originalNumber = number; | ||
|
||
// number of digits calculation | ||
while (originalNumber != 0) { | ||
originalNumber /= 10; | ||
++count; | ||
} | ||
|
||
originalNumber = number; | ||
|
||
// result contains sum of nth power of individual digits | ||
while (originalNumber != 0) { | ||
rem = originalNumber % 10; | ||
result += pow(rem, count); | ||
originalNumber /= 10; | ||
} | ||
|
||
// check if number is equal to the sum of nth power of individual digits | ||
if ((int)result == number) { | ||
printf("%d ", number); | ||
} | ||
|
||
// resetting the values | ||
count = 0; | ||
result = 0; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include<stdio.h> | ||
int Check_perfect(int n) | ||
{ | ||
int i,sum=0; | ||
for(i=1;i<n;i++){ | ||
if(n%i==0) | ||
sum+=i; | ||
} | ||
if(sum==n)return 1; | ||
return 0; | ||
} | ||
int main() | ||
{ | ||
int n; | ||
printf("Enter any number:"); | ||
scanf("%d",&n); | ||
if(Check_perfect(n))printf("Yes,it is a perfect number"); | ||
else printf("No it is not a perfect number"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <stdio.h> | ||
int main() | ||
{ | ||
char str[100]; | ||
char *p; | ||
int vCount=0,cCount=0; | ||
|
||
printf("Enter any string: "); | ||
fgets(str, 100, stdin); | ||
|
||
//assign base address of char array to pointer | ||
p=str; | ||
|
||
//'\0' signifies end of the string | ||
while(*p!='\0') | ||
{ | ||
if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U' | ||
||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u') | ||
vCount++; | ||
else | ||
cCount++; | ||
//increase the pointer, to point next character | ||
p++; | ||
} | ||
|
||
printf("Number of Vowels in String: %d\n",vCount); | ||
printf("Number of Consonants in String: %d",cCount); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
int num,i; | ||
printf("Enter the number : "); | ||
scanf("%d",&num); | ||
printf("Factors are "); | ||
for(i=1;i<=num/2;i++) | ||
{ | ||
if(num % i == 0) | ||
printf("%d ",i); | ||
} | ||
printf("%d",num); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// C program to find maximum in arr[] of size n | ||
#include <stdio.h> | ||
|
||
// C function to find maximum in arr[] of size n | ||
int largest(int arr[], int n) | ||
{ | ||
int i; | ||
|
||
// Initialize maximum element | ||
int max = arr[0]; | ||
|
||
// Traverse array elements from second and | ||
// compare every element with current max | ||
for (i = 1; i < n; i++) | ||
if (arr[i] > max) | ||
max = arr[i]; | ||
|
||
return max; | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[] = {10, 324, 45, 90, 9808}; | ||
int n = sizeof(arr)/sizeof(arr[0]); | ||
printf("Largest in given array is %d", largest(arr, n)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/*C program to find largest sum contiguous subarray using Kadane's Algorithm*/ | ||
#include<stdio.h> | ||
#include<limits.h> | ||
|
||
int maxSubArraySum(int a[], int size) | ||
{ | ||
int max_so_far = INT_MIN, max_ending_here = 0; | ||
int i; | ||
for ( i = 0; i < size; i++) | ||
{ | ||
max_ending_here = max_ending_here + a[i]; | ||
if (max_so_far < max_ending_here) | ||
max_so_far = max_ending_here; | ||
|
||
if (max_ending_here < 0) | ||
max_ending_here = 0; | ||
} | ||
return max_so_far; | ||
} | ||
|
||
/*Driver program to test maxSubArraySum*/ | ||
int main() | ||
{ | ||
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3}; | ||
int n = sizeof(a)/sizeof(a[0]); | ||
int max_sum = maxSubArraySum(a, n); | ||
printf("Maximum contiguous sum is %d", max_sum ); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <stdio.h> | ||
int main() | ||
{ | ||
int n; | ||
double arr[100]; | ||
printf("Enter the number of elements (1 to 100): "); | ||
scanf("%d", &n); | ||
|
||
for (int i = 0; i < n; ++i) | ||
{ | ||
printf("Enter number%d: ", i + 1); | ||
scanf("%lf", &arr[i]); | ||
} | ||
|
||
// storing the largest number to arr[0] | ||
for (int i = 1; i < n; ++i) | ||
{ | ||
if (arr[0] < arr[i]) | ||
{ | ||
arr[0] = arr[i]; | ||
} | ||
} | ||
|
||
printf("Largest element = %.2lf", arr[0]); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Enter size of matrix-->3 | ||
Enter the elements of the array | ||
4 7 0 | ||
2 5 8 | ||
4 3 8 | ||
Matrix is | ||
4 7 0 | ||
2 5 8 | ||
4 3 8 | ||
Tranpose of the matrix is | ||
4 2 4 | ||
7 5 3 | ||
0 8 8 | ||
*/ | ||
#include<stdio.h> | ||
|
||
void main() | ||
|
||
{ | ||
int a[20][20],i,j,n; | ||
printf("Enter size of matrix-->"); | ||
scanf("%d",&n); | ||
printf("\n Enter the elements of the array \n"); | ||
for(i=0;i<n;i++) | ||
{ | ||
for(j=0;j<n;j++) | ||
scanf("%d",&a[i][j]); | ||
} | ||
printf("\n Matrix is\n "); | ||
for(i=0;i<n;i++) | ||
{ | ||
for(j=0;j<n;j++) | ||
{ | ||
printf("%d ",a[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
printf("\n Tranpose of the matrix is\n "); | ||
for(j=0;j<n;j++) | ||
{ | ||
for(i=0;i<n;i++) | ||
{ | ||
printf("%d ",a[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* structure is used to return two values from minMax() */ | ||
#include<stdio.h> | ||
struct pair | ||
{ | ||
int min; | ||
int max; | ||
}; | ||
|
||
struct pair getMinMax(int arr[], int n) | ||
{ | ||
struct pair minmax; | ||
int i; | ||
|
||
/*If there is only one element then return it as min and max both*/ | ||
if (n == 1) | ||
{ | ||
minmax.max = arr[0]; | ||
minmax.min = arr[0]; | ||
return minmax; | ||
} | ||
|
||
/* If there are more than one elements, then initialize min | ||
and max*/ | ||
if (arr[0] > arr[1]) | ||
{ | ||
minmax.max = arr[0]; | ||
minmax.min = arr[1]; | ||
} | ||
else | ||
{ | ||
minmax.max = arr[1]; | ||
minmax.min = arr[0]; | ||
} | ||
|
||
for (i = 2; i<n; i++) | ||
{ | ||
if (arr[i] > minmax.max) | ||
minmax.max = arr[i]; | ||
|
||
else if (arr[i] < minmax.min) | ||
minmax.min = arr[i]; | ||
} | ||
|
||
return minmax; | ||
} | ||
|
||
/* Driver program to test above function */ | ||
int main() | ||
{ | ||
int arr[] = {1000, 11, 445, 1, 330, 3000}; | ||
int arr_size = 6; | ||
struct pair minmax = getMinMax (arr, arr_size); | ||
printf("nMinimum element is %d", minmax.min); | ||
printf("nMaximum element is %d", minmax.max); | ||
getchar(); | ||
} |
Oops, something went wrong.