Skip to main content

Posts

queue operations

#include<stdio.h> int N=30; int i; int arr[30]={0}; void enqueue(int n); void dequeue(void); void display(void); int main() {   enqueue(5);   enqueue(6);   display();   dequeue();   display(); } void enqueue(int n) {    for(i=N-1;i>=0;i--)    if(arr[i]==0)    {       arr[i]=n;       break;    } } void dequeue(void) {    for(i=N-1;i>=0;i--)    if(arr[i]!=0)    {       arr[i]=0;       break;    } } void display(void) {    for(i=0;i<N;i++)    if(arr[i]!=0)    printf("%d ",arr[i]);   printf("\n"); } 

stack operations

 #include<stdio.h> int N=30; int i; int arr[30]={0}; void push(int n) {    for(i=N-1;i>=0;i--)    if(arr[i]==0)    {       arr[i]=n;       break;    } } void pop(void) {    for(i=0;i<N;i++)    if(arr[i]!=0)    {       arr[i]=0;       break;    } } void display(void) {    for(i=0;i<N;i++)    if(arr[i]!=0)    printf("%d ",arr[i]);   printf("\n"); } int main() {    push(6);   push(4);   push(7);   push(3);   push(2);    display();    push(5);    display();    pop();    display();     }

PROGAM TO SORT ARRAY AT USER DEFINED POSITION

 #include<iostream> using namespace std; int main() {   int size,i,j,position,temp;   cout<<"Enter the size of array:-\t";   cin>>size;   int array[size];   for(i=0;i<size;i++)     cin>>array[i];   cout<<"Enter the index number from where you want to sort array in ascending order:-\n";   cin>>position;   for(i=position-1;i<size;i++)     for(j=position;j<size;j++)       if(array[i]<array[j])       {         temp=array[i];         array[i]=array[j];         array[j]=temp;       }   cout<<"New sorted array is :-\n";   for(i=0;i<size;i++)     cout<<array[i]<<" "; }

PROGRAM TO DELETE ELEMENT OF ARRAY

 #include<iostream> using namespace std; int main() { int n,m,temp,count=0;   cout<<"Enter size of array:-\t"; cin >>n; int arr[n];   cout<<"Enter elements of array:-\t"; for (int i = 0; i < n; i++) cin >> arr[i];   cout<<"Enter element of array you want to delete:-\t";   cin>>m;   for(int i=0;i<n;i++)     {        count+=1;       if(arr[i]==m)         for(int j=count;j<n;j++)             arr[j-1]=arr[j];     }   cout<<"New Array is :-\t";   for(int i = 0; i < n-1; i++) cout<< arr[i]<<" "; }

file handling

 #include<stdio.h> #include<stdlib.h> void write(int num) { FILE *fp; fp=fopen("storeNum.txt","w"); fprintf(fp,"%d",num); fclose(fp); printf("succesfully stored"); printf("\nQuitting the program...."); } void display(void) {    system("clear");    int num3;    FILE *fp;    fp=fopen("storeNum.txt","r");    fscanf(fp,"%d",&num3);    printf("Displaying numbee\n");    printf("%d\n",num3);    fclose(fp);    printf("Quitting the program...."); } void check(int num2) { int num4;    int x;    FILE *fp;    fp=fopen("storeNum.txt","r");    fscanf(fp,"%d",&num4);    if(num2==num4)    {    printf("It already exist\n");    printf("Quitting the program....");    fclose(fp);    }    else    {    printf("Not exist \nPress\n1.to save\n ");    scanf("%d"...

pointer in c

  #include   < stdio.h > void  update( int  *a, int  *b) {      int  m=*a,n=*b,i= 2 ;      *a=*a+*b;       if (m>n)      *b=(m-n);       else      *b=(m-n)+(-i)*(m-n); } int  main() {      int  a, b;      int  *pa = &a, *pb = &b;          scanf( "%d %d" , &a, &b);     update(pa, pb);     printf( "%d\n%d" , a, b);      return   0 ; }

playing with characters

  #include   < stdio.h > #include< stdlib.h > int  main()  {      char  c, ch[ 20 ],cham[ 50 ];      scanf( "%c" ,&c);      scanf( "%s\n" ,&ch);       scanf( "%[^\n]s" ,&cham); printf( "%c\n" ,c); printf( "%s\n" ,ch); printf( "%s" ,cham); return   0 ; }

