Category Archives: programs

Pyramid pattern

<br /> 

#include<iostream>
using namespace std;
int main()
{
int i,j,k,l,a;
cout<<"Enter the number of lines in the pyramid=";
cin>>a;
cout<<endl;
for(i=1;i<=a;i++)
{
for(j=a;j>=i;j--)
cout<<" ";
for(k=1;k<=i;k++)
{
cout<<"*";
}
for(l=1;l<=i-1;l++)
cout<<"*";

cout<<endl;

}
return 0;
}

 

output:

capture

Computerized Crime Register:

<br />#include

#include
#include
#include
#include
#include using namespace std;
class project
{
public:
char crime[20];
char name[20];

void getdata()
{
cout << "\nEnter name= "; cin>>name;

cout << "Enter the crime= "; cin>>crime;
}

void showdata()
{
cout << "\nName= " << name << endl ;
cout << "crime= " << crime<<endl;
}
void writedata()
{
ofstream fout("crime.txt", ios::app | ios::out);
project obj;
obj.getdata();
fout.write((char*)&obj, sizeof(obj));
fout.close();
}
};
int main()
{
project obj;
int num, input, flag = 0;
char pname[20],pcrime[20];
lable:
system("cls");
cout << "1.Add data\n";
cout << "2.Search by name\n";
cout << "3.Search by crime\n";
cout << "4.See all data\n";
cout << "5.Exit\n";
cout << "Enter choice: "; cin >> input;
ifstream fin("crime.txt",ios::in);
switch(input)
{ //adding new data
case 1:
obj.writedata();
fin.close();
goto lable;
case 2: //searching by name
cout << "\nEnter name= "; cin>>pname;
while (fin.read((char*)&obj,sizeof(obj)))
if (strcmp(pname,obj.name)==0)
{
obj.showdata();
flag = 1;
}
fin.close();
if (flag == 0)
cout << "\n\nDATA NOT FOUND!!!";
getch();
goto lable;
case 3: //searching by crime
cout << "\nEnter crime= "; cin>>pcrime;
while (fin.read((char*)&obj, sizeof(obj)))
if (strcmp(pcrime,obj.crime)==0)
{
obj.showdata();
flag = 1;
}
fin.close();
if (flag == 0)
cout << "\n\nDATA NOT FOUND!!!";
getch();
goto lable;
case 4: //show the list of all data
cout << "\nList of data::\n";
while (fin.read((char*)&obj, sizeof(obj)))
{
obj.showdata();
cout << endl;
flag = 1;
}
fin.close();
if (flag == 0)
cout << "\n\nDATA NOT FOUND!!!";
getch();
goto lable;
case 5:
exit(0);
default:
goto lable;
}
}

output:

c1 c2 c3c4

Integer To English Word Converter :

