Posts

Showing posts from June, 2018

C program to check the number of occurences of each characters in a string

Output: welcome                                                                                                                                         c->1                                                                                                                                            e->2                                                                                                                                            l->1                                                                                                                                            m->1                                                                                                                                            o->1                                                                                                                                            w->1

thetestofmylife����������������������������������������������������������������������������������������…

C program to write data into a file

Program: #include<stdio.h> #include<stdlib.h> int main() { char name[100]; FILE *fp; fp=fopen("one.txt","a"); printf("Enter the name \n"); gets(name); fprintf(fp,"%s",name); return 0; }

C

Headings
1.Patterns

Reverse without using temporary variables(Using XOR)

Program: #include<stdio.h> int main() { int a=5,b=10; b ^= a; a ^= b; b ^=a; printf("%d %d",a,b); return 0; }

Output:b a                                  
10 5

GCD of n numbers

Program: #include <stdio.h> int gcd(int a,int b) {     if (a == 0)         return b;     return gcd(b % a, a); } int findgcd(int a[],int n) {     int result=a[0];     for(int i=0;i<n;i++)     {         result=gcd(a[i],result);     }     return result; } int main() {     int test;     scanf("%d",&test);     for(int i=0;i<test;i++)     {         int size;         scanf("%d",&size);         int a[size];         for(int j=0;j<size;j++)         {             scanf("%d",&a[j]);         }         printf("%d \n",findgcd(a,size));     }     return 0; }
Input:55 2 4 8 16 324 3 6 9 123 5 10 152 50 1001 302355030

Pattern printing

Output:Obtain a output as follows: 
N=5 

2*3 
4*5*6 
7*8*9*10 
11*12*13*14*15 
11*12*13*14*15 
7*8*9*10 
4*5*6 
2*3 
1
Program: #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() {     int n;     int num=1;     scanf("%d",&n);     for(int i=0;i<n;i++)     {         for(int j=0;j<=i;j++)         {             printf("%d",num);             if(j>=0&&j<i)             {                 printf("*");             }             num++;         }         printf("\n");     }     int j;     for(int i=n-1;i>=0;i--)     {         int val=num-i-1;         for(j=0;j<=i;j++)         {             printf("%d",val);             if(j<i)             {                 printf("*");             }             val++;         }         num=val-j;         printf("\n");     }     return 0; }

C program to print triangular pyramid

Output: Enter number of rows: 5                                                                                                                                 *                                                                                                                                             * * *                                                                                                                                         * * * * *                                                                                                                                     * * * * * * *                                                                                                                                 * * * * * * * * *                                                                                                                               Program:#include<stdio.h>int main(){int i, space, rows, k=0; printf("Enter number of rows: "); scanf(…

c++ program to print triangle pattern

Code: #include <iostream>
using namespace std;
int main() {     int n;     cin>>n;     int i,j;     int k=1;     for(i=n-1;i>=0;i--)     {         int num=k;         for(j=0;j<=i;j++)         {             cout<<num<<" ";             num=num+n-j;         }         k++;         cout<<"\n";     } }
Output: 1 8 14 19 23 26 28                                                                                                                              2 9 15 20 24 27                                                                                                                                 3 10 16 21 25                                                                                                                                   4 11 17 22                                                                                                                                      5 12 18�����������������������������������������������������…

C program to find the sum of unique elements in an array

Logic: 1.sort the elements 2.Then read the elements one by one                   i) If the current element and the next element are not the same add the current element to the sum and then finally print the sum at the end.
Program: #include<iostream> using namespace std; int main() {     int n;     cin>>n;     int i;     int a[n];     for(i=0;i<n;i++)     {         cin>>a[i];     }     //sort the std::array<T, N> ;     int j;     for(i=0;i<n;i++)     {         for(j=0;j<n;j++)         {             if(a[i]<a[j])             {                 int temp=a[i];                 a[i]=a[j];                 a[j]=temp;             }         }     }     int sum=0;     for(i=0;i<n;i++)     {         if(a[i]!=a[i+1])         {             sum=sum+a[i];         }     }     cout<<sum; }
Output: 5                                                                                                                                               2 2 3 3 5���������…

C++ program to find the substring in a given string

Program: #include<iostream> #include<string.h> using namespace std; int main() {     char str1[100],test[100];     cout<<"Enter the string :\n";     cin>>str1;     cout<<"Enter the substring: \n";     cin>>test;     int l=strlen(test);     int i,j;     for(i=0;str1[i]!='\0'&&test[j]!=0;i++)     {         if(str1[i]==test[j])         {             j++;         }         else         {             j=0;         }     }     if(j==l)     {         cout<<"substring is found at position "<<i-j+1;     }     else     {         cout<<"substring is not found ";     } }
output: Enter the string :                                                                                                                              imaginedragons                                                                                                                                  Enter the substring:�������…

C program to find LCM of two numbers

