Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

C Printing Heart Pattern

c

#include<stdio.h> 
  
int main() { 
    int a, b, line = 12; 
  
    for (a = line/2; a <= line; a = a+2) { //for the upper part of the heart
        for (b = 1; b < line-a; b = b+2) //create space before the first peak         
            printf(" "); 
   
        for (b = 1; b <= a; b++) //print the first peak
            printf("*"); 
   
        for (b = 1; b <= line-a; b++) //create space before the first peak
            printf(" "); 
           
        for (b = 1; b <= a-1; b++) //print the second peak
            printf("*"); 
        printf("\n"); 
    } 

    for (a = line; a >= 0; a--) { //the base of the heart is inverted triangle
        for (b = a; b < line; b++) //generate space before triangle
            printf(" "); 
  
        for (b = 1; b <= ((a * 2) - 1); b++) //print the triangle
            printf("*"); 
  
        printf("\n");   
    } 
}

void pointer in C

c

#include<stdlib.h>
int main() {
	int a = 7;
	float b = 7.6;
	void *p;
	p = &a;
	printf("Integer variable is = %d", *( (int*) p) );
	p = &b;
	printf("\nFloat variable is = %f", *( (float*) p) );
	return 0;
}

NULL pointer in C

c

#include <stdio.h>
int main() {
	int *p= NULL;//initialize the pointer as null.
	printf("The value of pointer is %u",p);
	return 0;
}

C character literals

c

#include<stdio.h>

main(){
	printf("%d", sizeof('a'));
}

Declare a pointer to a function in C

c

#include <stdio.h>
void show(int x) { 
	printf("Value of x is %d\n", x); 
}
int main() { 
	void (*x)(int); x = &show;
	(*x)(7);
	return 0; 
}

Merge contents of two files into a third file using C

c

#include <stdio.h>
#include <stdlib.h>
int main() {
	char file1[100];
	char file2[100];
	char file3[100];
	printf("Enter first file name: ");
	scanf("%s", file1);
	printf("Enter second file name: ");
	scanf("%s", file2);
	printf("Enter third file name: ");
	scanf("%s", file3);
	FILE *fp1 = fopen(file1, "r");
	FILE *fp2 = fopen(file2, "r");
	FILE *fp3 = fopen(file3, "w");
	char c;
	if (fp1 == NULL || fp2 == NULL || fp3 == NULL) {
		puts("Could not open files");
		exit(0);
   }
	while ((c = fgetc(fp1)) != EOF)
		fputc(c, fp3);
	while ((c = fgetc(fp2)) != EOF)
		fputc(c, fp3);
	printf("files are merged");
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	return 0;
}

sortings

c

/*Note:-There may be some errors*/
------------merge---------------------
#include<stdio.h>
#include<conio.h>

#define MAX_SIZE 5

void merge_sort(int, int);
void merge_array(int, int, int, int);

int arr_sort[MAX_SIZE];

int main() {
  int i;

  printf("Simple Merge Sort Example - Functions and Array\n");
  printf("\nEnter %d Elements for Sorting\n", MAX_SIZE);
  for (i = 0; i < MAX_SIZE; i++)
    scanf("%d", &arr_sort[i]);

  printf("\nYour Data   :");
  for (i = 0; i < MAX_SIZE; i++) {
    printf("\t%d", arr_sort[i]);
  }

  merge_sort(0, MAX_SIZE - 1);

  printf("\n\nSorted Data :");
  for (i = 0; i < MAX_SIZE; i++) {
    printf("\t%d", arr_sort[i]);
  }
  getch();

}

void merge_sort(int i, int j) 
{
  int m;

  if (i < j)
   {
    m = (i + j) / 2;
    merge_sort(i, m);
    merge_sort(m + 1, j);
    merge_array(i, m, m + 1, j);
  }
}

