answersLogoWhite

0

📱

C Programming

Questions related to the C Computer Programming Language. This ranges all the way from K&R to the most recent ANSI incarnations. C has become one of the most popular languages today, and has been used to write all sorts of things for nearly all of the modern operating systems and applications. It it a good compromise between speed, power, and complexity.

9,649 Questions

Write a program to find gcd using recursive method in java?

for two positive integers:

public static int gcd(int i1, int i2) {

// using Euclid's algorithm

int a=i1, b=i2, temp;

while (b!=0) {

temp=b;

b=a%temp;

a=temp;

}

return a;

}

How do you count the number of words in a string?

To find the length of the string we use length method. The length property returns the length of a string (number of characters we use).The length of an empty string is 0.

For example:

function myFunction() {

var str = "Hello World!";

var n = str.length;

Hope this hepls.

What are the different tree methodologies in data structure?

The question itself is a bit vague since it doesn't suggest whether you're asking about the types of trees or the operations which can be performed on trees.

A tree is a data structure which stores information in a logical way that typically is ideally suited for searching quickly, often by ordering the nodes or items of the tree, also ideally, nodes should be able to be added to or removed from a tree quickly and efficiently. Often, trees are chosen as a means of simply structuring data in a meaningful way with no concerns as to their performance.

When trees are chosen as an alternative to a list, the reason for this is to gain the benefits of rapid insertion, searching and deletion of nodes. Common tree structures for this purpose are the binary tree and the black and red tree. A binary tree has an extremely simple and extremely fast method of searching for data, but it is highly dependent on nodes being added in an order which is somewhat random. If ordered data is added to a tree, the depth of the tree will be linear, thereby providing no benefits over a linked list in addition, removal of nodes can cause a binary tree to have to be rebuilt as all the nodes beneath the deleted node will have to be re-added to the tree, typically under different nodes, commonly causing linear branches of the tree and slowing down application performance.

R&B trees were designed to address many of the issues of a binary tree. By developing a data structure which intentionally keeps the depth of the tree shallow, high speed searching and node removal can be achieved, but at a cost to the insertion algorithm which is designed to "shake things up" a little. R&B trees are far beyond the scope of this answer.

General trees are used less as a means of providing ideal performance but instead are intended as a means of providing structure for data. General trees store information in a way which reflects the data format itself. An example of a general tree is the file system of a disk drive. The root directory contains zero or more children which each contain zero or more children which each contain zero or

more... you get the point. This structure is also used for things like the abstract syntax tree of a compiler. Source code is parsed into "tokens" which are the structured as nodes of a tree which are then "walked" when optimizing and producing binary code.

There are many more types of trees. Many of which ate covered by Donald Knuth in extensive, if not insane detail in "The Art of Computer Programming".

Among the operations you'd perform on the tree are insertion, deletion, searching, walking, reduction, simplification and more.

How can you declare global an local variables in flowcharts?

The simple answer is you don't. The primary purpose of a flowchart is to show the flow of execution through an algorithm, with all primary functions and decisions described in broad, abstract terms.

There is no need to distinguish between local and global variables because that is an implementation detail -- it's far too low-level a concept for algorithm design.

All variables utilised by an algorithm should essentially be declared up front at the start of the flowchart and should therefore be treated as being local to that particular flowchart. It doesn't matter where those variables came from, only that they be initialised accordingly. The decision to make a variable global comes much later, once the interactions between different flowcharts have been established. Even so, a global variable should only ever be considered when the variable represents a truly global concept. In the vast majority of cases, passing arguments into functions is often the better option. Especially when separate algorithms are intended to run concurrently because making a global variable thread-safe can seriously impact performance. But this is not a concern in algorithm design, they are only of importance to the implementers; the class designers.

How do you write a program that gives the GCD of three given numbers in C plus plus?

To find the GCD of three numbers, a, b and c, you need to find the GCD of a and b first, such that d = GCD(a, b). Then call GCD(d, c). Although you could simply call GCD(GCD(a, b), c), a more useful method is to use an array and iteratively call the GCD(a, b) function, such that a and b are the first two numbers in the first iteration, which becomes a in the next iteration, while b is the next number. The following program demonstarates this method.

Note that the GCD of two numbers can either be calculated recursively or iteratively. This program includes both options, depending on whether RECURSIVE is defined or not. In a working program you'd use one or the other, but the iterative approach is usually faster because it requires just one function call and no additional stack space.

The program will create 10 random arrays of integers of length 3 to 5 and process each in turn. Note that the more numbers in the array, the more likely the GCD will be 1.

#include<iostream>

#include<time.h>

#define RECURSIVE // comment out to use iterative method

#define ARRAY // comment out to use non-arrays

#ifdef RECURSIVE

// Returns the GCD of the two given integers (recursive method)

unsigned int gcd(unsigned int a, unsigned int b)

{

if(!a)

return(b);

if(!b)

return(a);

if(a==b)

return(a);

if(~a&1)

{

if(b&1)

return(gcd(a>>1,b));

else

return(gcd(a>>1,b>>1)<<1);

}

if(~b&1)

return(gcd(a,b>>1));

if(a>b)

return(gcd((a-b)>>1,b));

return(gcd((b-a)>>1,a));

}

#else

// Returns the GCD of the two given integers (iterative method)

unsigned int gcd(unsigned int a, unsigned int b)

{

if(!a)

return(b);

if(!b)

return(a);

int c;

for(c=0; ((a|b)&1)==0; ++c)

{

a>>=1;

b>>=1;

}

while((a&1)==0)

a>>=1;

do{

while((b&1)==0)

b>>=1;

if(a>b)

{

unsigned int t=a;

a=b;

b=t;

}

b-=a;

}while(b);

return(a<<c);

}

#endif RECURSIVE

// Returns the greatest common divisor in the given array

unsigned int gcd(const unsigned int n[], const unsigned int size)

{

if( size==0 )

return( 0 );

if( size==1 )

return( n[0] );

unsigned int hcf=gcd(n[0],n[1]);

for( unsigned int index=2; index<size; ++index )

hcf=gcd(hcf,n[index]);

return(hcf);

}

int main()

{

using std::cout;

using std::endl;

srand((unsigned) time(NULL));

for(unsigned int attempt=0; attempt<10; ++attempt)

{

unsigned int size=rand()%3+3;

unsigned int* num = new unsigned int[size];

unsigned int index=0;

while(index<size)

num[index++]=rand()%100;

unsigned int hcf=gcd(num,size);

cout<<"GCD(";

index=0;

cout<<num[index];

while(++index<size)

cout<<','<<num[index];

cout<<") = "<<hcf<<endl;

delete[]num;

}

cout<<endl;

}

What does the value 'float' mean in C plus plus?

The float data type is a fundamental numeric data type that can represent floating point values. Depending on the implementation, it is normally 4 bytes in size, with a precision around 6 decimal digits.

A float is a primitive data type in C++, representing a real number. On 32-bit system, a float occupies 4 bytes, representing values in the range 1.2e-38 to 3.4e38. They are often called single in other languages.

A double is also a floating point data type, larger than or equal to a float, but shorter or equal to a long double. On a 32-bit system, a double is 8 bytes long, representing values in the range 2.2e-308 to 1.8e308.

A long double is the longest floating point type and is equal to or greater than a double. On 32-bit systems, a long double is generally 10 byes long, representing values in the range 3.4e-4932 to 1.1e4932.

Note that in Microsoft's implementation of C++, a long double is the same length as a double, but they are still treated as different types.

Is C is better than JavaScript?

You can't compare them it's like apples and oranges. Javascipt is mainly geared for client-side execution in a web browser. c is an old language and is sort of the parent of all modern programming languages including javascript, but it would be used in a totally different scenario, eg. you could use it for application development on a server or desktop, rather than in an online scenario.

Three address code in compiler design?

prod=0;

i=1;

do

{

prod = prod +a[i] * b[i];

i=i+1;

} while(i<=20);

Write a program in c language to draw a diamond shaped if you give the number?

Hey, It is not the exact logic for a given program.

Here i m giving the code with minimum number of executing steps.

also i m sure that there doesn't exist any other way to print the same Diamond such that code executing steps will be less than mine.

It is not copied from any website, book etc.

#include

void main()

{

int n,i,s,k,flagReverce=0;

printf("Enter the no. of rows\n");

scanf("%d", &n);

for (i=1; i>=1; i++)

{

for (s=n; s>i; s-)

printf(" ");

for(k=1; k<(i*2)-1; k++)

printf("*")

printf("\n")

if (i == n)

flagReverce = 1

if (flagReverce = 1)

i-=2

}

}

How you use multiple programs in switch statement in c language?

program
#include
#include
#include
void main()
{
int a,b,ans,ch;
clrscr();
printf("enter the value of a and b");
scanf("%d%d",&a,&b);
printf("1.Addition");
printf("\n2.Subtraction");
printf("\n3.Multiplication");
printf("\n4. exit");
printf("enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
ans=a+b;
printf("\nAfter Addition:%d",ans);
break;
case 2:
ans=a-b;
printf("\nAfter Subtraction:%d",ans);
break;
case 3:
ans=a*b;
printf("\nAfter Multiplication:%d",ans);
break;
case 4:
exit(0);
break;
}
getch();
}

Java program to find compound interest?

public class SimpleInterest{

float principal,intRate, numOfYears;

public void setPrincipal(float principal){

this.principal = principal;

}

public void setIntRate(float intRate){

this.intRate = intRate;

} public void setNumOfYears(float numOfYears){

this.numOfYears = numOfYears;

} public float calculateSimpleInterest(){

return (this.principal * this.intRate * this.numOfYears) /100; } public static void main(String argv[]){

float principal,intRate, numOfYears;

SimpleInterest simpIntObj = new SimpleInterest();

principal = Float.parseFloat(argv[0]);

intRate = Float.parseFloat(argv[1]);

numOfYears = Float.parseFloat(argv[2]);

simpIntObj.setPrincipal(principal);

simpIntObj.setIntRate(intRate);

simpIntObj.setNumOfYears(numOfYears);

System.out.println ("Principal = "+principal);

System.out.println ("Interest Rate = "+intRate);

System.out.println ("Num of Years = "+numOfYears);

System.out.println ("Simple Interest Amount = "+simpIntObj.calculateSimpleInterest());

} }

Write an algorithm to print the factorial of given number and then draw the flowchart?

write an algorithm to print the factorial of a given number and then draw the flowchart.

This looks like someones homework, defiantly someone looking for the easy way. When it comes to programming, the more you do the better you get. Experience counts (making your own mistakes and learning from the mistake).

String reverse program with single string and without using library functions in java?

You can create a separate string initially empty. Then using a loop, start at the end of the string and add it to the end of the other string. At the end of the loop, the other string would contain the reverse.

Why stack is implemented by link list?

A Linked-List is basically a series of Nodes. Each Node contains two things: The contents, and the pointer to the next Node in the Linked-List. So you can traverse the Linked-List by following the "next" pointers in each Node, a bit like following road directions from city to city.

A stack is an abstract data type where you have two operations: "push" and "pop". Pushing means to put an item in the stack, Popping means to get the first element of the stack. When you push an item onto a stack, you put the item at the top: so its like cutting in line to the very front. The last one in is now first, and thus, the first one out. Another helpful image is a "stack" of trays at a cafeteria -- you can only get the tray from the top of the stack, or put a tray on top of the stack. The very first tray in the stack is actually the one at the very bottom, and thus, the last one to be used. "First in, Last Out."

A stack deals with what comes first/last, while a Linked-List describes how data is stored. A stack needs to store data, so a stack can be implemented as a Linked-List.

What is 49f in c?

C = (5F - 160°) ÷ 9, where C is the temperature in degrees Celsius and F is the temperature in degrees Fahrenheit.

C = (5 x 42° - 160°) ÷ 9

= (210° - 160°) ÷ 9

= 50° ÷ 9

≈ 5.6° C

What is a major advantage of licensing?

The person or body granting the license can get money for the license taken out.

Write an algorithm to find the numbers divisible by 3 but not by 6 and then draw the flowchart?

Start

Input a

if a%3 = 0 then

Print a

else

print" enter another number"

end

1. start

2. Input N

3. if N<3, go to step 6

4. if N%3 && N%6!=0 then

print N

5. N=N-3 go to step 3

6. end

Write a program that accepts a number and output its equivalent in words?

/*mycfiles.wordpress.com
program to convert digits to character*/
#include
#include
void main()
{
int a[5],i,n;
clrscr();
cout<<"Enter the Value";
cin>>n;
for(i=4;i>=0;i--)
{
a[i]=n%10;
n=n/10;
}
for(i=0;i<5;i++)
{
if(a[i]!=0)
{
switch(a[i])
{
case 0:cout<<"Zero";
break;
case 1:cout<<"One";
break;
case 2:cout<<"Two";
break;
case 3:cout<<"Three";
break;
case 4:cout<<"Four";
break;
case 5:cout<<"Five";
break;
case 6:cout<<"Six";
break;
case 7:cout<<"Seven";
break;
case 8:cout<<"Eight";
break;
case 9:cout<<"Nine";
break;
}
}
}
getch();
}


------------------------------------------------------------------------------------


Program to Convert Numbers into Words

#include

void pw(long,char[]);
char *one[]={" "," one"," two"," three"," four"," five"," six"," seven","
eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","
fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
char *ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty","
seventy"," eighty"," ninety"};


void main()
{
long n;
clrscr();
printf("
Enter any 9 digit no: ");
scanf("%9ld",&n);
if(n<=0)
printf("Enter numbers greater than 0");
else
{
pw((n/10000000),"crore");
pw(((n/100000)%100),"lakh");
pw(((n/1000)%100),"thousand");
pw(((n/100)%10),"hundred");
pw((n%100)," ");
}
getch();
}


void pw(long n,char ch[])
{
(n>19)?printf("%s %s ",ten[n/10],one[n%10]):printf("%s ",one[n]);
if(n)printf("%s ",ch);
}


// for any query visit
// "http://www.c.happycodings.com/Beginners_Lab_Assignments/code51.html"

Program to Convert Numbers into Words

#include

void pw(long,char[]);
char *one[]={" "," one"," two"," three"," four"," five"," six"," seven","
eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","
fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
char *ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty","
seventy"," eighty"," ninety"};


void main()
{
long n;
clrscr();
printf("
Enter any 9 digit no: ");
scanf("%9ld",&n);
if(n<=0)
printf("Enter numbers greater than 0");
else
{
pw((n/10000000),"crore");
pw(((n/100000)%100),"lakh");
pw(((n/1000)%100),"thousand");
pw(((n/100)%10),"hundred");
pw((n%100)," ");
}
getch();
}


void pw(long n,char ch[])
{
(n>19)?printf("%s %s ",ten[n/10],one[n%10]):printf("%s ",one[n]);
if(n)printf("%s ",ch);
}


// for any query visit
// "http://www.c.happycodings.com/Beginners_Lab_Assignments/code51.html"

How many bytes are required to store an address of an integer?

32 bits or 4 bytes.

This depends heavily on the processor architecture your computer uses, AND the programming language you are using.

Typically, an integer is 1 word in length, however that processor architecture defines "word". That could be 32-bits (4 bytes), 64-bits (8 bytes), 8-bits (1 byte), or even 9 bits (in certain old computers).

What is the format of the switch statement?

switch (expression) { case value 1 : [ statement-block 1] [break ;] case value 2 : [ statement-block 2] [break ;] ……. ……. case value N : [ statement-block N] [break ;] [default: [default block] [break;] ] } statement x;

What are the applications of double pointer in c?

insert or delete values both side.so use double pointer

What is convoluction?

Wikipedia has a section on this. Click the Wikipedia link below

Differences between if else and while loop?

Easy: if-else is not a loop; while, for and do-while are loops.

if-else just run once, but do-while run many times.

C programming to display a calendar?

hmmm. . .

string main () //NOTE you dont have to use the string main or return applepie = )

{

int month, days;

string month_name;

// also if you have to configure the Year i would go by coppying the number of days

//in each month by year and create a massive clause of if else trees by YEAR

// for instance (int year 9)

{

days= 30;

}

}

return applepie;