Sunday, 22 January 2017

Skill Rack PPS2

Verification of circular prime number


A circular prime number is a prime number 'p' with a property that all the number s got by cyclic permuting the digits of 'p', are also a prime number.
A number is said to be a prime number if it has no factors other than 1 and itself .19937 is a circular prime number , as all the numbers obtained by cyclic permuting the numbers-19937,99371,93719,37199,71993,19937 are all prime.
Develop an algorithm and write a C program to check if the number is circular prime or not.

input Format

A number

output number

Print Circular prime or Not circular prime.

C code-

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int n=0,i=0,l=0,c=0,j,pr,qw,rem=0;
    char s[200],temp;
    scanf("%d",&n);
    while(n!=0)                           //counting =the number of digits and getting the digits in a list
    {
        s[i]=n%10;
        n=n/10;
        l++;
        i++;
    }

    for(c=0;c<l;c++)
    {
        for(j=1;j<l;j++)
        {
            temp=s[j-1];
            s[j-1]=s[j];
            s[j]=temp;
        }
for(i=l-1;i>=0;i--)
{
    rem=rem*10+s[i];
}

    for(pr=2;pr<rem;pr++)
    {
    if (rem%pr==0)
    {
        qw=1;
    }
    }
}
    if(qw==1)
        printf("Not circular prime");
    if (qw==2)
        printf("Circular prime");
}




basic pay and salary-

C code-

#include<stdio.h>
#include<string.h>
main()
{
    int s,bp,da,n=0,id=0,hra;
    char m[100];
    scanf("%d",&n);
   do{
scanf("%s%d%d%d%d",&m,&id,&bp,&da,&hra);
    s=bp+((da*bp)/100)+hra;
    printf("\n%d",id);
    printf("\n%d",s);
    n--;
    }
    while(n!=0);
}


Ques-Consider an nxn board game with four types of coins red, green, blue and yellow. Given the state of the board with coins in all cells, develop an algorithm and write a C program to check if the same coins are placed in  the shape of ‘L’  on the board. The number of cells in the vertical and horizontal line of ‘L’ shape, is same. Red coins are represented by ‘r’, blue coins are represented by ‘b’, green coins are represented by ‘g’ and yellow coins are represented by ‘y’.

C code-
#include<stdio.h>
void main()
int i,j,n,max,num;
scanf("%d",n);
int a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
max=a[i][0];
for(j=0;j<n;j++)
{
   if (a[i][j]>max)
max=a[i][j];
}
printf("%d\n",num);
}
}

row maximum-

ques-Given an nXn matrix with entries as numbers, print the maximum value in each row of the matrix.


c code-

#include< stdio.h >
void main()
{
int i,j,n,max,num;
scanf("%d",&n);
for(i=0;i < n;i++)
{
max=-1;
for(j=0;j < n;j++)
{
scanf("%d",&num);
if(num > max)
max=num;
}
printf("%d\n",max);
}
}

identify machines on same network

ques-Numeric addresses for computers on the international network, ‘Internet’ has four parts, separated by periods, of the form   xxx.yyy.zzz.mmm    where  xxx ,  yyy ,  zzz , and  mmm  are positive integers. Locally, computers are usually known by a nickname as well.
Sample Data
IP address       Name
111.22.3.44        platte
555.66.7.88        wabash
111.22.5.66        green
0.0.0.0                none
A pair of computers are said to be in same locality when the first two components of the addresses are same. Given the details of some computers, design an algorithm and write a C program to display a list of messages identifying each pair of computers from the same locality. In the messages, the computers should be identified by their nicknames. In this example, the message to be displayed will be Machines platte and green are on the same local network. For example, given IP address and nick name of machines as follows:
101.33.2.1              Atlas
101.33.56.80          Horizon
101.43.45.74          Pluto
Print ‘Machines Atlas and Horizon are on the same local network’.


#include< stdio.h >
#include< string.h >
void main()
{
    int n,p,i,j;
    char s[20][20],na[10][10];
    scanf("%d",&n);
    for(i=0;i < n;i++)
    {
        scanf("%s%s",s[i],na[i]);
        p=0;
        j=0;
        while(p!=2)
        {
            j++;
            if(s[i][j]=='.')
            p++;
        }
        s[i][j]='\0';
    }
    for(i=0;i < n-1;i++)
    for(j=i+1;j < n;j++)
    if(strcmp(s[i],s[j])==0)
    printf("Machines %s and %s are on the same local network",na[i],na[j]);

}

