Technocrat

  • Home
Showing posts with label Tricky. Show all posts
Showing posts with label Tricky. Show all posts

Wednesday, September 23, 2015

Find the maximum length of the subarray in increasing order.

 Sumit Kar     September 23, 2015     Array, Tricky     No comments   

For example we have an array {5,4,6,-2,-1,0,1,9}. The biggest sub-array in increasing order is: {-2,-1,0,1,9}. So the length is 5.

#include<stdio.h>
void main () {
int arr[8] = {5,4,6,-2,-1,0,1,9},i,arrn[8],count=1,j=0,max;
printf("The given array is: ");
for(i=0;i<8;i++)
printf(" %d" ,arr[i]); // printing the actual array - unnecessary step.
printf("\n");
for(i=0;i<7;i++){
if(arr[i]<arr[i+1])
{
count++;
}
else
{
arrn[j]=count; // adding the length of sub array
count=1;
j++;
}
}
arrn[j]=count; // adding the final length
max=arrn[0];
printf("Length of sub arrays in increasing order");
for(i=0;i<=j;i++){
if(max<arrn[i])
max=arrn[i];
printf(" %d",arrn[i]); // printing the new array - unnecessary step.
}
printf("\n Maximum Length of the substring: %d",max); // print max value
}

   
Output
The given array is: 5 4 6 -2 -1 0 1 9
Length of sub arrays in increasing order 1 2 5
Maximum Length of the substring: 5
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Spiral Matrix

 Sumit Kar     September 23, 2015     If Else, Matrix, Tricky, User Defined Function, While Loop     No comments   

#include <stdio.h>
#define R 3
#define C 3

void spiralPrint(int m, int n, int a[R][C])
{
int i, k = 0, l = 0;

/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/

while (k < m && l < n)
{
/* Print the first row from the remaining rows */
for (i = l; i < n; ++i)
{
printf("%d ", a[k][i]);
}
k++;

/* Print the last column from the remaining columns */
for (i = k; i < m; ++i)
{
printf("%d ", a[i][n-1]);
}
n--;

/* Print the last row from the remaining rows */
if ( k < m)
{
for (i = n-1; i >= l; --i)
{
printf("%d ", a[m-1][i]);
}
m--;
}

/* Print the first column from the remaining columns */
if (l < n)
{
for (i = m-1; i >= k; --i)
{
printf("%d ", a[i][l]);
}
l++;
}
}
}

/* Program to test above functions */
int main()
{
int a[R][C] = { {1, 2, 3},
{8, 9, 4},
{7, 6, 5}
};

spiralPrint(R, C, a);
return 0;
}

   
Output
1 2 3 4 5 6 7 8 9
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Find the maximum occurrence of a character in a string

 Sumit Kar     September 23, 2015     Array, For Loop, If Else, Tricky     No comments   

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

int main() {
char str[20], ch;
int count = 0, i,j=0 , max;
int arr[10];
printf("\nEnter a string : ");
scanf("%s", &str);

printf("\nEnter the character to be searched : ");
scanf("%c", &ch);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ch)
count++;
else if(count != 0)
{
arr[j] = count;
j++;
count = 0;
}
}
arr[j]=count;
max = 0;
for(i=0;i<=j;i++)
if(max<arr[i])
max=arr[i];
if (max == 0)
printf("\nCharacter '%c'is not present", ch);
else
{
printf("\nMaximum occurrence of character '%c' : %d", ch, max);
}
return (0);
}

   

Output

Enter a string : aabbbaaab

Enter the character to be searched :
Maximum occurrence of character 'a' : 3

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Monday, July 27, 2015

Clear Screen without clrscr() function call

 Sumit Kar     July 27, 2015     Tricky, User Defined Function     No comments   

#include<stdio.h>
void clearmyscreen();
void main()
{
printf("Hello");
getch();
clearmyscreen();
printf("World");
getch();
}

void clearmyscreen()
{
system("cls");
}

   

Output
World

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Thursday, May 21, 2015

Find Greatest number using Conditional Operator

 Sumit Kar     May 21, 2015     Basic, Conditional Operator, Tricky     No comments   

#include<stdio.h>

void main()
{
int n1,n2,n3;
printf("Enter Numbers (n1 n2 n3) : ");
scanf("%d %d %d",&n1,&n2,&n3);
printf("Greatest= %d",n1>n2?n1>n3?n1:n3:n2>n3?n2:n3);
}

  

Output
Enter Numbers (n1 n2 n3) : 4 9 2
Greatest= 9
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Print "Hello, World!" without semicolon

 Sumit Kar     May 21, 2015     Basic, Tricky     No comments   

Solution: 1
#include<stdio.h>
void main(){
if(printf("Hello world")){
}
}
Solution: 2
#include<stdio.h>
void main(){
while(!printf("Hello world")){
}
}

