Posts

Showing posts from September, 2018

Given a sentence, , print each word of the sentence in a new line.

#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() {     char *s,tmp;     int len,i;     s = malloc(1024 * sizeof(char));     scanf("%[^\n]", s);     s = realloc(s, strlen(s) + 1);     //Write your logic to print the tokens of the sentence here.         len=strlen(s);     for(i=0;i<len;i++){         tmp=s[i];         if(tmp==' '){     printf("\n");             continue; }         printf("%c",s[i]);             }     return 0; }

spiral like pattern program in c

#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() {     int n,i,j,boundry,num;     scanf("%d", &n); boundry=(n*2)-1;     for(i=0;i<boundry;i++){      for(j=0;j<boundry;j++){           if(i<j) num=i; else num=j;          if(num>=boundry-i) num=boundry-i-1;          if(num>=boundry-j-1) num=boundry-j-1;          printf("%d ",n-num);                //printf("sa");     }          printf("\n");     }         return 0; }

bitwise AND OR NOT operations between numbers c program

Image
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> //Complete the following function. void calculate_the_maximum(int n, int k) {   //Write your code here.     int a1=0,o1=0,xo1=0,tmp1,tmp2,tmp3;     int s[n+1],i,j;     for(i=0;i<n;i++){         s[i]=i+1;     }       for(i=0;i<n;i++){         for(j=i+1;j<n;j++){          if(o1<s[i]|s[j]){              tmp1=s[i]|s[j];              if(tmp1<k){                              o1=s[i]|s[j];                                      }                            }                                 tmp2=s[i]&s[j];            if(a1<tmp2){              if(k>tmp2) {a1=s[i]&s[j];                         // for testing           printf("\n %d&%d=%d & k=%d",s[i],s[j],s[i]&s[j],k);                         }                                               }                                 tmp3=s[i]^s[j];            if

C program to reverse a string without using any string functions-asr

Image
#include <stdio.h> #include <conio.h> void main() { char *s; int len,i=0; clrscr(); printf("\nENTER A STRING: "); gets(s); while(1){ if(s[i]=='\0'){ len=i; break; }          i++; }        for(i=len;i>=0;i--){ printf("%c",*(s+i));        } getch(); }

c patterns level 2