Thursday, 19 January 2017

INLAB -ALL

INLAB 7-(Tested working 100%)

Question-Price of Books and CD/DVD

Lally publication House publishes two types of materials viz,books and recorded CDs.Both.......i am lazy to type

Solution-that's all u gotta type

void learning_Material::get(){}
void learning_Material::print(){}

class book:public learning_Material
{
public:
void get()
{
cin>>isbn>>title>>author>>year;
}
void print()
{
cout<<isbn<<"\n"<<title<<"\n"<<price;
}
void calc_Price()
{
int pages;
cin>>pages;
price=pages*1;
}
};
class CD:public learning_Material
{
public:
void get()
{
cin>>isbn>>title>>author>>year;
}
void print()
{
cout<<isbn<<"\n"<<title<<"\n"<<price;

}
cin>>isbn
void calc_Public()
{
int dur:
cin>>dur;
price=dur*2;
}
};



INLAB 5


Question (Electronics Store Problem)



XYZ  electronics store has three different products in their store such as Television, Refrigerator and Washing machine. Create a C++ class to process the basic details about the electronic products in the store, such as the name of the product, product code, product price and number of products in stock. Create another C++ class to support the store keeper to maintain the stock details in the store. Write a non member function to help the store keeper to find the products whose stock is less than 10.



Input Format:

The first line will contain the number of products n.
Name of the product
Product code
Price
stock in hand

Output Format:
The complete details of the product whose available stock is less than 10

Boundary Conditions:
n=3

Solution

#include< iostream >
using namespace std;
class store_keeper;
class item
{
char prod_name[30];
char prod_code[10];
float prod_price;
int stock_In_Hand;
public:
void get();
void print()const;
friend class store_keeper;
};
class store
{
int num_Of_Items;
item items[20];
public:
void get_details();
void print_details()const;
friend class store_keeper;
};
class store_keeper
{
char name[30];
char id[10];
public:
void get();
void print();
//Function used to print name and id of products
// with stock_In_Hand less than 10
void stock_mgmt(store &);
};

ANSWER-

void item::get()
{
cin>>prod_code>>prod_item>>prod_price>>stock_in_hand;
}
void item::print(const)
{
cout<<prod_code<<prod_item;
}
void store::get_details()
{
cin>>num_Of_Items;
for(int i=0;i<num_Of_Item;i++)
items[i].get();
}
void store::print_details()const
{}
void store_keeper::get()
{}
void store_keeper::print()
{}
void store_keeper::stock_mgmt(store &s)
{
for(int i=0;i<s.num_Of_Items;i++)
if(s.items[i].stock_In_hand<10)
s.items[i].print();
}


main()
{
store s;
store_keeper sk;
s.get_details();
sk.stock_mgmt(s);
}



INLAB 4

Question (Arithmetic Progression)

An Arithmetic Progression(AP) is described by the first term ‘f’ and the common difference’. The n-th term of an AP is described by a_n= f+(n-1)d. The progression 1,4,9 .. is an AP.  Given the f,d n, k, p , q, Develop a program in C++ using the class, to represent an arithmetic progression and provide methods to compute:
(i)  n-th term of the arithmetic progression
(ii)  r such that a_r=k. If there is no r such that a_r=k, the program should print zero.
(iii) Absolute Difference between a_p and a_q
Input Format:
First line contains the first term of the AP
Next line contains the common difference d
Next line contains the value of k
Next line contains the value of p
Next line contains the value of q
Output Format:
First line should contain the nth of the arithmetic progression
Next line should contain the value of ‘r’
Next line should contain the absolute difference between a_p and a_q
Boundary Conditions:
a , d are integers
n,p,q are all positive integers

c++ code-

#include<iostream>
#include<stdlib.h>
using namespace std;
class apr
{

    int first;

    int diff;
public:
    void get();

    int compute_terms(unsigned int n);

    int find_r(int k);
    int abs_diff(unsigned int p, unsigned int q);
};
void apr::get()
{
    cin>>first>>diff;
};
int apr::compute_terms(unsigned int n)
{
    n=first+(n-1)*diff;
    return((n));
}
int apr::find_r(int k)
{
    if (((k-first)%diff)+1==0)
{
    k=(abs(((k-first)/diff)+1));
    return(abs(k));
}
    else
    return(0);
}
int apr::abs_diff(unsigned int p, unsigned int q)
{
   return(abs((p-q)*diff));   
}