void merge_array(int a, int b, int c, int d) 
{
  int t[50];
  int i = a, j = c, k = 0;

  while (i <= b && j <= d) 
  {
    if (arr_sort[i] < arr_sort[j])
      t[k++] = arr_sort[i++];
    else
      t[k++] = arr_sort[j++];
  }
  while (i <= b)
    t[k++] = arr_sort[i++];

  while (j <= d)
    t[k++] = arr_sort[j++];

  for (i = a, j = 0; i <= d; i++, j++)
    arr_sort[i] = t[j];
}
----------------------quick------------------------------
#include <stdio.h>
 
void quicksort (int [], int, int);
 
int main()
{
    int list[50];
    int size, i;
 
    printf("Enter the number of elements: ");
    scanf("%d", &size); 
    printf("Enter the elements to be sorted:\n");
    for (i = 0; i < size; i++)
    {
        scanf("%d", &list[i]);
    } 
    quicksort(list, 0, size - 1);
    printf("After applying quick sort\n");
    for (i = 0; i < size; i++)
    {
        printf("%d ", list[i]);
    }
    printf("\n");
 
    return 0;
}
void quicksort(int list[], int low, int high)
{
    int pivot, i, j, temp;
    if (low < high)
    {
        pivot = low;
        i = low;
        j = high;
        while (i < j) 
        {
            while (list[i] <= list[pivot] && i <= high)
            {
                i++;
            }
            while (list[j] > list[pivot] && j >= low)
            {
                j--;
            }
            if (i < j)
            {
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
        }
        temp = list[j];
        list[j] = list[pivot];
        list[pivot] = temp;
        quicksort(list, low, j - 1);
        quicksort(list, j + 1, high);
    }
}
--------------------------heap-----------------------------------
#include <stdio.h>
int main()
{
    int heap[10], no, i, j, c, root, temp;
    clrscr();
    printf("\n Enter no of elements :");
    scanf("%d", &no);
    printf("\n Enter the nos : ");
    for (i = 0; i < no; i++)
       scanf("%d", &heap[i]);
    for (i = 1; i < no; i++)
    {
	c = i;
	do
	{
	    root = (c - 1) / 2;
	 if (heap[root] < heap[c])   /* to create MAX heap array */

	    {

		temp = heap[root];

		heap[root] = heap[c];

		heap[c] = temp;

	    }

	    c = root;

	} while (c != 0);

    }
    printf("Heap array : ");

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

	printf("%d\t ", heap[i]);

    for (j = no - 1; j >= 0; j--)

    {

	temp = heap[0];

	heap[0] = heap[j];    /* swap max element with rightmost leaf element */

	heap[j] = temp;

	root = 0;

	do

	{

	    c = 2 * root + 1;    /* left node of root element */

	    if ((heap[c] < heap[c + 1]) && c < j-1)

		c++;

       if (heap[root]<heap[c] && c<j)    /* again rearrange to max heap array */

	    {

		temp = heap[root];

		heap[root] = heap[c];

		heap[c] = temp;

	    }

	    root = c;

	} while (c < j);

    }

    printf("\n The sorted array is : ");

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

       printf("\%3d", heap[i]);

    printf("\n Complexity : \n Best case = Avg case = Worst case = O(n logn) \n");
 return 0;

}
---------------------------------selection------------------------------
#include <stdio.h>
 
int main()
{
   int array[100], n, c, d, position, swap;
 
   printf("Enter number of elements\n");
   scanf("%d", &n);
 
   printf("Enter %d integers\n", n);
 
   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);
 
   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;
 
      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }
 
   printf("Sorted list in ascending order:\n");
 
   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);
 
   return 0;
}
--------------------------------shell---------------------------------
#include<stdio.h>
int main()
{
    int t,gap,i,A[100],n;
	printf("enter n\n");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	scanf("%d",&A[i]);
	gap=n;
	while(gap>1)
	{
	gap=(gap+1)/2;
	for(i=0;i<n-gap;i++)
	{
	if(A[i+gap]<A[i])
	{
	t=A[i];
	A[i]=A[i+gap];
	A[i+gap]=t;
	}
	}	 
     } 
	printf("The Sorted Elements are\n");
	for(i=0;i<n;i++)
	printf("%3d",A[i]);
}
------------------------------insertion-------------------------------------
#include<stdio.h>
void main()
{
    int i,j,k,z,a[100],n;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for (i=1;i<n;i++)
    {
	k=a[i];
	j=i-1;
	while((j >= 0) && (k < a[j]))
	{
	    a[j+1]=a[j];
	    j--;
	}
	a[j+1]=k;
    }
    for(z=0;z<n;z++)
       {
	printf("%d ",a[z]);
       }
       
}

