Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

What Is Compilier

Posted by Tushar

  • A program that translates an executable 
  • program in one language into an executable program in another language.
  • we expect the program produced by the 
  • compiler to be better, in some way, than the original




Role of Compilers:


High-level programming languages provides following advantages -

  • increase programmer productivity 
  • better maintenance
  • portable
but we require following Low-level machine details too! 
  • instruction selection 
  •  addressing modes
  •  pipelines
  •  registers & cache
  • instruction-level parallelism


The Structure of a Compiler:




Multiple Phases
Modern compilers contain two (large) parts, each of which is often subdivided. These two parts are the front end, shown in green on the right and the back end, shown in pink.

The front end analyzes the source program, determines its constituent parts, and constructs an intermediate representation of the program. Typically the front end is independent of the target language.

The back end synthesizes the target program from the intermediate representation produced by the front end. Typically the back end is independent of the source language.


Advantage of  front/back end division 

This front/back division very much reduces the work for a compiling system that can handle several (N) source languages and several (M) target languages. Instead of NM compilers, we need N front ends and M back ends. For gcc (originally standing for Gnu C Compiler, but now standing for Gnu Compiler Collection), N=7 and M~30 so the savings is considerable.

The front and back end are themselves each divided into multiple phases. The input to each phase is the output of the previous. 
Sometime a phase changes the representation of the input.
For example, the lexical analyser converts a character stream input into a token stream output. 

Sometimes the representation is unchanged. 
For example, the machine-dependent optimizer transforms target-machine code into (hopefully improved) target-machine code.

Conceptually, there are three phases of analysis with the output of one phase the input of the next. The phases are called lexical analysis or scanningsyntax analysis or parsing, and semantic analysis
Read More

Frequently Asked Questions In C

Posted by Tushar Bedekar
                                
Q1. Write a simple program to find the size of different basic data types in c?

Q.2 Write a program in c for showing working of different logical operator in C.
Your program should guide user with proper message/menu on the console.

Q.3 Write a function to find the area of a triangle whose length of three sides is given.

Q.4 Write a C program to read the internal test marks of 25 students in a class and show the number of students who have scored more than 50% in the test.

Q.5 Write a C function to swap two given numbers using call by reference mechanism?

Q.6 Write a C program for finding GCD of two given numbers.

Q.7 Write a C program for addition of two 3x3 matrix.

Q.8 Write a C programme for counting the number of words in a given string?

Q.9 Write a C programme for concatenating two given strings?

Q.10 Write a C program using structure to find students grades in a class?

Q.11 Write a program to insert any 10 numbers and calculate its EVEN SUM,ODD SUM and EVEN AVERAGE,ODD AVERAGE.

Q.12 Write a program to insert any ten numbers and find minimum and maximum numbers.

Q.13 Write a program to calculate a given number is a prime number or not.

Q.14 Write a program to swapping two values using pointer and malloc function.

Q.15 C program for malloc function with example.








 For More Questions Please visit the link Given Below

                                                                 
                                                          

Read More

SORTING A STRING IN AN ALPHABETICAL ORDER:-

Posted by Tushar Bedekar


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sort_string(char*);

int main()
{
   char string[100];

   printf("Enter some text\n");
   gets(string);

   sort_string(string);
   printf("%s\n", string);

   return 0;
}

void sort_string(char *s)
{
   int c, d = 0, length;
   char *pointer, *result, ch;

   length = strlen(s);

   result = (char*)malloc(length+1);       //use of pointer here 

   pointer = s;

   for ( ch = 'a' ; ch <= 'z' ; ch++ )
   {
      for ( c = 0 ; c < length ; c++ )
      {
         if ( *pointer == ch )
         {
            *(result+d) = *pointer;
            d++;
         }
         pointer++;
      }
      pointer = s;
   }
   *(result+d) = '\0';

   strcpy(s, result);
   free(result);

}


Read More

C ANAGRAM PROGRAMMING CODE

Posted by Tushar Bedekar


#include <stdio.h>

int check_anagram(char [], char []);

int main()
{
   char a[100], b[100];
   int flag;

   printf("Enter first string\n");
   gets(a);

   printf("Enter second string\n");
   gets(b);

   flag = check_anagram(a, b);

   if (flag == 1)
      printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
   else
      printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);

   return 0;
}

