Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Check_Palindrome.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
#define SIZE 8

int main()
{
unsigned int n;
printf("Enter number ( max range 255)\n");
scanf("%d",&n);
int c[SIZE]={0};
int i=SIZE-1;
printf("Binary representation is: ");
while(n!=0){
c[i--]=n&1;
n=n>>1;
}
for(int j=0;j<SIZE;j++)
printf("%d",c[j]);
printf("\n");

for(int j=0,k=SIZE-1;j<k;j++,k--){
if(c[j]!=c[k]){
printf("The number is not palindrome\n");
return 0;
}
}
printf("The number is palindrome\n");
return 0;
}
28 changes: 28 additions & 0 deletions Counting_Substring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
#include <string.h>
void count(char *str);
int main()
{
char str(100)
scanf("%[^\n]",str);
count(str);
}
void count(char *str)
{
char tmp[]="the";
int i,j,c=0,cnt=0;
for(i-0;i<strlen(str);)
{
j=0;c=0;
while(str[i]==tmp[j]&&j!=3)
{
c++;i++;j++;
}
if(c==strlen(tmp) && (str[i]==' ' || str[i]==' \n' 1 str[i]==NULL))
{
cnt++;
}
i++;
}
printf("%d", cnt);
}
21 changes: 21 additions & 0 deletions DivisibleBy9.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<stdio.h>
int divisibleby9(int n)
{
if(n==9 || n==0)
return 1;
if(n<9)
return 0;
int x=n>>3;
int y=n&7;
n=x-y;
return divisibleby9(n);
}
int main()
{
int n;
scanf("%d",&n);
if(divisibleby9(n))
printf("Yes");
else
printf("No");
}
21 changes: 21 additions & 0 deletions FibonacciSeries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<iostream>
using namespace std;
void printFibonacci(int n){
static int n1=0, n2=1, n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n-1);
}
}
int main(){
int n;
cout<<"Enter the number: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2);
return 0;
}
28 changes: 28 additions & 0 deletions Himanshu Sharma/Check_Palindrome.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
#define SIZE 8

int main()
{
unsigned int n;
printf("Enter number ( max range 255)\n");
scanf("%d",&n);
int c[SIZE]={0};
int i=SIZE-1;
printf("Binary representation is: ");
while(n!=0){
c[i--]=n&1;
n=n>>1;
}
for(int j=0;j<SIZE;j++)
printf("%d",c[j]);
printf("\n");

for(int j=0,k=SIZE-1;j<k;j++,k--){
if(c[j]!=c[k]){
printf("The number is not palindrome\n");
return 0;
}
}
printf("The number is palindrome\n");
return 0;
}
28 changes: 28 additions & 0 deletions Himanshu Sharma/Counting_Substring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
#include <string.h>
void count(char *str);
int main()
{
char str(100)
scanf("%[^\n]",str);
count(str);
}
void count(char *str)
{
char tmp[]="the";
int i,j,c=0,cnt=0;
for(i-0;i<strlen(str);)
{
j=0;c=0;
while(str[i]==tmp[j]&&j!=3)
{
c++;i++;j++;
}
if(c==strlen(tmp) && (str[i]==' ' || str[i]==' \n' 1 str[i]==NULL))
{
cnt++;
}
i++;
}
printf("%d", cnt);
}
21 changes: 21 additions & 0 deletions Himanshu Sharma/DivisibleBy9.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<stdio.h>
int divisibleby9(int n)
{
if(n==9 || n==0)
return 1;
if(n<9)
return 0;
int x=n>>3;
int y=n&7;
n=x-y;
return divisibleby9(n);
}
int main()
{
int n;
scanf("%d",&n);
if(divisibleby9(n))
printf("Yes");
else
printf("No");
}
21 changes: 21 additions & 0 deletions Himanshu Sharma/FibonacciSeries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<iostream>
using namespace std;
void printFibonacci(int n){
static int n1=0, n2=1, n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n-1);
}
}
int main(){
int n;
cout<<"Enter the number: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2);
return 0;
}
35 changes: 35 additions & 0 deletions Himanshu Sharma/Issue76.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<stdio.h>
void findSmallest(int n)
{
int i, j=0;
int res[100];

if (n < 10)
{
printf("%d", n+10);
return;
}
for (i=9; i>1; i--)
{
while (n%i == 0)
{
n = n/i;
res[j] = i;
j++;
}
}
if (n > 10)
{
printf("Not possible");
return;
}
for (i=j-1; i>=0; i--)
printf("%d", res[i]);
}
int main()
{
int n;
scanf("%d",&n);
findSmallest(n);
return 0;
}
40 changes: 40 additions & 0 deletions Himanshu Sharma/Jump_Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<iostream>
#include<cmath>

using namespace std;
int jumpSearch(int array[], int size, int key) {
int start = 0;
int end = sqrt(size);

while(array[end] <= key && end < size) {
start = end;
end += sqrt(size);
if(end > size - 1)
end = size;
}

for(int i = start; i<end; i++) {
if(array[i] == key)
return i;
}
return -1;
}

int main() {
int n, searchKey, loc;
cout << "Enter number of items: ";
cin >> n;
int arr[n];
cout << "Enter items: " << endl;

for(int i = 0; i< n; i++) {
cin >> arr[i];
}

cout << "Enter search key to search in the list: ";
cin >> searchKey;
if((loc = jumpSearch(arr, n, searchKey)) >= 0)
cout << "Item found at location: " << loc << endl;
else
cout << "Item is not found in the list." << endl;
}
29 changes: 29 additions & 0 deletions Himanshu Sharma/LeftShift.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>

int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int m;
scanf("%d",&m);
int tmp[m];
for(int i=0;i<m;i++)
tmp[i]=a[i];

int j=0;
for(int i=0;i<n;i++){
if((i+m)>=n){
a[i]=tmp[j];
j++;
}
else
a[i]=a[i+m];
}
for(int i=0;i<n;i++)
printf("%d ",a[i]);

return 0;
}
50 changes: 50 additions & 0 deletions Himanshu Sharma/Magic Square.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bits/stdc++.h>
using namespace std;
void generateSquare(int n)
{
int magicSquare[n][n];
memset(magicSquare, 0, sizeof(magicSquare));

int i = n/2;
int j = n-1;
for (int num = 1; num <= n*n; )
{
if (i == -1 && j == n)
{
j = n-2;
i = 0;
}
else
{
if (j == n)
j = 0;
if (i < 0)
i = n - 1;
}
if (magicSquare[i][j])
{
j -= 2;
i++;
continue;
}
else
magicSquare[i][j] = num++;

j++; i--;
}
cout<<"The Magic Square for n="<<n<<":\nSum of "
"each row or column "<<n*(n*n+1)/2<<":\n\n";
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
cout<<magicSquare[i][j]<<" ";
cout<<endl;
}
}
int main()
{
int n;
cin>>n;// Works only when n is odd
generateSquare (n);
return 0;
}
31 changes: 31 additions & 0 deletions Himanshu Sharma/Minimum adjacent swaps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
void solve(int a[], int n)
{
int maxx = -1, minn = a[0], l = 0, r = 0;
for (int i = 0; i < n; i++) {
if (a[i] > maxx) {
maxx = a[i];
l = i;
}
if (a[i] <= minn) {
minn = a[i];
r = i;
}
}
if (r < l)
cout << l + (n - r - 2);
else
cout << l + (n - r - 1);
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
solve(arr, n);
return 0;
}
Loading