int main()

{
    apr ap1;
    unsigned int n,p,q;
    int k;
    ap1.get();
    cin>>n;
    cout<<ap1.compute_terms(n)<<endl;
    cin>>k;
    cout<<ap1.find_r(k)<<endl;
    cin>>p>>q;
    cout<<ap1.abs_diff(p,q)<<endl;
}

INLAB -3

Guiding a travelling salesman


Ques-A laptop sales company wants their salesperson to visit some cities every month and promote their sales.  A code has to be written to find the sequence of visit so that he travels the minimum. Given ‘n’ cities and a home city, design an algorithm and write a C++ code to determine the number of routes to be evaluated when the problem is to be solved by brute force approach. For example, if there are four cities A, B, C, and D and ‘A’ is the home city then brute force method calculate distance of the following routes:

A B C D A

A B D C A

A C B D A

A C D B A

A B C D A

A B D C A

By default the company assign only five months every month for each of its employee but it may vary.

Input Format

Number of cities

Name of the cities

Home city name

Output Format

Number of routes to be analyzed in brute force approach


C code-

#include<iostream>
#include<conio.h>
using namespace std;
int use(int n)
{
int a=1;
for(int i=n;i > 1;i--)
a=a*i;
return(a);
}
main()
{
int n,i;
char p;
cin>>n;
for(int i=0;i < n;i++)
cin>>p;
cin>>p;
cout<<use(n-1);
return(0);
}


for those who don't know what is brute force method-

example : "If we wanted to guess a person's age, we could brute force by just starting from 1 and increasing by 1 every time, etc" by adding 1 if not found the solution.
It's brute force because you'd eventually reach the person's age, but you didn't do anything but try every possibility until one worked. An algorithm is not brute force if it exploits some advantage or approaches a problem such that you could arrive at a solution without having to try every possibility, ever. For example if a person was an adult, and you knew he was born in the 1970s, your 'guessing' algorithm would limit you to only 10 or so age possibilities, because you have some knowledge you can exploit to limit how many solutions you have to try


Nature of digit in position

Ques-Given a number 'n' and a position ; write an algorithm and the subsequent 'C' program to check if the 'p-th' digit .starting from the left most digit of the number of 'n',is odd or even. For example-if n is 3145782 and p is 4 then you have to check if 5 is odd or even .Since it is odd ,print 'Odd',make your code to accept numbers of larger size.

Input format-
the first line contains the number ,n
The second line contains the position p
Output format-
Print either "Odd" or "Even".

c code-


#include<stdio.h>
main()
{
int  b,z,i=0,a,k,j,l=0;
long n,r;
scanf("%d",&n);
r=n;
scanf("%d",&k);
while(n!=0)
{
n=n/10;
l++;
}
b=l-k;
a=pow(10,b);
r=r/a;
z=r%10;
if(z%2==0)
    printf("number is even");
else
    printf("number is odd");

}



Complex number

ques-A computer scientist working in image processing is working on discrete Fourier transforms.he need an implementation of complex number to use in his program. develop an algorithm and write a C program to implement addition,subtraction and multiplication in complex numbers. implement each operation as a function and call it in your main.The function call sequence is addition,subtraction and multiplication. for example-
when the complex numbers are 3+2i and 1+7i the output should be-
4+9i
2-5i
-11+23i

input format-

real part of complex number 1
imaginary part of complex number 1
real part of complex number 2
imaginary part of complex number 2

output format-

resultant complex number represented as
real part +/- imaginary part followed by an i

C code-

#include<stdio.h>
void add();
void subs();
void mul();
main()
{
    int a,b,c,d,e,f,n,i;
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);
    scanf("%d",&d);
    add(a,b,c,d);
    printf("\n");
    subs(a,b,c,d);
    printf("\n");
    mul(a,b,c,d);
    printf("\n");
}
void add(int a,int b,int c,int d)
{   int e,f;
    e=a+c;
    f=b+d;
    {
        printf("%d",e);
    }
    {
    if (f>0)
        printf("+%di",f);
    else
        printf("%di",f);
    }
}
void subs(int a,int b,int c,int d)
{
    int g,h;
    g=a-c;
    h=b-d;{
        printf("%d",g);
    }
    {
    if (h>0)
        printf("+%di",h);
    else
        printf("%di",h);
}
}
void mul(int a,int b,int c,int d)
{   int i,j;
    i=(a*c-b*d);
    j=(a*d+b*c);
     {
        printf("%d",i);
    }
    {
    if (j>0)
        printf("+%di",j);
    else
        printf("%di",j);
    }
}

