Posts

Showing posts with the label C Language

Pascal Triangle

The below program is going to print the Pascal Triangle: 0 row = 1 1 row = adding the two numbers above them to the left and the right          = (0+1) , (1+0)          = 1 , 1 2 row = (0+1) , (1+1) , (1+0)             = 1 , 2 , 1 3 row = (0+1), (1+2), (2+1), (1+0)           = 1 , 3 , 3 , 1 4 row = (0+1), (1+3)  , (3+3), (3+1), (1+0)           =  1 , 4 , 6 , 4 , 1  1 1        1 1      2       1 1      3       3      1  1      4        6     4        1 #include <stdio.h> #include <conio.h> void main() { int i,j,n,c,k,space; clrscr(); printf("Enter the limit "); scanf("%...

C program to print prime numbers between 1 to n.

In this post we are going to write a simple C program which prints prime numbers from 1 to n. Prime numbers are those numbers whose only two whole- number  factors are 1 and itself.   - 1 is not a prime number #include < stdio.h > #include < conio.h > int main() {  int nr,n,div,p;  printf("Enter any number: ");  scanf("%d",  & nr);  for(n=2; n<=nr; n++)  {   for(div=2; div<n; div++)   {    if(n%div==0)    {      p=0;       break ;    }    p=1;   }   if(p)     printf("\t%d",n);  }   return 0 ; }

C program to find the factorial value of a number.

/*c program to find out factorial value of a number*/ #include < stdio.h > #include < conio.h > int main() {  int n,i,factorial=1;  printf("Enter any number : ");  scanf("%d",  & n);  for(i=1; i<=n; i++)     factorial = factorial * i;  printf("Factorial value of %d = %d",n,factorial);   return 0 ; }

Quick Sort

Quick sort  is a divide and conquer algorithm. Its divided large list in mainly three parts: 1.       Elements less than pivot element. 2.       Pivot element. 3.       Elements greater than pivot element. Where pivot as middle element of large list. Let’s understand through example: List : 3 7 8 5 2 1 9 5 4 In above list assume  4  is pivot element so rewrite list as:   3 1 2   4   5 8 9 5 7 Here, I want to say that we set the  pivot element ( 4 ) which has in left side elements are less than and right hand side elements are greater than. Now you think, how’s arrange the less than and greater than elements? Be patient, you get answer soon. Now let’s start understand the concept of quick sort. The  steps are : 1.      Pick a pivot element. 2.      Reorder the list so that all elements with values less than the...

Find Max and Min

/*Program ti find minimum and maximum number when the user gives ten numbers*/ #include<stdio.h> #include<conio.h> int main() {  int arr[10];  int i,num,min,max;  for(i=0; i<10; i++)  {    printf("Enter %d Number : ",i+1);    scanf("%d",&arr[i]);  }  min=max=arr[0];  for(i=0; i<10; i++)  {    if(arr[i] < min)       min=arr[i];    else if(arr[i] > max)       max=arr[i];  }  printf("\nMaximum number is %d",max);  printf("\nMinimum number is %d",min);  getch();  return 0; }

Fibonacci Series

Fibonacci numbers are in the following integer sequence : 0 1 1 2 3 5 8 13 21 34 55 89 ... /*Print Fibonacci series*/ #include<stdio.h> #include<conio.h> int main() {  int x,y,z;  x=0;  y=1;  z=x+y;  printf("%d\t",x);  x=y;  y=z;  getch();  return 0; }

Check if a point is inside of a Triangle

/*In this program you will give 4 coordinates, 3 that will form the triangle and it will check if the fourth is inside or out the triangle*/ #include<stdio.h> #include<conio.h>     struct point{ int x,y;};     int main()     {      point a, b, c,d;     int i;     printf("First coordinate  a (x;y) :");     scanf("%d%d", &a.x, &a.y);     printf("Second coordinate b (x;y) :");     scanf("%d%d", &b.x, &b.y);     printf("Third coordinate c (x;y) :");     scanf("%d%d", &c.x, &c.y);     printf("Fourth coordinate d (x;y) :");     scanf("%d%d", &d.x, &d.y);             if ( ((a.x <= d.x) && (b.x <= d.x) && (c.x <= d.x)) ||((a.x >= d.x) && (b.x >= d.x) && (c.x >= d.x)) ||          ((a.y <...

Print Asterisk Triangle

C program to print the following triangle: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #include<stdio.h>  #include<conio.h>  int main()  {   int num=6,r,c,sp;   for(r=1;  num>=r; r++)   {    for(sp=num-r;  sp>=1; sp--)         printf(" ");    for( c=r;  c>=1; c--)          printf("*");     for(c=r;  c>1; c--)          printf("*");     printf("\n");   }      return 0;    }

Counts characters and words in a string

/*This program counts the characters in a string and the words in it */ #include<stdio.h> #include<conio.h> int main() {  int count_words=0,i;  int count_char=0;  char str[20];  printf("Enter string: ");  gets(str);  for(i=0; str[i]!=NULL; i++)  {    char++;    if(str[i]==' ') /*Controlls if the character is a space*/       words++;  }  printf("\nNumber of characters : %d",char);  printf("\nNumber of words: % d",words+1);  getch();  return 0; }      Output : Enter string : programming geek Number of characters in string : 16 Number of words in string : 2

Selection sort - C Program

Write a C program to sort a list of elements using selection sort. #include<stdio.h> #include<conio.h> #define SIZE 10 int main() {  int i,j,min,tmp;  int arr[SIZE];  for(i=0; i<SIZE; i++)  {   printf("Enter elements : ");   scanf("%d",&arr[i]);  }  for(i=0; i<SIZE; i++)  {    min=i;    for(j=i+1; j<SIZE; j++)      if(arr[j]<arr[min])         min=j;      tmp=arr[i];      arr[i]=arr[min];      arr[min]=tmp;  }  printf("After selection sort the elements are:\n");  for(i=0; i<SIZE; i++)     printf("%d\t",arr[i]);  return 0; } Output: Enter elements : 12 Enter elements : 4 Enter elements : 47 Enter elements : 56 Enter elements : 5 Enter elements : 14 Enter elements : 89 ...

Hello World

Print message on screen - First C Program /* Hello World program */ #include<stdio.h> main() { printf("Hello World"); }