to print a char or a no given no of times

11 replies [Last post]
ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102

#include<stdio.h>
int print(int n,char c);
{
int n;
char c;
printf("enter no of times");
scanf("%d",&n);
printf("enter the char");
scanf("%c",&c);
p=print(n,c);
}
print(int n,char c)
{
int i;
for(i=0;i<n;i++)
{
print=(n*c);
}
printf("the value is %d",p);
}

ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102
i dont know the program code

i dont know the program code pls help me to find out a sol for this

is this program correct

 

shashwat
Offline
Last seen: 1 year 31 weeks ago
Joined: 2009-02-18
bOt Points: 1160
ramyaa try this code

ramyaa try this code -

 

#include<stdio.h>

#include<iostream.h>

 

void print(int n,char c)

{

if(n>1)

print(n-1,c);

cout<<c;

}

 

int main()

{

int number=10,c;

printf("enter no of times");

scanf("%d",&number);

printf("enter the char");

c='*';

cout<<"\nHere is your pattern using recursive function - \n";

print(number,c);

}

 

Founder and Admin

ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102
thanks a lot shashwat this is

thanks a lot shashwat this is how i wanted but got cofused

ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102
gcd program in c

how to write a c functions to find the gcd of a number

shashwat
Offline
Last seen: 1 year 31 weeks ago
Joined: 2009-02-18
bOt Points: 1160
#include<stdio.h> #include<io

#include<stdio.h>

#include<iostream.h>

 

/* a & b are the numbers whose GCD is to be found. 

 Given a > b

 */

  int gcd(int a,int b)

  {

    int c;

    while(1)

    {

   c = a%b;

   if(c==0)

    return b;

   a = b;

   b = c;

    }

  }

  int main()

  {

  int first_no=200;

  int second_no=18;

  (first_no>second_no)?cout<<(gcd(first_no,second_no)):cout<<(gcd(second_no,first_no));

  

  } 

Founder and Admin

ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102
thanks for the program

thanks for the program

ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102
could u pls tell me what is the error in this program it is conc

 

 it is concatenation of 2 arrays
#include<stdio.h>

main()
{
int a1[40],a2[30],a3[60];
int i,j,n,m;
printf("\n how many no in 1st array");
scanf("%d",&n);
printf("\nhow many ele in 2nd array");
scanf("%d",&m);
printf("\n enter the nos");
for(i=0;i<=n;i++)
{
scanf("%d",&a1[i]);
}
printf("\n enter the elements in 2nd array");
for(j=0;j<=m;j++)
{
scanf("%d",&a2[j]);
}
for(i=0;i<=n+m;i++)
{
printf("\n the elements after concationation%d",a1[i],a2[j]);
}

}

Note:- This post has been edited by ramyaa at Sat, 2009-12-19 20:39.

shashwat
Offline
Last seen: 1 year 31 weeks ago
Joined: 2009-02-18
bOt Points: 1160
concatenation of strings

Hi ramyaa,

Check this program..

 

#include <stdio.h>    /* stdin, printf, and fgets */

#include <string.h>   /* for all the new-fangled string functions */


/* this function is designed to remove the newline from the end of a string

entered using fgets.  Note that since we make this into its own function, we

could easily choose a better technique for removing the newline.  Aren't

functions great? */

void strip_newline( char *str, int size )

{

    int i;


    /* remove the null terminator */

    for (  i = 0; i < size; ++i )

    {

        if ( str[i] == '\n' )

        {

            str[i] = '\0';


            /* we're done, so just exit the function by returning */

            return;   

        }

    }

    /* if we get all the way to here, there must not have been a newline! */

}


int main()

{

    char name[50];

    char lastname[50];

    char fullname[100]; /* Big enough to hold both name and lastname */


    printf( "Please enter your name: " );

    fgets( name, 50, stdin );


    /* see definition above */

    strip_newline( name, 50 );


    /* strcmp returns zero when the two strings are equal */

    if ( strcmp ( name, "shashwat" ) == 0 ) 

    {

        printf( "That's my name too.\n" );

    }

    else                                     

    {

        printf( "That's not my name.\n" );

    }

    // Find the length of your name

    printf( "Your name is %d letters long", strlen ( name ) );

    printf( "Enter your last name: " );

    fgets( lastname, 50, stdin );

    strip_newline( lastname, 50 );

    fullname[0] = '\0';            

    /* strcat will look for the \0 and add the second string starting at

       that location */

    strcat( fullname, name );     /* Copy name into full name */

    strcat( fullname, " " );      /* Separate the names by a space */

    strcat( fullname, lastname ); /* Copy lastname onto the end of fullname */

    printf( "Your full name is %s\n",fullname );


    getchar();


    return 0;

} 

Founder and Admin

ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102
thanks for ur answer but i

thanks for ur answer but i need to concatenate 2 nos for eg a1=123

a2=45

then after concatnation 12345

shashwat
Offline
Last seen: 1 year 31 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Check this code - #include

Check this code -

#include <stdio.h>    

#include <iostream.h>  



int main()

{

    int a1=123;

    int a2=45;

    int a3=0;

    while(a2%10)

    {

    a3=(a3*10)+(a2%10);

    a2/=10;

    }

    while(a3%10)

    {

    a1=(a1*10)+(a3%10);

    a3/=10;

    }

    cout<<"Number after concatenation = "<<a1;

    return 0;

} 

Founder and Admin

ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102
thanks for the code

thanks for the code