Tuesday, 17 January 2017

Skillrack PPS1 Solutions

Fibonacci series

Ques-Given the value of 'n' , write a recursive routine in C to print the first 'n' elements of the Fibonacci series.

input format-

value of 'n'

output format-

fibonacci series of 'n' terms, each term separated by a space.


C code-

#include<stdio.h>
main()
{
    int a,i,s=0,m=1,n=0;
    scanf("%d",&a);
    for(i=1;i<=a;i++)
    {
        if(i==1)
        {
            printf("%d ",s);
            continue;
        }
        if(i==2)
        {
            printf("%d ",m);
            continue;
        }
        n=s+m;
        s=m;
        m=n;
        printf("%d ",n);
    }
 }


Reversed string-

Ques-Enter any string and find the reverse of the string. for example-abc should be printed as cba.

C code-

#include<stdio.h>
#include<string.h>
main()
{
    int i=0,j=0;
    char s[20],temp;
    scanf("%s",&s);
    j=strlen(s)-1;
    i=0;
    while(i<j)
    {
        temp=s[i];
        s[i]=s[j];
        s[j]=temp;
        i++;
        j--;
    }
    printf("%s",s);
}


Cyclic Right Shift-

Ques-enter the number of elements then enter that number of elements and then enter the number of times you want the letters to be shifted.
example-number of elements-6
1,2,3,4,5,6
number of times shifted-2
the new shifted series-5,6,1,2,3,4

C code-


#include<stdio.h>
main()
{
    int a,i,s[200],c=0,m,temp;
    scanf("%d",&a);
    for(i=0;i<a;i++)
    {
        scanf("%d",&s[i]);
    }
   
    scanf("%d",&m);

do
    {

    for(i=a-1;i>0;i--)
    {
        temp=s[i];
        s[i]=s[i-1];
        s[i-1]=temp;

        }c++;
}while(c<m);
for(i=0;i<a;i++)
{
printf("%d",s[i]);
}
}


Second Smallest-

ques-given the set of numbers find out the second smallest number present in the set.
example-number of elements-5
elements-1,4,5,6,3
second smallest-3

C code-

#include<stdio.h>
#include<string.h>
main()
{
    int a=0,i=0,temp,j=0,s[200];
    scanf("%d",&a);
    for(i=0;i<a;i++)
    {
        scanf("%d",&s[i]);
    }
    for(i=0;i<a;i++)
    {
        for(j=i+1;j<a;j++)
        {
            if (s[i]>s[j])
            {
            temp=s[i];
            s[i]=s[j];
            s[j]=temp;
            }
    }
    }

    printf("%d",s[1]);
}


leaders of elements

Ques-Given a set of 'n' elements in an order.identify all the leaders and print them .An element is said to be a leader if all the elements to its right are smaller than it .For example-if the elements are 12,13,16,7,10 then there is only one leader -16.if there are no leaders in a given set of elements then print 'No Leaders'.

input format-
number of numbers in a given set 'n'
element-1
element-2
.....
element-n

output format-
elements that are leaders else print 'No leaders' when there is no leader.

C code-  


#include<stdio.h>
main()
{
    int n,i,j,d=0;
scanf("%d",&n);
int s[n];
for(i=0;i<n;i++)
{
scanf("%d",&s[i]);
}
for(i=0;i<n-1;i++)
{
      int c=0;
for(j=i-1;j<n;j++)
{
      if (s[i]>s[j]
c++;
}
if (c==n-(i+1))
{
      printf("\n%d",s[i]);
d++;
}
}
if (d==0)
printf("No leaders");
}


for more just STAY TUNED






Tuesday, 10 January 2017

How to download and install CODEBLOCKS for C/C++

Hey every one just go to the link and download the codeblocks for windows from the link given also download the compiler (coz without compiler it just won't run) ,so basically u have to download both the files.

to download code:blocks for windows and complier altogether go here
go here-

download code:blocks



now u just gotta install the program
now try running a small sample program-
here it is for u-


#include<stdio.h>
main()
{
printf("you are noob");
return(0);
}

any queries pls post in comments...
                                

GPT4ALL - A new LLaMa (Large Language Model)

posted 29th March, 2023 - 11:50, GPT4ALL launched 1 hr ago  What if we use AI generated prompt and response to train another AI - Exactly th...