<br />#include<iostream>
using namespace std;
int main()
{
int a,c,i=0;
int b[6];
cout<<"Enter the number=";
cin>>a;
if(a==0)
{
cout<<"Zero";
exit;
}
while(1)
{
b[i]=a%10;
a=a/10;
if(a==0)
{
break;
}
i++;
}
while(i>0)
{
if(i==5)
{
switch(b[i])
{
case 1:
{
cout<<"one lakh ";
i--;
break;
}
case 2:
{
cout<<"two lakh ";
i--;
break;
}
case 3:
{
cout<<"three lakh ";
i--;
break;
}
case 4:
{
cout<<"four lakh ";
i--;
break;
}
case 5:
{
cout<<"five lakh ";
i--;
break;
}
case 6:
{
cout<<"six lakh ";
i--;
break;
}
case 7:
{
cout<<"seven lakh ";
i--;
break;
}
case 8:
{
cout<<"eight lakh ";
i--;
break;
}
case 9:
{
cout<<"nine lakh ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
if(i==4)
{
if(b[3]==0)
{
switch(b[4])
{
case 1:
{
cout<<"ten thousand ";
i--;
break;
}
case 2:
{
cout<<"twenty thousand ";
i--;
break;
}
case 3:
{
cout<<"thirty thousand ";
i--;
break;
}
case 4:
{
cout<<"forty thousand ";
i--;
break;
}
case 5:
{
cout<<"fifty thousand ";
i--;
break;
}
case 6:
{
cout<<"sixty thousand ";
i--;
break;
}
case 7:
{
cout<<"seven thhousand ";
i--;
break;
}
case 8:
{
cout<<"eight thousand ";
i--;
break;
}
case 9:
{
cout<<"ninty thousand ";
i--;
break;
}
default:
{
i--;
}
}
}
else
{
if(b[4]==1)
switch(b[3])
{
case 1:
{
cout<<"eleven thousand ";
i--;
i--;
break;
}
case 2:
{
cout<<"tweleve thousand ";
i--;
i--;
break;
}
case 3:
{
cout<<"thirteen thousand";
i--;
i--;
break;
}
case 4:
{
cout<<"fourteen thousand ";
i--;
i--;
break;
}
case 5:
{
cout<<"fifteen thousand ";
i--;
i--;
break;
}
case 6:
{
cout<<"sixteen thousand ";
i--;
i--;
break;
}
case 7:
{
cout<<"seventeen thousand ";
i--;
i--;
break;
}
case 8:
{
cout<<"eighteen thousand ";
i--;
i--;
break;
}
case 9:
{
cout<<"nineteen thousand ";
i--;
i--;
break;
}
default:
{
i--;
break;
}
}
else
{
switch(b[4])
{
case 2:
{
cout<<"twenty ";
i--;
break;
}
case 3:
{
cout<<"thirty ";
i--;
break;
}
case 4:
{
cout<<"forty ";
i--;
break;
}
case 5:
{
cout<<"fifty ";
i--;
break;
}
case 6:
{
cout<<"sixty ";
i--;
break;
}
case 7:
{
cout<<"seventy ";
i--;
break;
}
case 8:
{
cout<<"eighty ";
i--;
break;
}
case 9:
{
cout<<"ninty ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
}
}
if(i==3)
{
switch(b[3])
{
case 1:
{
cout<<"one thousand ";
i--;
break;
}
case 2:
{
cout<<"two thousand ";
i--;
break;
}
case 3:
{
cout<<"three thousand ";
i--;
break;
}
case 4:
{
cout<<"four thousand ";
i--;
break;
}
case 5:
{
cout<<"five thousand ";
i--;
break;
}
case 6:
{
cout<<"six thousand ";
i--;
break;
}
case 7:
{
cout<<" seven thousand ";
i--;
break;
}
case 8:
{
cout<<"eight thousand ";
i--;
break;
}
case 9:
{
cout<<"nine thousand ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
if(i==2)
{
switch(b[2])
{
case 1:
{
cout<<"one hundred ";
i--;
break;
}
case 2:
{
cout<<"two hundred ";
i--;
break;
}
case 3:
{
cout<<"three hundred ";
i--;
break;
}
case 4:
{
cout<<"four hundred ";
i--;
break;
}
case 5:
{
cout<<"five hundred ";
i--;
break;
}
case 6:
{
cout<<"six hundred ";
i--;
break;
}
case 7:
{
cout<<"seven hundred ";
i--;
break;
}
case 8:
{
cout<<"eight hundred ";
i--;
break;
}
case 9:
{
cout<<"nine hundred ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
if(i==1)
{
if(1)
{
switch(b[1])
{
case 1:
{
if(b[0]==0)
{
cout<<"ten ";
i--;
}
break;
}
case 2:
{
cout<<"twenty ";
i--;
break;
}
case 3:
{
cout<<"thirty ";
i--;
break;
}
case 4:
{
cout<<"forty ";
i--;
break;
}
case 5:
{
cout<<"fifty ";
i--;
break;
}
case 6:
{
cout<<"sixty ";
i--;
break;
}
case 7:
{
cout<<"sevety ";
i--;
break;
}
case 8:
{
cout<<"eighty ";
i--;
break;
}
case 9:
{
cout<<"ninty ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
if(b[1]==1)
{
switch(b[0])
{
case 1:
{
cout<<"eleven ";
i--;
break;
}
case 2:
{
cout<<"twelve ";
i--;
break;
}
case 3:
{
cout<<"thirteen ";
i--;
break;
}
case 4:
{
cout<<"forteen ";
i--;
break;
}
case 5:
{
cout<<"fifteen ";
i--;
break;
}
case 6:
{
cout<<"sixteen ";
i--;
break;
}
case 7:
{
cout<<"seventeen ";
i--;
break;
}
case 8:
{
cout<<"eighteen ";
i--;
break;
}
case 9:
{
cout<<"nineteen ";
i--;
break;
}
default:
{
i--;
break;
}
}
}
}
if(i==0&&b[1]!=1)
{
switch(b[0])
{
case 1:
{
cout<<"one";
i--;
break;
}
case 2:
{
cout<<"two";
i--;
break;
}
case 3:
{
cout<<"three";
i--;
break;
}
case 4:
{
cout<<"four";
i--;
break;
}
case 5:
{
cout<<"five";
i--;
break;
}
case 6:
{
cout<<"six";
i--;
break;
}
case 7:
{
cout<<"seven";
i--;
break;
}
case 8:
{
cout<<"eight";
i--;
break;
}
case 9:
{
cout<<"nine";
i--;
break;
}
default:
{
i--;
break;
}
}
}
else
{
i--;
break;
}
}
return 0;
}

output:-

capture
Output

Program that determines the number of trailing zeros at the end of X! (X factorial), where X is an arbitrary number.

<br />#include<iostream>
using namespace std;
int main()
{
long long a,b=1;
cout<<“Enter the number whose factorial you want to calculate=”;
cin>>a;
if(a==0)
{
cout<<endl<<“The factorial of the given number is=1″<<endl;
}
else
{

while(a>1)
{
b=b*a;
a–;
}

}
cout<<“The factorial of the given number is=”<<b<<endl;
a=0;
while(1)
{
if(b%10==0)
{
b=b/10;
a++;
}
else
{
break;
}
}
cout<<“The number of the trailing zeroes in the given factorial is=”<<a;
return 0;
}

Sieve of Eratosthenes – prime number generator

Description:-

A prime number is a natural number that has exactly two distinct natural number divisors: 1 and itself.

To find all the prime numbers less than or equal to a given integer n by Eratosthenes’ method:

1. Create a list of consecutive integers from 2 through n: (2, 3, 4, …, n).
2. Initially, let p equal 2, the first prime number.
3. Starting from p, enumerate its multiples by counting to n in increments of p, and mark them in the list (these will be 2p, 3p, 4p, … ; the p itself should not be marked ).
4. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.

Code Implementation :-

<br />#include<iostream>
#include <cstring>
#include <ctime>
#include <cstdio>
using namespace std;

int markprime(bool arr[], int n, int i) {
int x = 2, p;
while((p = (i * x)) < n) {
arr[p] = true;
++x;
}

x = i+1;

while (arr[x]) x++;
return x;
}

int main(){
int n, i;
i = 2;
cout<<"enter the number to which you want to generate the prime numbers-like 100 for 0 - 100"<<endl;
cin >> n;

bool arr[n];
for(int i = 0; i < n; i++) arr[i] = false;

arr[0] = true;
arr[1] = true;

while ((i*i) <= n) {
i = markprime(arr,n,i);

}

for(i = 1; i < n; i++){
if(arr[i] == false)
printf("%d ", i);
}

return 0;
}

Output :-
PrimeGenerator

Factorial

<br />#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n,m=1;
cout<<"Enter the value of n : ";
cin>>n;
if(n<0)
{
cout<<endl<<"Factorial of "<<n<<" is infinity"<<endl;
}
else
{
for(int i=1;i<=(n);i++)
{
m=m*i;
}
cout<<endl<<"Factorial of "<<n<<" is : "<<m<<endl;
}
cout<<endl<<" @Myprogworld.....!!!!";
getch();
return 0;
}

Output:-

factorial

Dynamic allocation in c++ using calloc/malloc and realloc.

#include<iostream>
#include<stdlib.h>
using namespace std;
main()
{
int *arr,m,n,i;
cout<<"Enter total no. of elements:";
cin>>n;
arr=(int *)malloc(n*sizeof(int)); //use of malloc
cout<<"\nEnter "<<n<<" elements:";
for(i=0;i<n;i++)
{
cin>>*(arr+i);
}
cout<<"Enter another total no. of elements:";
cin>>m;
arr=(int *)realloc(arr,m*sizeof(int)); //use of realloc
cout<<"Enter another "<<m<<" elements:";
for(i=n;i<m+n;i++)
{
cin>>*(arr+i);
}
cout<<"\nThe elements are:";
for(i=0;i<n+m;i++)
{
cout<<"\t"<<*(arr+i);
}
free(arr);
}

Output:-
Untitled

Anagram Strings :-

NVIDIA Interview Question Software Engineer / Developers

Question:-
write a code to check if the two strings are anagram or not.

Answer:-
An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “hello” and “hlelo” are anagram of each other.

<br />#include <iostream>
#include <string>
using namespace std;
#define MAX_ASCII 127

int main()
{
char first_str[30],second_str[30];
cout<<"Enter first string:-"<<endl;
cin>>first_str;
cout<<endl<<"Enter second string:-"<<endl;
cin>>second_str;
if ( strlen(first_str) != strlen(second_str) )
{
cout << "Not anagram....!!!" << endl;
return 0;
}
int freq[MAX_ASCII + 1];
for ( int i = 0; i < MAX_ASCII + 1; i++ )
freq[i] = 0;
char* ptr1 = first_str;
while ( *ptr1 != '' ) {
char c = tolower(*ptr1);
freq[(int)c]++;
ptr1++;
}
char *ptr2 = second_str;
while ( *ptr2 != '' ) {
char c = (char)tolower(*ptr2);
freq[(int)c]--;
ptr2++;
}
for ( int i = 0; i < MAX_ASCII + 1; i++ )
if ( freq[i] > 0 )
{
cout <<endl<< "oops...Strings are not anagram....!!" << endl;
return 0;
}
else
{
continue;
}
cout <<endl<< "yeepy...strings are anagram..!!!" << endl;
return 0;
}

Output :-

Untitled

 

Pushing all the zero to the end in an array.

Question (Asked by Amazon ) :- Given a number in an array form, Come up with code to push all the zeros to the end.

Solution :-

#include<iomanip>
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int arr[20];
int len,count=0;
int i=0,n;
cout<<"Enter the size of array:-"<<endl;
cin>>n;
cout<<"enter elements:-"<<endl;
for(int i=0;i<n;i++)
{
cout<<" ";
cin>>arr[i];
}
cout<<"You've Entered :-"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i];
}
//--------------sorting------------------
for(int i=0;i<n;i++)
{
if(arr[i] != 0)
arr[count++]=arr[i];
}
while(count<n)
{
arr[count++]=0;
}
cout<<endl<<"Now after pushing all the zeros to the end array is:-"<<endl;
for(int i=0;i<n;i++)
{
cout<<" "<<arr[i];
}
cout<<"nn"<<setw(30)<<"@ myprogworld..!! ";
getch();
return 0;
}

Output :-

Untitled

Pythagorean Triplet :-

<br />#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
int x,y,z,count=0,n;
cout<<"----------Pythagorean triplet till [Enter a number]:-"<<endl;
cin>>n;

for(x=1;x<=n;++x)
{
for(y=1;y<=n;++y)
for(z=1;z<=n;++z)
{
if(x*x==y*y+z*z)
{ cout<<endl<<x<<"^2 = "<<y<<"^2 + "<<z<<"^2";
count++;
}
}
}
getch();
}

Output :-
Untitled