PYRAMID DESIGNING;

QUESTION- WRITE A C PROGRAM TO ACCEPT NO. OF ROWS FROM USER AND DISPLAY THE NUMBER PYRAMID;

ANSWER-

#include<stdio.h>
#include<conio.h>
int main()
{
int n,r,c,q;
printf(“\nNo.of rows you want : “);
scanf(“%d”,&n);
for(r=1; r<=n; r++)
{
for(q=n-r; q>=1; q–)
printf(” “);
for(c=1; c<=r; c++)
printf(“%d”,c);
for(c=r-1; c>=1; c–)
printf(“%d”,c);
printf(“\n”);
}
for(r=1; r<=n; r++)
{
for(q=r; q>=1; q–)
printf(” “);
for(c=1; c<=(n-r); c++)
printf(“%d”,c);
for(c=n-r-1; c>=1; c–)
printf(“%d”,c);
printf(“\n”);
}
printf(“\nPROGRAMMING AT C#ODE STUDIO”);
getch();
return 0;
}

pyramid

BINARY TO DECIMAL;

QUESTION- WRITE A PROGRAM TO CONVERT A NO. FROM BINARY TO DECIMAL USING FN.

ANSWER;

#include<stdio.h>
#include <math.h>
#include<conio.h>
int fn1(long int);
int main()
{
    long int dec,rem,quo;
    printf(“Enter any BINARY number: “);
    scanf(“%ld”,&quo);
    fn1(quo);
    printf(“\n\n\tPROGRAMMING @CODE STUDIO”);
    getch();
}

int fn1(long int quo)
{
    long int rem,dec=0,j=1;
    while(quo!=0)
    {
        rem=fmod(quo,10);
        dec=dec+rem*j;
        j=j*2;
        quo=quo/10;
    }
    printf(“\nEQUIVALENT DECIMAL NO. IS %ld”,dec);
   
}

 

OUTPUT-

bin_dec

PALINDROME;

QUESTION-

Implement a function that receives a phrase and display the longest palindrome as a word in the phrase. The function should also return the length of longest palindrome to the calling function.

ANSWER-

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
     char str[30][50];
     char *p,*t;
     int i,n,x,max=strlen(str[1]);
     printf(“\nNO.of words in phrase:”);
     scanf(“%d”,&n);
     printf(“Enter any PHRASE(every word is followed by enter key):\n “);
     for(i=0;i<n;i++)
     {
         printf(“\t”);
         scanf(“%s”,&str[i]);
     }
     for(i=0;i<n;i++)
    {
         for(p=str[i];*p!=NULL ; p++); //here p stores address for ‘str’ //
              for(t=str[i], p– ; p>=t; ) // t stores address for str//
              {
                if(*p==*t) //if we put * sign over p and t then it means the value of str string variable//
                {
                        p–;
                    t++;
                }
                   else
                    break;
              }
              if(t>p)
              {
                  printf(“\nWORD %d is palindrome”,i+1);
                  if(strlen(str[i])>max)
                  {
                      max=strlen(str[i]);
                      x=i;
                  }
              }
              else
                  printf(“\nWORD %d is Not palindrome”,i+1);
 
     }
    printf(“\nLARGEST PALINDROME WORD IS ~ %s ~ WITH LENGTH ~ %d ~”,str[x],max);
    printf(“\n\n\tProgramming at C#ODE STUDIO”);
      getch(); 
}

 

OUTPUT- Winking smile

palind

DECIMAL TO BINARY (MATH.H, FUNCTION);

QUESTION-

Write a program to convert a binary number to a decimal number using functions.

ANSWER-

#include<stdio.h>
#include <math.h>
#include<conio.h>
int fn1(long int,long int);
int main()
{
    long int dec,rem,quo;
    printf(“Enter any decimal number: “);
    scanf(“%ld”,&dec);
    quo = dec;
    fn1(quo,dec);
    printf(“\n\n\tPROGRAMMING @CODE STUDIO”);
    getch();
}

int fn1(long int quo,long int dec)
{
    long int rem;
    int bin[100],i=1,j;
    while(quo!=0)
    {
        bin[i++]= fmod(quo,2); //here fmod is use to get the remainder of quo by 2//
        quo = quo / 2;
    }
    printf(“Equivalent binary value of decimal number %d: “,dec);
    for(j = i -1 ;j> 0;j–)
        printf(“%d”,bin[j]);
}

 

OUTPUT-

dec_bin