Image
/******************************************************************************                             Online C Compiler.                 Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <stdio.h> int main() {     int i,j,k=1,l=9,tmp;     for(i=1,k=1;i<=10;i++,k=k+2,l--){           tmp=l;         for(j=1;j<=19;j++){                     if(j<=k){                        if(j<=i) {                ++tmp;                if(tmp!=10) printf("%d",tmp);                else printf("0");            }            else {                --tmp;                if(tmp!=10) printf("%d",tmp);                else printf("0");            }           }                                                     }                        

patterns programs in c

Image
/******************************************************************************                             Online C Compiler.                 Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <stdio.h> int main() {     int i,j,k,l;     for(i=1;i<=9;i++){               for(j=1;j<=5;j++){                      if(i<6){if(j<6-i) printf("  ");             else printf("* ");         }         else{             if(j<=i-5) printf("  ");             else printf("* ");         }               }         printf("\n");           }     return 0; } /******************************************************************************                             Online C Compiler.                 Code, Compil

task : debug the prime number logic

#include <stdio.h> #include <math.h> void main(){   int x,n=0,i; scanf("%d",&x); for(i=0;i<(int)sqrt(x);i++){     if(x%i==0){     n=1;     break;     }     } if(n==1) printf("not"); else printf("PRIME"); }

reverse of given string worlds c program -asr

Image
#include <stdio.h> void main(){         int i=0,len,num=0,a[10],j,f,l;     char s[100];                 while(1){         s[i]=getchar();         if(s[i]=='\n'){                 len=i;           //get chatacters                 s[i]=' ';        //set last character as space             break;   //break if enter pressed         }                 i++;     }         for(i=0;i<len;i++){     if(s[i]==' '){         a[num]=i+1;     //detect the positions where space is present         num++;     }     }    a[num]=len+1;    //insert length in top index + incremented for including the space that we assigned for(i=num;i>=0;i--){         f=a[i];     l=a[i-1]; for(j=l;j<f;j++){     printf("%c",s[j]);   //prints reverse     } } }

kite patterns in c

Image
/******************************************************************************                             Online C Compiler.                 Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <stdio.h> int main() {     int i,j,k;     for(i=1;i<=6;i++){             for(j=1;j<=5;j++){         if(i==1||i==5) {                        if(j==3) printf("*");             else printf(" ");                     }            if(i==2||i==4||i==6) {                        if(j>1&&j<5) printf("*");             else printf(" ");                     }            if(i==3) {                        printf("*");                               }                                     }         printf("\n"

beautiful stars pattern based on inputted number-asr

Image
/******************************************************************************                             Online C Compiler.                 Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <stdio.h> #include <stdlib.h> int main() {     char a[20],n[10]; int num[10],x=0; int i=0,big=1,j; while(1){ a[i]=getchar(); n[0]=a[i]; //assignt into n[0] otherwise will err...coz index start from 0 num[x]=atoi(n);    if(a[i]=='\n'){     break;    }  i++; x++; } big=num[0]; for(i=1;i<x;i++){       if(big<num[i])   {         big=num[i];      //finding big number from input       }    } for(i=0;i<=big;i++){      for(j=0;j<x;j++){             if(i==big)       {           printf("%d ",num[j]);  // print numbers in bottom       }  

aaabb to32 ,aabbb to a2b3 convert in c-asr

Image
/******************************************************************************     Online C Compiler. Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> int main() {   char s[10],c[10];   int i=1,j,tmp[10];   int p[10];   static int x,num=0;   s[0]='*';   c[0]='*';   tmp[0]=0;  printf("\nEnter a string\n");  while(1){     s[i]=getchar(); //reads single characters until enter pressed      if(s[i]==s[i-1])    //checks the previous character matched or not      { x++; tmp[num]=x; //increment x as much as character matches the previous characters insert into tmp      }      else{ x=1; c[num]=s[i-1]; num++;          tmp[num]=x;  //if not matched then x is 1 as above in sta

a2b3 output aabbb c program and beautiful use of getchar() ,isdigit() ,atoi() functions and stdlib.h header file-asr

Image
a2b3 output aabbb c program Beautiful use of getchar() ,isdigit() ,atoi() functions and stdlib.h header file i noted in string comparison single quote used,while in initialization double quotes /******************************************************************************                             Online C Compiler.                 Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <stdio.h> #include <stdlib.h> int main() {   char s[10],n[10],c[10];   int i=0,j,num=0;   int p[10];  while(1){     s[i]=getchar(); //reads single characters until enter pressed     if(isdigit(s[i]))    //checks the character is digit or not      {          n[0]=s[i];  //digit assigned in array          c[num]=s[i-1]; //previous character of digit is stored for printing          p[num]=at

largest elemet in array-asr

largest elemet in array C program /******************************************************************************                             Online C Compiler.                 Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <stdio.h> int main() { int i,n,a[20],max; printf("enter any number"); scanf("%d",&n); for(i=0;i<n;i++){     scanf("%d",&a[i]);   } max=a[0]; for(i=1;i<n;i++){ if(max<a[i]){    max=a[i]; }  } printf("%d",max);     return 0; }

fibonacci upto n-asr

/******************************************************************************                             Online C Compiler.                 Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <stdio.h> int main() { int a=0,b=1,c=0,n;     printf("Enter n"); scanf("%d",&n); while(1){       c=a+b;      if(c>n){         break;     }     printf("%d ",c);     a=b;     b=c;   }     return 0; }

prime number using square root -asr

Image
prime number c program using square root function -asr #include <stdio.h> #include <conio.h> #include <math.h> void main(){      int i,n,r,a=0; clrscr(); printf("Enter number\n"); scanf("%d",&n); if(n%2==0||n%3==0||n%5==0||n%7==0)     {   r=1;   goto check;     } for(i=11,a=1;i<= (int)sqrt(n); i=i+2,a++)          {              if(n%i==0)                                 {                                   r=1;                                 break;                                 }            } check: printf("Loop runned %d times\n",a); if(r==1) printf("number is not prime"); else printf("number is prime"); getch(); }

C program to print square spiral number pattern - asr

Image
#include <stdio.h> #include <conio.h> void main(){ int i,j,a[10],n=3,big,k; clrscr(); printf("enter three numbers\n "); for(i=0;i<n;i++){ scanf("%d",&a[i]); }      printf("\n"); for(i=1;i<=5;i++)    {     for(j=1;j<=5;j++)       {        i>1&&i<5&&j>1&&j<5?i==3&&j==3?        printf("%d ",a[0]):        printf("%d ",a[1]):        printf("%d ",a[2]);       }    printf("\n"); } getch(); }   output: