You are on page 1of 4

1.

how do you write a program which produces its own source code as its
output in c
There are 3 types of executable programs (interpreted(basic, perl) , compiled programs
(c, pascal) & semi interpreted (java) )

You can produce its own source code as output only in interpreted programs.

its possible for semi interpreted using a process called as decompilation but the output
may not be exactly same as initial source.

It not possible for compiled program.

example for interpreted program using perl.


=========================

#!/usr/bin/perl

open(INFILE, $0);
while(<INFILE>)
{
print "$_";
}

2. How can I find the day of the week given the date?
#include <stdio.h>
#include <string.h>
/* Prototype Declaration */
int validateDate(int dd, int mm, int yyyy);
void printError();
int calcDay_Dec31(int yyyy);
int dayInYear(int dd, int mm);
void nameInStr (char daysInWord[], int days);
void main(void)
{
     int dd, mm, yyyy;
     int days;
     char daysInWord[11];
     
     /* Read a date and validate the date */

         do{
          printf("Enter a date(dd/mm/yyyy) :");
          scanf("%d / %d / %d", &dd, &mm ,&yyyy);
          fflush(stdin);
         }
         
         while(validateDate(dd, mm, yyyy));
         
         /* Calculate the day for Dec 31 of the previous year */
         days = calcDay_Dec31(yyyy);
         /* Calculate the day for the given date */
         days = (dayInYear(dd, mm) + days) % 7;
         /* Add one day if the year is leap year and desired date is after February */
         if ((!(yyyy % 4) && (yyyy % 100) || !(yyyy % 400)) && mm > 2)
          days++;
         nameInStr(daysInWord, days);
         /* Print the day of the desired date */
         printf("The day for date %d/%d/%d is %snn", dd, mm, yyyy, daysInWord);
} /* main */

int validateDate(int dd, int mm, int yyyy)


{
         int i = 0, j = 0;
         int a[7] = {1, 3, 5, 7, 8, 10, 12};
         int b[4] = {4, 6, 9, 11};
         int error = 0;
         if (mm < 1 || mm > 12)
          error = 1;
         
         
         if (mm == 2)

             {
              if (!(yyyy % 4) && (yyyy % 100) || !(yyyy % 400))

                  {
                   if (dd < 1 || dd > 29)
                    error = 1;
                       
                  }
                  else if (dd < 1 || dd >28)
                   error = 1;
                 }
                 
                 for (i=0;i<6;i+=1)
                 { 
                  if (mm == a[i])

                      {
                       if (dd < 1 || dd > 31)
                        error = 1;
                      }
                     }
                     for (j=0;j<4;j+=1)

                         {
                          if (mm == b[j])

                              {
                               if (dd < 1 || dd > 30)
                                error = 1;
                              }
                             }
                             if (error == 1)
                              printError();
                             return error;
                        }
                        void printError()
                            {
                             printf("Invalid Input!nn");
                        }
                        int calcDay_Dec31(int yyyy)

                            {
                             int dayCode = 0;
                             dayCode = ((yyyy-1)*365 + (yyyy-1)/4 - (yyyy-1)/100 + (yyyy-1)/400) % 7;
                             return dayCode;
                        } /* calcDay_Dec31 */
                        int dayInYear(int dd, int mm)

                            {
                             switch(mm)

                                 {
                                 case 12:dd += 30;
                                 case 11:dd += 31;
                                 case 10:dd += 30;
                                 case 9:dd += 31;
                                 case 8:dd += 31;
                                 case 7:dd += 30;
                                 case 6:dd += 31;
                                 case 5:dd += 30;
                                 case 4:dd += 31;
                                 case 3:dd += 28;
                                 case 2:dd += 31;
                                 }
                                 return dd;
                            } /* dayInYear */
void nameInStr(char daysInWord[], int days)
{
switch(days)

{
case 0:strcpy(daysInWord, "Sunday");break;
case 1:strcpy(daysInWord, "Monday");break;
case 2:strcpy(daysInWord, "Tuesday");break;
case 3:strcpy(daysInWord, "Wednesday");break;
case 4:strcpy(daysInWord, "Thursday");break;
case 5:strcpy(daysInWord, "Friday");break;
case 6:strcpy(daysInWord, "Saturday");break;
                                     }
}

3. Why doesn?t C have nested functions?

#include<stdio.h>

int add(int a,int b)


{
    int c=3;
    int d=a+b;

    int addToC(int c, int d )


    {
        printf("This is addToC.....n");
        return(c+d);
    }
    return(addToC(c,d));

int main()
{
    printf(" Addition   %d n" ,add(1,2));
    return 0;

ANS: c does not have nested functions because c doesnot not support encapsulation.in c each and
every function is independent to each other and performs its assigned task separately

4. What is the most efficient way to count the number of bits which are set in a value?

int fnCountbits(int num )
{
 int iCount = 0;
 while ( num )
 {
   num &= (num-1) ;
  iCount++;
 } 

return iCount;

5. How can I convert integers to binary or hexadecimal?

we can convert the gn integer no into binary and hex no by using %x for binary and %e for
hexadecimal in the printf statement instead of %d

You might also like