Sunday, November 10, 2013

Printing numbers in the below given format

/*
Program to Print Numbers in the below format
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
*/


#include<stdio.h>
void main()
{
int lines=0,i, num=1,col=0,count=0;
while(lines<5)
{
count++;
col=0;
for(i=col;i<count;i++)
{
printf("%d \t",num);
num++; col++;
}
printf("\n");
lines++;
}
}

Friday, August 9, 2013

Sum of factorial of each digit of a number

/*
*Sum of factorial of each digit of a number
*/

#include<stdio.h>

int fact(int );
void main()
{
        int num,sum=0,rem;
        printf("Enter any Number\n");
        scanf("%d",&num);
        while(num!=0)
       {
             rem=num%10;
             sum=sum+fact(rem);
             num=num/10;
       }
       printf("Sum=%d\n",sum);
}
int fact(int num)
{
           if(num==1)
                     return 1;
           if(num==0)
                     return 1;
           else
                     return num*fact(num-1);
}

/*
*Enter any Number
*23
*Sum=8
*/

Sum of alternate digit of a number

/*
*Program to find sum of alternate digit of a number
*/

#include<stdio.h>
void main()
{
        int num,rem=0,sum=0,count=0;
        printf("Enter any number\n");
        scanf("%d",&num);
        while(num!=0)
        {
                rem=num%10;
                num=num/10;
                if(count%2==0)
                {
                          sum=sum+rem;
                }
                count++;
        }
        printf("Sum=%d\n",sum);
}

/*
Output

Enter any number
3456
Sum=10
*/

Tuesday, April 16, 2013

Program to print pyramid of numbers


#include<stdio.h>
void main()
{
        int i,n,j;
        printf("\n Enter Value of N:  ");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            for(j=0;j<n-i;j++)
            printf("  ");
            for(j=i;j>=0;j--)
            printf(" %d",j);
            for(j=1;j<=i;j++)
            printf(" %d",j);
            printf("\n");
        }
}
 
 
/* 
 
     Enter Value of N:  9


                       0
                     1 0 1
                   2 1 0 1 2
                 3 2 1 0 1 2 3
               4 3 2 1 0 1 2 3 4
             5 4 3 2 1 0 1 2 3 4 5
           6 5 4 3 2 1 0 1 2 3 4 5 6
         7 6 5 4 3 2 1 0 1 2 3 4 5 6 7
       8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8
 
 
*/ 

Monday, April 8, 2013

Number of Days between 2 dates

/*

Program to find the number of days between two dates
Link: http://stackoverflow.com/questions/12862226/the-implementation-of-calculating-the-number-of-days-between-2-dates

*/

#include <iostream>

using namespace std;

#define check_leap(year) ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
#define debug(n) cout << n << endl

int get_days(int month, bool leap){
    if (month == 2){
        if (leap) return 29;
        return 28;
    } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
        return 31;
    } else {
        return 30;
    }
}


