Skip to content
This repository was archived by the owner on Jan 15, 2023. It is now read-only.

array rotation #687

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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 .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
80 changes: 54 additions & 26 deletions C/ArrayRotation.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,61 @@
#include<stdio.h>
// C program to rotate an array by
// d elements
#include <stdio.h>

int main()
{
int i, j, k, n=5, arr[n], temp;

printf("Enter the element of an array: \n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
/* function to print an array */
void printArray(int arr[], int size);

printf("Enter the number of rotation: \n");
scanf("%d",&k);
k=k%n;
/*Function to get gcd of a and b*/
int gcd(int a, int b);

printf("Array before rotation: \n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
/*Function to left rotate arr[] of siz n by d*/
void leftRotate(int arr[], int d, int n)
{
int i, j, k, temp;
/* To handle if d >= n */
d = d % n;
int g_c_d = gcd(d, n);
for (i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
temp = arr[i];
j = i;
while (1) {
k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}

while(k!=0)
{
temp=arr[0];
for(j=0;j<(n-1);j++)
arr[j]=arr[j+1];
arr[n-1]=temp;
k=k-1;
}
/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
}

printf("\n Array after rotation: \n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
/*Function to get gcd of a and b*/
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}

return 0;
/* Driver program to test above functions */
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
leftRotate(arr, 2, 7);
printArray(arr, 7);
getchar();
return 0;
}
Binary file added C/ArrayRotation.exe
Binary file not shown.
54 changes: 34 additions & 20 deletions CPP/Armstrong Number.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
#include <iostream>
using namespace std;
int main()
{
int n,r,sum=0,temp;
cout<<"Enter the Number= ";
cin>>n;
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
cout<<"Armstrong Number."<<endl;
else
cout<<"Not Armstrong Number."<<endl;
return 0;
}
#include <cmath>
#include <iostream>

using namespace std;

int main() {
int num, originalNum, remainder, n = 0, result = 0, power;
cout << "Enter an integer: ";
cin >> num;

originalNum = num;

while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;

while (originalNum != 0) {
remainder = originalNum % 10;

// pow() returns a double value
// round() returns the equivalent int
power = round(pow(remainder, n));
result += power;
originalNum /= 10;
}

if (result == num)
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";
return 0;
}
Binary file added CPP/Armstrong Number.exe
Binary file not shown.