9 Mar 2013

pattern_nested loops

/*

program in c to print following pattern...

&  &  &  &  &  &  &
   &  &  &  &  &
      &  &  &
         &
*/
 
#include<stdio.h>
#include<conio.h>

void main()
{
int a,t,i,j,k,b=0;
clrscr();
printf("\nEnter length:");
scanf("%d",&a);
b=a;
for(i=0;i<a;i++)
{
 t=i;
 for(j=1;j<=b;j++)
   {
    for(k=1;k<=t;k++)
    {
    printf("   ");
    }
   t=0;
   printf("&  ");
   }
 printf("\n");
 b=b-2;
}



getch();
}

Categories: ,

ugly_number

/*
program to generate nth ugly number



Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
shows the first 11 ugly numbers. By convention, 1 is included.

*/

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

/*This function divides a by greatest divisible
  power of b*/
int maxDivide(int a, int b)
{
  while (a%b == 0)
   a = a/b;
  return a;
}

/* Function to check if a number is ugly or not */
int isUgly(int no)
{
  no = maxDivide(no, 2);
  no = maxDivide(no, 3);
  no = maxDivide(no, 5);

  return (no == 1)? 1 : 0;
}

/* Function to get the nth ugly number*/
int getNthUglyNo(int n)
{
  int i = 1;
  int count = 1;   /* ugly number count */

  /*Check for all integers untill ugly count
    becomes n*/
  while (n > count)
  {
    i++;
    if (isUgly(i))
      count++;
  }
  return i;
}

/* Driver program to test above functions */
int main()
{   int n;
    unsigned no;
printf("\nEnter the value for n :");
scanf("%d",&n);

    no=getNthUglyNo(n);
    printf("%dth ugly no. is %d ",n,no);
    getchar();
    return 0;
}

Categories: ,

perfect_number








/*
 program to check whether a given no. is a ""perfect no."" or not.


a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself


The first perfect number is 6

*/




#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,s=0;
cout<<"Enter the number:";
cin>>a;
cout<<"The factors of "<<a<<" are as follows:"<<endl;
for(b=1;b<a;b++)
{
a%b;
if(a%b==0)
{cout<<b<<endl;
s+=b;
}
}
if(s==a)
cout<<a<<" is  a perfect number.";
else
cout<<a<<" is not a perfect number.";
getch();
}

Categories: ,

pattern_rectangle

/*
program to print following pattern

***********************
*                     *
*                     *
*                     *
*                     *
***********************

*/




#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,x,y,z;
cout<<"Enter rows:";
cin>>i;
cout<<"Enter cloumns:";
cin>>j;
for(x=1;x<=i;x++)
{
if((x==1)||(x==i))
{
for(y=1;y<=j;y++)
{
cout<<"*";
}
}
else
{
for(z=1;z<=j;z++)
{
if((z==1)||(z==j))
{
cout<<"*";
}
else
{
cout<<" ";
}
}
}
cout<<'\n';
}
getch();
}

Categories: ,

Pattern_nested LOOP

/*

program to generate following pattern......


    *
   ***
  *****
 *******
*********



 */




#include<iostream.h>
#include<conio.h>
 

void main()
{
clrscr();
 

int i,j,r,a=1;
 

for(i=1;i<=5;i++)
 {
  for(j=5;j>i;j--)
   {
    cout<<" ";
   }
  for(r=1;r<=a;r++)
   {
    cout<<"*";
   }
  cout<<'\n';
  a+=2;
 }
 

getch();
}

Categories: ,

8 Mar 2013

Palindrome_String



/*

program to check whether a string is palindrome or not in c++

*/

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
int i,j,k,flag=0;
char a[80];
cout<<"Enter the string:";
gets(a);
k=strlen(a);
for(i=0,j=k-1;i<=j;i++,j--)
{
if(a[i]!=a[j])
{
flag=1;
break;
}
}
if(flag>0)
cout<<"String is not palindrome.";
else
cout<<"String is palindrome.";
getch();
}

Categories: ,

graplin level-1 solved


/*

solution to graplin inline challenge level-1
*/




#include<stdio.h>
#include<conio.h>
#include<string.h>
void cutstr(char *,int ,int );
char arr1[150]={'\0'};char arr2[150]={'\0'};
char arr11[]="FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetract";
char arr12[]="TgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth";

void main()
{ char *s;
strcpy(s,arr11);
strcat(s,arr12);
printf("\n%s",s);
for(int i=0;i<strlen(s);i++)
{ for(int j=i+1;j<=strlen(s);j++)
    {if(s[i]==s[j])
        {cutstr(s,i,j);
        }

    }

}


getch();
}

void cutstr(char *s,int i,int j)
{static flag=0;
int b=i;
for(int k=0;i<=j;i++,k++)
    {arr1[k]=s[i];
    }
arr1[k]='\0';

strcpy(arr2,arr1);
strrev(arr1);

if( strcmp(arr1,arr2)==0 && flag<j-b+1)
{printf("OUTPUT : %s",arr2);
flag=j-b+1;}
}

Categories: ,

grapline level-2 solved


/*
 solution to grapline online change level-2

*\



#include<stdio.h>
#include<conio.h>
int checkprime(int );
void main()
{long int n=514230,i,sum=0;
for(i=2;i<n-1;i++)
    {if(checkprime(i)!=0 && n%i==0)
        {printf("\n%ld",i);
            {sum=sum+i;}
        }
    }
printf("OUTPUT:%ld",sum);
getch();
}

int checkprime(int i)
{for(int j=2;j<i;j++)
    {if(i%j==0)
        {return 0;}
    }
if(i==j)
return 1;
}

Categories: ,

Fibonacci series



/*
      program to generate Fibonacci series in C++.
 */

 





#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
unsigned long first,second,third,n;
first=0; second=1;
cout<<"How many elements(>5)?\n";
cin>>n;
cout<<"Fibonacci series\n";
cout<<first<<" "<<second;
for(int i=2;i<n;++i)
 {
  third=first+second;
  cout<<" "<<third;
  first=second;
  second=third;

 }
getch();
return(0);
}

Categories: ,

Copyright © UPgradeCODING | Powered by Blogger