int days[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

#define days_prior_to_month(n) days[n-2]
int num_days_between(int month1, int day1, int month2, int day2, bool leap){
    if (month2 > month1)
        return ((days_prior_to_month(month2) - days_prior_to_month(month1+1)) + get_days(month1, leap) - day1 + 1 + day2) + ((leap &&  month1 <= 2 && 2 <= month2) ? 1 : 0);
    else if (month2 == month1)
        return day2;
    return -1;
}

int main(int argc, char * argv[]){
    int year, month, day, year2, month2, day2;
    cout << "Year: "; cin >> year;
    cout << "Month: "; cin >> month;
    cout << "Day: "; cin >> day;
    cout << "Year 2: "; cin >> year2;
    cout << "Month 2: "; cin >> month2;
    cout << "Day 2: "; cin >> day2;

    int total = 0;
    if (year2 != year){
        int leapyears = 0;
        total += num_days_between(month, day, 12, 31, check_leap(year));
        debug(total);
        total += num_days_between(1, 1, month2, day2, check_leap(year2));
        debug(total);
        int originalyear = year;
        year++;
        year = year + year % 4;
        while (year <= year2-1){
            leapyears += check_leap(year) ? 1 : 0;
            year += 4;
        }

        total += leapyears * 366;
        debug(total);
        total += max(year2 - originalyear - leapyears - 1, 0) * 365;
        debug(total);

    } else {
        total = num_days_between(month, day, month2, day2, check_leap(year));
    }
        cout << "Total Number of Days In Between: " << total << endl;
    return 0;
}


/*
 In one of my interview i got this question. This program is been referenced from the above provided link.
*/

Friday, March 22, 2013

Java Program to split multiple email address into separate email address


/*
 * Java Program to split multiple email address into separate email address
 */
public class str
{
public static void main(String args[])
{
String to="syedmohammadali123@gmail.com;arjun.gadvi@gmail.com;";
System.out.println(to);
String to1[]=new String[1000];
String ch="";
String temp="";
int i=0,j=0;
{
temp="";
for(i=0; i<to.length();i++)
{
ch="";
to1[i]="";
ch=ch+to.charAt(i);
if(ch.equals(";"))
{
to1[j]=""+temp;
temp="";
j++;
continue;
}
temp=temp+ch;
}

}
System.out.println(to1[0]+"\n");
System.out.println(to1[1]+"\n");
}
}


OUTPUT:
syedmohammadali123@gmail.com;arjun.gadvi@gmail.com;
syedmohammadali123@gmail.com

arjun.gadvi@gmail.com

Thursday, February 14, 2013

Program to remove the characters present in Primary String from Secondary String and print the remaining.

/*
Program to remove the characters present in Primary String from Secondary String and print the remaining.
*/
#include<stdio.h>
void main()
{
    char prmstr[]={'a',' ','b','c','\n'};
    char secstr[]={'m','y',' ','n','a','m','e',' ','i','s',' ','a','b','c','\n'};
    char res[10];
    char ch,secch;
    int i,j,k;
    int flag=0;
    i=0;j=0;k=0;
    while(secstr[i]!='\n')
    {
        flag=0;
        ch=secstr[i];
        i++;
        j=0;
        while(prmstr[j]!='\n')
        {
            secch=prmstr[j];
            j++;
            if(ch==secch)
            {
                flag=1;
            }
        }
        if(flag==0)
        {
            res[k]=ch;
            k++;
        }
    }
    res[k]='\n';
    k=0;
    while(res[k]!='\n')
    {
        printf("%c",res[k]);
        k++;
    }
    printf("\n");
}

/*
OUTPUT

mynmeis

*/

Wednesday, February 13, 2013

Program To Rotate the Matrix

/*
Program to Rotate the Given Matrix:

*/
#include<stdio.h>
void main()
{
    int a[2][3]={0,1,0,1,1,1};
    int i,j;
    int i1,j1;
    printf("Matrix A:\n");
    for(i=0;i<2;i++)
    {
        for(j=0;j<3;j++)
            printf("%d\t",a[i][j]);
        printf("\n");
    }
    printf("Matrix After Rotation:\n");
    for(i=2;i>=0;i--)
    {
        for(j=0;j<2;j++)
            printf("%d\t",a[j][i]);
        printf("\n");
    }
}

/*
Suppose the Given Matrix be:

0    1    0   
1    1    1

Then the result should be:

0    1   
1    1   
0    1

//Simple Logic Take Care of loop
*/

Monday, February 11, 2013

Series of given range (Sum of previous two numbers)

/*
Program to read two numbers and a range and print the series (Sum of previous two numbers) of given range.

*/
#include<stdio.h>
void main()
{
    int range;
    int fs,sec,count=2,temp;
    printf("Enter the Fiirst and seconfd number:\n");
    scanf("%d%d",&fs,&sec);
    printf("Enter the Range:\n");
    scanf("%d",&range);
    if(fs<sec)
    {
        printf("%d\t%d\t",fs,sec);
        while(count<range)
        {
            temp=fs+sec;
            printf("%d\t",temp);
            fs=sec;
            sec=temp;           
            count++;
        }
        printf("\n");
    }
    else
    {
        printf("Invalid Input\n");
    }
}

/*
Say my Input is 2 and 3 for numbers and range is 5
the output should be as follow
2    3    5    8    13
*/

Sunday, February 10, 2013

Program to check the given number is power of two or not?

/*
Program to check the given number is power of two

*/


#include<stdio.h>
void main()
{
    int num,exor;
    printf("Enter a number:\n");
    scanf("%d",&num);
    exor=num & (num-1);
    if(exor==0)
        printf("Number is pow of 2\n");
    else
        printf("Number is not pow of 2\n");   
}


/*
Simple logic
Step 1:just read the number
Step 2:find Exclusive OR of number and (number-1) assign value to some variable say exor
Step 3: if exor's value is zero (0) thn we say number is the power of 2 otherwise not


If not understood convert the number and (number-1) to binary and follow from step 2
*/

Thursday, February 7, 2013

Covert binary number to its decimal equivalent

/*
Program to convert the binary number to its decimal equivalent
*/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

main()
{
    int binnum,decimal=0,rem,i=0;
    printf("Enter the binary number: ");
    scanf("%d",&binnum);
   
    while(binnum!=0)
    {
        rem=binnum%10;
        if(rem>1){
            printf("\nNot Binary number\n");
            exit(1);
        }
        if(rem==0)
            i++;
        else
        {
            decimal+=pow(2,i);
            i++;
        }       
        binnum/=10;
    }
    printf("Decimal Equivalent is : %d\n",decimal);
}

Sunday, February 3, 2013

Program to find difference of two integers without using - operator


/*
Program to find difference of two integers without using - operator
Programmer : Syed Mohammad Ali
*/
#include<stdio.h>
void main()
{
     int a,b,diff=0;
     printf("Enter two integers\n");
     scanf("%d%d",&a,&b);
     if(a!=b)
     {
while(a!=b)
       {
diff++;
            b++;
       }
    }
    else
   {

   }
   printf("Difference = %d\n",diff);
}

/*
The Basic idea here is we assume we don't have - operator in our language.
Increment the small value variable until its value becomes equal to other variable value. Simultaneously increment the counter variable. When both variables values are equal, counter will hold the difference of the two numbers.
In the above program diff is the counter variable.
*/

Program to find product of two integers without using * operator


/*
Program to find product of two integers without using * operator
Programmer : Syed Mohammad Ali
*/
#include<stdio.h>
void main()
{
int a,b,prod=0;
printf("Enter two integers\n");
scanf("%d%d",&a,&b);
while(a!=0)
{
prod=prod+b;
a--;
}
printf("Product= %d\n",prod);
}


/*
The basic idea is that we assume that we dont have * operator in the language.
In this case add value of one variable to prod variable second variable number of times. In the example we added b to prod a number of times.
*/

Sunday, January 27, 2013

Decimal to Binary Logic

/*
Program to to conver the decimal number to the binary equivalent
OS: UNIX or Windows
*/

#include<stdio.h>
void main()
{
int num,bin=0,rem,k=1;
printf("Enter the decimal number:\n");
scanf("%d",&num);
while(num!=0)
{
rem=num%2;
bin=bin+rem*k;
k=k*10;
num=num/2;
}
printf("Binary Equivqlent of a given number is %d\n",bin);
}

Perfect Number Logic

/*
Program to check the given number is perfect or not
OS: UNIX or Windows

*/

#include<stdio.h>
void main()
{
         int sum=0, num, i;
         printf("Enter the number");
         scanf("%d",&num);
         for(i=1;i<num;i++)
         {
                 if(num%i==0)
                          sum=sum+i;
         }
         if(sum==num)
                   printf("%d is perfect number\n",num);
         else
                   printf("%d is not a perfect number\n",num);
}

/*
 Refer these links to get the clear idea about Perfect numbers

http://mathworld.wolfram.com/PerfectNumber.html

Perfect number 'n' is a positive number where sum of divisors of n is equal to n.

Ex: number is 6, its divisors are 1,2,3 there fore sum of 1+2+3=6

*/