LOGIC: Find the largest number that is divided by both the numbers.
Program:
#include <stdio.h> int main () {   int n1, n2, max = 0;   scanf ("%d %d", &n1, &n2);   max = (n1 > n2) ? n1 : n2;   while (1)     {       if (max % n1 == 0 && max % n2 == 0) {   printf ("%d", max);   break; }       max++;     }   return 0; }
Output: 6 7                                                                                                                                             42                                                                                                                                             
6 3                                                                                                                                               6

C++ program for gcd of two numbers

Logic: We can solve this by recursion .
1.The base condition is that if both numbers are same let,s say x and y are equal we return either x or y.
2.else                * if x is greater than y we subtract y from x and then again call the function like this                                gcd(x-y,y)                *else if y is greater than x we subtract x from y and then again call the function like this                               gcd(x,y-x)
program: #include<iostream> using namespace std; int gcd(int n1,int n2) {     if(n1==n2)     {         return n1;     }     if(n1>n2)     {         return gcd(n1-n2,n2);     }     return (n1,n2-n1); } int main() {     int a,b;     cin>>a>>b;     cout<<gcd(a,b); }

output: 60 45                                                                                                                                           15 
100 1050����������������������������������������������������������������������������������������������…
Mom : call your friends

Me : okay hold on





......




yourFriends();

c program to compare two strings and remove the common letters

Program: #include <stdio.h> int main() {     char str1[100],str2[100];     scanf("%s %s",str1,str2);     int i,j;     for(i=0;i<strlen(str2);i++)     {         for(j=0;j<strlen(str1);j++)         {             if(str2[i]==str1[j])             {                 str1[j]='*';             }         }     }     for(i=0;i<strlen(str1);i++)     {         if(str1[i]!='*')         {             printf("%c",str1[i]);         }     } return 0;     }
output:
improvisation                                                                                                                                   ipm                                                                                                                                             rovsaton                                                                                                                                       

experience������������������������������������������������������…

Reverse a string using recursion

Program: #include <stdio.h> int reverse(char arr[],int index,int size) {     char temp=arr[index];     arr[index]=arr[size-index];     arr[size-index]=temp;     if(index==size/2)     {         return arr;     }     else     {         reverse(arr,index+1,size);     } } int main() {     char str1[100];     printf("Enter the string to reverse :\n");     scanf("%s",str1);     int size=strlen(str1);     reverse(str1,0,size-1);     printf("String after reversing : %s",str1);     return 0; }

output: Enter the string to reverse :                                                                                                                 programming                                                                                                                                   String after reversing : gnimmargorp
Image

Advocate and switch statement

The advocate was jealous of switch statement .Do you know why?

It had many cases.

C and c++ proposes to a girl

I am c, He is c++, We both proposed a girl, She rejected me and chose him, I asked her why?


She said.....



He has class .

Print without semicolon

Logic: In c statements are printed using a printf statement terminated by a semicolon.If we provide this printf statement inside an if,while or any other loops the statements will be printed without the requirement of a semicolon. Since the loop will be executed by default
program:
#include <stdio.h> int main() {     if(printf("hello world!"))     return 0; }
output: hello world!

c program to delete reapeating words in a string

program: #include<stdio.h> #include<string.h> int main() {     char str[100],b[20][20];     int j=0,k=0,i;     scanf("%[^\n]s",str);     for(int i=0;str[i]!='\0';i++)     {         if(str[i]!=' ')         {             b[k][j]=str[i];             j++;         }         else if(str[i]=' ')         {             b[k][j] = '\0';             k++;             j=0;         }     }     b[k][j]='\0';     for(i=0;i<=k;i++)     {         for(j=i+1;j<=k;j++)         {             if(strcmp(b[i],b[j])==0)             {                 for(int m=j;m<=k;m++)                 strcpy(b[m],b[m+1]);                 k--;             }         }     }     for(i=0;i<=k;i++)     {         printf("%s ",b[i]);     }     return 0; }

output:
hey penne penne , unnai kanda pinbe neram nalla neram endru thondruthe!!                                                                        hey penne , unnai kanda pinbe neram nalla end…

C program to print string by eliminating extra white spaces

#include <stdio.h>
int main()
{
   char text[100], blank[100];
   int c = 0, d = 0;

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

   while (text[c] != '\0')
   {
      if (!(text[c] == ' ' && text[c+1] == ' ')) {
        blank[d] = text[c];
        d++;
      }
      c++;
   }

   blank[d] = '\0';

   printf("Text after removing blanks\n%s\n", blank);

   return 0;
}


Output:

Enter some text                                                                                                                                 My                     name is            bond ,    James     bond                                                                  Text after removing blanks                                                                                                                      My name is bond , James bond

Structures

Structure is a datatype.
Structure is the collection of data.
The struct keyword is used to define a structure.

Example for structure declaration struct student {           char name[100];           int age;           float average; }
structure variables are declared next; Then using dot operator the members of the structure variables can be accessed.
Program #include <iostream> using namespace std; struct student {     char name[10];     int age; }; int main() {     student s1;     cin.get(s1.name,50);     cin>>s1.age;     cout<<"name:"<<s1.name<<endl;     cout<<"age:"<<s1.age;     return 0; }
Output John Smith                                                                                                                                      21                                                                                                                                              name:John Smith������������������������…