C Program to find size of a File

c

#include <stdio.h>
int findfileSize(char f_n[]) {
	FILE* fp = fopen(f_n, "r");
	if (fp == NULL) {
		printf("File Not Found!\n");
		return -1;
	}
	fseek(fp, 0L, SEEK_END);
	int res = ftell(fp);
	fclose(fp);
	return res;
}
int main() {
	char f_n[] = { "b.txt" };
	int result = findfileSize(f_n);
	if (result != -1)
		printf("Size of the file is %ld bytes \n", result);
	return 0;
}

Practica A R R

c

//Programa 2//
//Rodríguez Ruíz Angélica TESOEM Matricula 173010068 Grupo 4I12 Ingeniería Industrial//

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

int main()
{
    int radio;
    float p;
    
    printf("Este programa calcula el perimetro de una circunferencia\n");
    printf("\n Caltura el valor del radio\n");
    scanf("%d",&radio);
    p=2*3.14*radio;
    printf("\n perimetro es: %f",p);
    getch();
}

=====

c

#include <stdio.h>
int main()
{
    typedef struct date
{
     int d,m,y;
}date;
    date d1;
    scanf("%d%d%d",&d1.d,&d1.m,&d1.y);
    switch(d1.m)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        if(d1.y>0)
        {
          if(d1.d<=31 && d1.d>=1)
          {
              if(d1.d<=30 && d1.d>=1)
              {
                  d1.d+=1;
              }else if(d1.d==31)
              {
                  d1.d=1;
                  d1.m+=1;
              }printf("%02d-%02d-%04d\n",d1.d,d1.m,d1.y);
        
    }else{
    printf("invalid date");
    }
    }else 
    printf("invalid year");
    break;
        case 4:
        case 6:
        case 9:
        case 11:
        if(d1.y>0)
        {
           if(d1.d<=30 && d1.d>=1)
          {
              if(d1.d<=29 && d1.d>=1)
              {
                  d1.d+=1;
              }else if(d1.d==30)
              {
                  d1.d=1;
                  d1.m+=1;
              }printf("%02d-%02d-%04d\n",d1.d,d1.m,d1.y);
        
    }else {
        printf("invalid date");
    }
    }else 
    printf("invalid year");
    break;
        case 2:
        if(d1.y>0)
        {
          if((d1.y)%4==0)
           {
               if(d1.d<=28 && d1.d>=1)
               {
                 d1.d+=1;
           }else if(d1.d==29 )
           {
               d1.d=1;
               d1.m+=1;
           printf("%02d-%02d-%04d\n",d1.d,d1.m,d1.y); 
           }else {
        printf("invalid date");
    }break;
           }else if(d1.d<=27 && d1.d>=1)
               {
                 d1.d+=1;
           }else if(d1.d==28)
           {
               d1.d=1;
               d1.m+=1;
           printf("%02d-%02d-%04d\n",d1.d,d1.m,d1.y); 
           }
           else {
        printf("invalid date");
    }
    }else 
    printf("invalid year");
    break; 
               
        case 12:
               if(d1.y>0)
               {
               if(d1.d<=31 && d1.d>=1)
               {
                  if(d1.d<=31 && d1.d>=1)
               {
                   d1.d+=1;
               }else if(d1.d==31)
               {
             d1.d=1;
             d1.m=1;
             d1.y+=1;
               }
             printf("%02d-%02d-%04d\n",d1.d,d1.m,d1.y);
               }else 
                printf("invalid date");
                }else
                printf("invalid year");
                break;
        default :
        printf("invalid month");
}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.