Solution: 3
#include<stdio.h>
void main(){
switch(printf("Hello world")){
}
}

  


Output
Hello, World!
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp
Older Posts Home

Pages

  • Home

Trending Now

  • Write a Program to Add two 3x3 Matrix using C
    #include<stdio.h> void main () { int a [ 3 ][ 3 ], b [ 3 ][ 3 ], s [ 3 ][ 3 ], i , j ; printf ( "Enter the values of ...
  • C program for Unit Conversion
    /* Convert Celsius to Fahrenheit */ #include<stdio.h> void main() {     float c,f;     printf("Enter the temperature in Celcius: ...
  • Addition of two numbers on Server sent from Client [TCP] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • Write a Program to Print the Truth Table of Basic Gates using C
  • Write a Program to Add two 5x5 Matrix using C
    #include<stdio.h> void main() {   int a[5][5],b[5][5],c[5][5];   int i,j;   for(i=0;i<5;i++)   {     printf("\nEnter elements ...
  • Using the concept of Inheritance write a C++ Program to calculate the area and perimeter of rectangle
    /* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */ #include using namespace std; class Re...
  • Concatenation of two strings sent from Client on the Server - [ TCP ] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • 8085 Programming: Exchange the contents of memory locations
    Exchange the contents of memory locations 2000H and 4000H. Program 1: LDA 2000H : Get the contents of memory location 2000H into accumulator...
  • Calculate Depreciation using C
    #include<conio.h> #include<stdio.h> void main () { float sv , pv , dep ; int yos ; clrscr (); printf ( "Enter the pu...
  • 8085 Programming: 1's COMPLEMENT OF A 16-BIT NUMBER
    The 16bit number is stored in C050,C051 The answer is stored in C052,C053   LXI H,C050   MOV A,M   CMA   STA C052   INX H   MOV ...

Blog Archive

  • ▼  2020 (1)
    • ▼  May (1)
      • Automating your deployments using Ansible
  • ►  2015 (92)
    • ►  October (4)
    • ►  September (3)
    • ►  August (3)
    • ►  July (9)
    • ►  June (9)
    • ►  May (20)
    • ►  April (7)
    • ►  March (22)
    • ►  February (7)
    • ►  January (8)
  • ►  2014 (158)
    • ►  November (70)
    • ►  October (6)
    • ►  September (82)
Powered by Blogger.

Categories

C - Programming Java Programming Basic Technology 8085 Assembly Programming For Loop Numerical Methods WhatsApp Algorithm Shell Programming Programming Networking Windows Android C++ Programming CPP If Else Tricky Internet Microsoft Pattern Photography Socket Program News While Loop Array DBMS DS Macro Recursion User Defined Function Conditional Operator Data Structure Durga Puja Earthquake Google Mela Nokia SQL Share Yahoo Airtel Bio Command Prompt Confused Facebook Finance Firefox Ganges Graph HokKolorob Input Kolkata MCQ Math Matrix NetNeutrality Oracle Religion Search Sumit Kar Survey Switch Case Viral Virus Visual Studio do-while featured featured_slider

Popular Programs

  • Write a Program to Add two 3x3 Matrix using C
    #include<stdio.h> void main () { int a [ 3 ][ 3 ], b [ 3 ][ 3 ], s [ 3 ][ 3 ], i , j ; printf ( "Enter the values of ...
  • C program for Unit Conversion
    /* Convert Celsius to Fahrenheit */ #include<stdio.h> void main() {     float c,f;     printf("Enter the temperature in Celcius: ...
  • Addition of two numbers on Server sent from Client [TCP] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • Write a Program to Print the Truth Table of Basic Gates using C
  • Write a Program to Add two 5x5 Matrix using C
    #include<stdio.h> void main() {   int a[5][5],b[5][5],c[5][5];   int i,j;   for(i=0;i<5;i++)   {     printf("\nEnter elements ...
  • Using the concept of Inheritance write a C++ Program to calculate the area and perimeter of rectangle
    /* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */ #include using namespace std; class Re...
  • Concatenation of two strings sent from Client on the Server - [ TCP ] using C
    /* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #inc...
  • 8085 Programming: Exchange the contents of memory locations
    Exchange the contents of memory locations 2000H and 4000H. Program 1: LDA 2000H : Get the contents of memory location 2000H into accumulator...
  • Calculate Depreciation using C
    #include<conio.h> #include<stdio.h> void main () { float sv , pv , dep ; int yos ; clrscr (); printf ( "Enter the pu...
  • 8085 Programming: 1's COMPLEMENT OF A 16-BIT NUMBER
    The 16bit number is stored in C050,C051 The answer is stored in C052,C053   LXI H,C050   MOV A,M   CMA   STA C052   INX H   MOV ...

Daily Hits

Copyright © Sumit Kar