C Programs for you – Set 3

C Programs for you – Set 3

Q1. Write a program to generate the Fibonacci series.

rn

Answer:

rn

#include <stdio.h>

rn

 void main()

rn

{

rn

    int  fib1 = 0, fib2 = 1, fib3, limit, count = 0;

rn

     printf(“Enter the limit to generate the Fibonacci Series n”);

rn

    scanf(“%d”, &limit);

rn

    printf(“Fibonacci Series is …n”);

rn

    printf(“%dn”, fib1);

rn

    printf(“%dn”, fib2);

rn

    count = 2;

rn

    while (count < limit)

rn

    {

rn

        fib3 = fib1 + fib2;

rn

        count++;

rn

        printf(“%dn”, fib3);

rn

        fib1 = fib2;

rn

        fib2 = fib3;

rn

    }

rn

}

rn

Q2. Write a program to compare two strings without using strcmp() function.

rn

Answer:

rn

#include<stdio.h>

rn

 int main()

rn

{

rn

            char str1[20], str2[20];

rn

            int i=0, c=0;

rn

            printf(“nEnter first string :: “);

rn

            gets(str1);

rn

            printf(“nEnter second string :: “);

rn

            gets(str2);

rn

            printf(“nStrings are :: nn”);

rn

    puts(str1);

rn

    puts(str2);

rn

            while((str1[i]!=’’) || (str2[i]!=’’))

rn

    {

rn

                        if(str1[i]!=str2[i])

rn

                        c++;

rn

                        i++;

rn

            }

rn

            if(c==0)

rn

                        puts(“nStrings are equal.n”);

rn

            else

rn

                        puts(“nStrings are not equal.n”);

rn

 

rn

            return 0;

rn

}

rn

Q3. Write a program to concatenate two strings without using strcat() function.

rn

Answer:

rn

#include<stdio.h>

rn

void main(void)

rn

{

rn

  char str1[25],str2[25];

rn

  int i=0,j=0;

rn

  printf(“nEnter First String:”);

rn

  gets(str1);

rn

  printf(“nEnter Second String:”);

rn

  gets(str2);

rn

  while(str1[i]!=’’)

rn

  i++;

rn

  while(str2[j]!=’’)

rn

  {

rn

    str1[i]=str2[j];

rn

    j++;

rn

    i++;

rn

  }

rn

  str1[i]=’’;

rn

  printf(“nConcatenated String is %s”,str1);

rn

}

rn

Q4. Write a program to print “Hello World “without using semicolon anywhere in the code.

rn

Answer:

rn

#include<stdio.h>

rn

void main(){

rn

    if(printf(“Hello world”)){

rn

    }

rn

}

rn

Solution: 2

rn

#include<stdio.h>

rn

void main(){

rn

    while(!printf(“Hello world”)){

rn

    }

rn

}

rn

Solution: 3

rn

#include<stdio.h>

rn

void main(){

rn

    switch(printf(“Hello world”)){

rn

    }

rn

}

rn

Q5. Write a program to print a semicolon without using a semicolon anywhere in the code.

rn

Answer:

rn

#include <stdio.h>

rn

void main(void) {

rn

    //prints the character with ascii value 59, i.e., semicolon

rn

    if (printf(“%c “, 59)) {

rn

      //prints semicolon

rn

    }

rn

}

c