int check_anagram(char a[], char b[])
{
   int first[26] = {0}, second[26] = {0}, c = 0;

   while (a[c] != '\0')
   {
      first[a[c]-'a']++;
      c++;
   }

   c = 0;

   while (b[c] != '\0')
   {
      second[b[c]-'a']++;
      c++;
   }

   for (c = 0; c < 26; c++)
   {
      if (first[c] != second[c])
         return 0;
   }

   return 1;
}

OUTPUT OF THE PROGRAM ME:-

Download Anagram Programme

Read More

c program me to get IP address of your computer

Posted by Tushar Bedekar 4 Comments


 
 #include<stdlib.h>

int main()
{
system("C:\\Windows\\System32\\ipconfig");


return 0;
}
 

 

 Download ip address programme

Read More

Write a C program to Sort the list of integers using Bubble Sort

Posted by Tushar Bedekar


#include(stdio.h)                                 //note place '<' & '>' in place of '(' & ')'
#include(conio.h)
void bubble(int [ ], int);                                           // func. declaration
void main( )
{
 int a[10],n,i;
clrscr();
printf("Enter the no. of elements u want to fill:");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0; i< n; i++)
scanf("%d",&a[i]);

bubble(a,n);                                                             // calling function
getch();
}

void bubble(int b[], int no)                                            // called function
{
int i,j,temp;
for(i=0; i< no-1; i++)
{
for(j=0; j< no-i; j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
printf("\nAfter sorting : ");
for(i=0; i< no; i++)
printf("%5d",b[i] );
}

Output:- Enter the no. of elements u want to fill: 5
Enter the array elements : 8  1  90  5  4
After sorting : 1  4  5  8  90
Read More

Write a C program to print the Given pyramid

Posted by Tushar Bedekar




      1


    1 2  
   1 2 3 
  1 2 3 4 
 1 2 3 4 5
  .
  .
  .
  n

Solution:-#include(stdio.h)                        // note place '<' & '>' in place of '(' & ')'
#include(conio.h)
int main( )
{
int r,c,n,sp;
clrscr( );
printf("Enter the no. of rows u want to print:");
scanf("%d", &n);
for(r=1; r<=n; r++)

for(sp=n; sp>=r; sp--)
printf(" ");                                                 // give one space in betw quotations.
for(c=1; c<=r; c++)
printf("%2d", c);
printf("\n");
}
getch( );
return 0;
}
Read More

Write a C program to find GCD of each of the two consecutive elements of an array. write a function GCD(a,b) that would take two consecutive elements of the array as arguments and return the GCD

Posted by Tushar Bedekar

#include(stdio.h)                                  // Note place '<' & '>' in place of '(' & ')'
int gcd(int,int);
void main()
{
  int a[10],n,i,b[10];
  int x,y;
  clrscr();
  printf("Enter the size of the array:");
  scanf("%d",&n);
  printf("\nEnter the array elements:");
  for(i=0; i< n ;i++)
    scanf("%d",&a[i]);
 for(i=0; i< n-1 ;i++)
  {
     x=a[i];
     y=a[i+1];
     b[i]=gcd(x,y);
    printf("\nG.C.D of %d and %d is : %d",x,y,b[i]);
  }
getch();
}
int gcd(int a,int b)
{
  int rem;
  for(;;)
  {
    rem=a%b;
    if(rem==0)
       break;
    a=b;
    b=rem;
  }
 return b;
}
Read More

Write a C program to get the maximum and minimum values of a Data type

Posted by Tushar Bedekar

#include(stdio.h)                                     // note place '<' & '>' in place of '(' & ')'
#include(conio.h) 
void main( )
{
int x, y;
clrscr( );
x=1;  
while( x > 0)
{
y = x;
x + + ;
}
printf("\nMaximum value : %d ", y );
printf("\nMinimum value : %d", x);
getch( );
}

Output:- Maximum value : 32767
Minimum value : -32768

Similarly we can find the below datatypes,
long int ( %ld ) 
char ( %d ) 
unsigned ( %u )
.....
Read More

Write a C function that defines two strings as parameters, as follows, and checks whether the second string (str2) is a substring of the first one (str1).

Posted by Tushar Bedekar



int substring_finder(char* str1, char* str2);
The function should return an integer as follows:
• 2 if str2 is a substring of str1 and also of same length
o e.g. str1 = "Hello" and str2= "Hello"
• 1 if str2 is a substring of str1 and of different lengths
o e.g. str1 = "Hello World" and str2= "Hello"
• 0 if str2 is not a substring of str1
o e.g. str1 = "Hello" and str2= "Helo"


 

program me starts from here:-

#include(string.h)                                         //  note place '<' & '>' in place of '(' & ')'
#include(stdio.h)
int substring_finder(char *,char *);
int main()
{
  char *str1,*str2;
  int fl;
  clrscr();
  printf("Enter the main string:");
  gets(str1);
  printf("Enter the sub-string:");
  gets(str2);
  fl=substring_finder(str1,str2);

  if(fl==0)
    printf("\n'str2' is not a substring of 'str1'");
  else if(fl==1)
    printf("\n'str2' is a substring of 'str1'");
  else if(fl==2)
    printf("\nBoth are equal");

getch();
return 0;
}
int substring_finder(char *str,char *sub)
{
  int flag;
  if(strcmp(str,sub)==0)
     flag=2;
  else if(strstr(str,sub))
     flag=1;
  else
     flag=0;

  return flag;
}
Read More

Write a C program that prints a given positive integer in reverse order and also sum of the individual digits involved in the given integer.

Posted by Tushar Bedekar

#include< stdio.h>
#include< conio.h>

int main( )
{
int no, rev=0, rem, sum=0;
clrscr( );
printf("Enter the required integer:");
scanf("%d", &no);
while(no>0)
{
rem = no%10;
rev = (10*rev)+rem;
sum = sum+rem;
no=no/10;
}
printf("\nReverse Number : %d", rev);
printf("\nSum of the individual digits : %d", sum);
getch( );

return 0;
}
Read More

Write C programs that use both recursive and non-recursive functions to find the factorial of a given integer.

Posted by Tushar Bedekar

(a).without using recursive function.

#include(stdio.h)                              // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fact( int);                                        // function declaration
int main( )
{
int no,result;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
result = fact( no);
printf("\n %d Factorial is : %d", no, result);
getch( );
return 0;
}
int fact(int n)
{
int ft,
for( ft=1; n>=1; n--)
ft=ft*n;
return ft;
}

Output:- 4 Factorial is : 24

(b).using recursive function.

#include(stdio.h)                           // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fact( int);                                      // function declaration
int main( )
{
int no,result;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
result = fact( no);
printf("\n %d Factorial is : %d", no, result);
getch( );
return 0;
}
int fact(int n)
{
int ft,
if( n==1)
return 1;
else
ft= n*fact (n-1);
return ft;
}
Read More

Write a C program to find the roots of a quadratic equation using Pointers and Functions.

Posted by Tushar Bedekar

 #include(stdio.h)                    // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(math.h)
void quadratic_roots(double *,double *, double *);
int main(void)
{
double a, b, c;
clrscr( );
printf("Enter the values of a,b and c :");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\nThe required quadratic equation is : ax^2 + bx + c = 0");
printf("\nGiven Values: a = %lf \n b= %lf \n c= %lf ",a ,b, c);
quadratic_roots(&a,&b,&c);
getch( );
return 0;
}

void quadratic_roots(double *aa, double *bb, double *cc)
{
double root1, root2;
if((*bb)*(*bb)= = 4*(*aa)*(*cc))
{
root1= -(*bb)/2*(*aa);
printf("\nRoots are equal");
printf("\nvalue : %lf", root1);

else if((*bb)*(*bb) > 4*(*aa)*(*cc))
{
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("\nThe roots of the equation are: %lf and %lf ", root1, root2);
}
else
{
printf("\nImaginary roots");
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("roots are %f+i(%f) , %f-i(%f)",r1, r2, r1, r2);
}
}
Read More

Design of Pyramids IN C Programming

Posted by Tushar Bedekar


A B C D E F G G F E D C B A      
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

solution:-#include<stdio.h>
#include<conio.h>
int main( )
{
  int r,c,askey;
  clrscr( );
  
  for( r=7; r>=1; r-- )
  {
    askey=65;
    for(c=1; c<=r; c++ )
      printf("%2c", askey++ );
    askey--; 
    for(c=r; c>=1; c-- )
       printf("%2c", askey--);
   
   printf("\n");
  }
  getch();
  return 0;
}

Output:- 
A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

Note:- 
'r' and 'c' means rows and columns .
'askey' variable is for disp. values.
Read More

Write a C program to delete an element from the array

Posted by Tushar Bedekar

#include< stdio.h>
#include< conio.h>
int main( )
{
int a[20], len, i, n, item, j;
clrscr( );
printf("Enter how many elements you want to enter:");
scanf("%d", &len);

printf("\nEnter the array elements:");
for(i=0; i<'len' ; i++)                                 // remove the single quotes in for loop
scanf("%d", &a[ i]);

printf("\nEnter the location, where you want to delete:");
scanf("%d", &n);

item= a[n-1];
printf("\nDeleted value is : %d", item);

for( j= n-1; j<'len-1; j++) 

{ a[ j]= a[ j+1]; }

len=len-1;
printf("\nThe new element list :");
for(i=0; i<'len; i++)

printf("%5d", a[i]);

getch( );
return 0;
}

Output:-
Enter how many elements you want to enter: 4
Enter the array elements:
10
20
30
40
Enter the location, where you want to delete: 3
Deleted value : 30
The new element list: 10 20 40
Read More

Write a C program to display the message "Welcome to C" without a Semicolon

Posted by Tushar Bedekar

// C program without a Semicolon.
This can done in three ways but one of them is infinite.(ie, while loop)
Solution:1
#include(stdio.h) // note place '<' & '>' in place of '(' & ')'
void main( )
{
if(printf("Welcome to C"))
{
}
}

Solution:2
void main( )
{
switch(printf("Welcome to C"))
{
}
}

Solution:3
void main( )
{
while(printf("Welcome to C")) //infinite loop
{
}
}
Read More

Write a C program that adds first seven terms of the following series 1/1! +2/2! + 3/3! + ..........

Posted by Tushar Bedekar


#include< stdio.h>
#include< conio.h>
int main( )
{
int n,i,j,fact;
float sum=0;
clrscr();
for(i=1; i<=7; i++) { fact=1; for(j=i; j>=1; j--)     // to find the factorial
fact=fact*j;

sum=sum+(float)i/fact;
}
printf("\nSum : %f",sum);
getch();
return 0;
}
Read More

Write a C program to find the greatest common divisor of the two given positive integers

Posted by Tushar Bedekar



#include< stido.h>
#include< conio.h>
int main( )
{
int a, b, res;
clrscr( );
printf("Enter the two integer values:");
scanf("%d%d", &a, &b);

for(  ;  ;  )                                                  //This is empty for loop.
{
res = a%b;
if( res = = 0) break;
a = b;
b = res;
}
printf("\n The GCF is : %d", res);
getch( );
return 0;
}
Read More

HEAP SORTING IN C

Posted by Tushar Bedekar



Q. Write a C program to sort 5 numbers using heap sorting method.


Ans.


/* c program for heap sorting method*/

#include<stdio.h>
#include<conio.h>
void manage(int *, int);
void heapsort(int *, int, int);
int main()
{
 int arr[20]; 
 int i,j,size,tmp,k;
 printf("\n\t------- Heap sorting method -------\n\n");
 printf("Enter the number of elements to sort : ");
 scanf("%d",&size);
 for(i=1; i<=size; i++)
 {
   printf("Enter %d element : ",i);
   scanf("%d",&arr[i]);
   manage(arr,i);
 }
 j=size;
 for(i=1; i<=j; i++)
 {
   tmp=arr[1];
   arr[1]=arr[size];
   arr[size]=tmp;
   size--;
   heapsort(arr,1,size);
 }
 printf("\n\t------- Heap sorted elements -------\n\n");
 size=j;
 for(i=1; i<=size; i++)
     printf("%d ",arr[i]);
 getch();
 return 0;
}


void manage(int *arr, int i)
{
 int tmp; 
 tmp=arr[i];
 while((i>1)&&(arr[i/2]<tmp))
 {
   arr[i]=arr[i/2];
   i=i/2;
 }
 arr[i]=tmp;
}


void heapsort(int *arr, int i, int size)
{
 int tmp,j;
 tmp=arr[i];
 j=i*2;
 while(j<=size)
 {
   if((j<size)&&(arr[j]<arr[j+1]))
      j++;
   if(arr[j]<arr[j/2]) 
      break;
   arr[j/2]=arr[j];
   j=j*2;
 }
 arr[j/2]=tmp;
}

/************* OUTPUT ***************/

Screen shot for Heap sorting C program





Read More

C MAGICAL PROGRAM ME:-

Posted by Tushar Bedekar


void main()
{
int a;
a=300*300/300;
printf(“%d”,a);
}
What will be the output of the above Program

Few options which I can give are -

(1)300
(2)1
(3)81
(4)600
The Correct answer for this question is -

Option 3 i.e. 81

Confused???????

Check here



In C integer has range from -32768 to 32767.
Hence          32767 + 1 = -32768
Similarly       32767 + 3 = -32766

For the above question

300*300=90000

The value stored in integer variable ‘a’ is 24300

Now 24300/300 = 81 



Hence the answer is 81
Read More
back to top