problem on function

  #include< stdio.h > int  sum; int  max_of_four( int  a, int  b, int  c, int  d) {    if (a>b&&a>c&&a>d)   {       sum=a;   }    if (b>a&&b>c&&b>d)   {       sum=b;   }    if (c>b&&c>a&&c>d)   {       sum=c;   }    if (d>b&&d>c&&d>a) {     sum=d; } return  sum; } int  main() {      int  p,q,r,s;     scanf( "%d%d%d%d" ,&p,&q,&r,&s);     printf( "%d" ,max_of_four(p,q,r,s)); }

problem on loop

  #include< stdio.h > int  main() {      int  n,m;     scanf( "%d %d" ,&n,&m);      do {          switch (n)     {          case   1 :         printf( "one\n" );          break ;          case   2 :         printf( "two\n" );          break ;          case   3 :         printf( "three\n" );          break ;          case   4 :         printf( "four\n" );      ...

printing bigger number goto statement in c

 #include <stdio.h> int main() {     int i,num,a=1,b;     for(i=0;i<100;i++)     {          printf("Enter number:-\nEnter 0 to quit the program:-\n");          scanf("%d",&num);          if(a<num)          a=num;          else          printf("Number is less than previous bigger number\n");         if(num==0)         {             goto QUIT;         }         printf("The bigger number is %d \n",a);     }      QUIT:     printf("Quitting the program\n");     printf("BYE BYE !!!!!");     return 0; }

Example of arithmetic operator in c

  #include <stdio.h> int main() { int a=40,b=20, add,sub,mul,div,mod; add = a+b; sub = a-b; mul = a*b; div = a/b; mod = a%b; printf("Addition of a, b is : %d\n", add); printf("Subtraction of a, b is : %d\n", sub); printf("Multiplication of a, b is : %d\n", mul); printf("Division of a, b is : %d\n", div); printf("Modulus of a, b is : %d\n", mod); }

Example of ASSIGNMENT OPERATORS IN C

  // to divide #include<stdio.h> int main() { int a =12; printf("%d\n",a/=2);                                 // it simply divide a by 2 return 0; } // to add #include<stdio.h> int main() { int a =12; printf("%d\n",a+=1);                               // it simply add 1 to a return 0; } // to subtract #include<stdio.h> int main() { int a =12; printf("%d\n",a-=5);                            // it simply subtrac...

1-D & 2-D array

 #include<stdio.h> int main() {     int marks[]={25,28,27};     marks[3]=29;     printf("The of student 1 is:-%d\n",marks[0]);     printf("The of student 2 is:-%d\n",marks[1]);     printf("The of student 3 is:-%d\n",marks[2]);     printf("The of student 4 is:-%d\n",marks[3]);     printf("The average of student marks is %d:-",(marks[0]+marks[1]+marks[2]+marks[3])/4);     return 0; }     //Another code  2-D array      #include<stdio.h> int main() {     int marks[2][5]={{95,93,97,87,94},    here 2 represents no of rows & 5 s represents no of column.                      {87,89,88,85,83}};     for (int i = 0; i < 2; i++)     {         for (int j = 0; j < 5; j++)         {             printf("Th...

program to check whether a number is prime number or not.

#include<stdio.h> int main() {     int a,i,j;     printf("Enter a range of numbers to check prime number between them\n");     printf("Enter the first number\n");     scanf("%d",&i);     printf("Enter the second number\n");     scanf("%d",&j);      for(a=i;a<=j;a++)      {     if (a%1==0&&a%a==0&&a%2!=0&&a%3!=0&&a%4!=0&&a%5!=0&&a%6!=0&&a%7!=0&&a%8!=0&&a%9!=0)     {         printf("%d is prime number\n",a);     }     else     printf("%d is not a prime number\n",a);      } } there is mistake in it find it out.

strings in c

#include <stdio.h> int main() { char name1[] = { 's' , 'a' , 't' , 'y' , 'a' , 'm' , ' \0 ' ,}; printf ( "%s" ,name1); char name2[] = " \n satyam" ; printf ( "%s" ,name2); return 0 ; }

example of printing star pattern

#include <stdio.h> void starPattern( int rows ) { for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j <= i; j ++ ) { printf ( "*" ); } printf ( " \n " ); } } void reverseStarPattern( int rows ) { for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j <= rows - i - 1 ; j ++ ) { printf ( "*" ); } printf ( " \n " ); } } int main() { int rows, type; printf ( "Enter 0 for star pattern \n Enter 1 for reversed star pattern \n " ); scanf ( "%d" , & type); printf ( "How many rows do you want? \n " ); scanf ( "%d" , & rows); switch (type) { case 0 : starPattern (rows); break ; case 1 : reverseStarPattern (rows); break ; default: printf ( "You have entered an invalid choice" ); break ; } return 0 ; }