Saturday, March 31, 2012

Wap to Print the Reverse of a Number

No comments

*Print the Reverse of a Number*/

 #include<stdio.h> 
#include<conio.h>
void main() 
 {
        long num,i,sum=0,as;
         clrscr();
         printf("\n Program for Print the Reverse of a Number");      
         printf("\n Enter A Number ::");
         scanf("%ld",&num);

         printf("\n Reverse number ::"); 
         while(num>0) 
         { 
             as=num%10;
             sum=sum+as;
             num=num/10;
            printf("%ld",as); 
        } 
    printf("\n Want More Need Example for Practice ");
    printf("\n Logon www.Code2Create.com"); getch();
   }


Wap to Calculate Profit & Loss

No comments

/*Profit & Loss */
#include<stdio.h>
#include<conio.h>
     void main()
       {
 float selling_prize,buying_prize;
 clrscr();

 printf("\n Calculate Profit & Loss");

 printf("\n Enter Purchasing Prize  for 15 Items::");
 scanf("%f",&buying_prize);


   printf("\n Enter Selling Amount for 15 Items::");
 scanf("%f",&selling_prize);

 if(buying_prize<selling_prize)
 printf("\n\t Profit :$ %f",selling_prize - buying_prize);
 else
 printf("\n\t Loss  :$%f",buying_prize - selling_prize);

 printf("\n Want More Need Example for Practice ");
 printf("\n     Logon www.Code2Create.com");

 getch();
}


Sum of First and Last Digits of a Number

No comments

 /*Sum of First and Last Digit of a Number*/


#include<stdio.h>
#include<conio.h>
     void main()
       {
 int num,sum=0,a,b;

 printf("\n Sum of First and Last Digit of a four Digits Number");

 printf("\n Enter A Number    ::");
 scanf("%d",&num);


 a=num%10;
 b=num/1000; 
 sum=a+b;

 printf("\n First Number is  :: %d",b);
 printf("\n Last Number is   :: %d",a);
 printf("\n Sum : %d",sum);

 printf("\n Want More Need Example for Practice ");
 printf("\n     Logon www.Code2Create.com");

 getch();
}

If you want to add first and last digit number then  
b=num/n 
  • For 2 digits Number  (n=10)
  • For 3 digits Number  (n=100)
  • For 4 digits  Number (n=1000)


Monday, March 26, 2012

Wap to print 1 12 123

No comments

/*It is complied on Turbo C++ 3.0*/  

#include<stdio.h>
#include<conio.h>
    void main()
       {
 int i,result=0;
 clrscr();
 for(i=1;i<5;i++)
 {

    result=result*10+i;
    printf("\n%d",result);

  }
   getch();
   }
OutPut

Saturday, March 24, 2012

WAP to shutdown your computer in C

1 comment

/*It is complied on Turbo C++ 3.0 Or Use CodeBlock Complier*/  

#include<stdio.h>
#include<process.h>
    main( )
         {
              system("shutdown -s");
          }    


Friday, March 16, 2012

Write A Program Without Using Header File in C.

No comments

/*It is complied on Turbo C++ 3.0*/  

We can write a program without using header-file in C language. First of All I will give a simple example to understand this concept. See the example:
        #include<stdio.h>
        #include<conio.h>
              void main ( )
                    {
                          int num1,num2,sum; /*This is integer type variable  declaration*/

                          clrscr ( ) ;
                          printf("\n Enter Two Numbers :: ");
                          scanf("%d%d",&num1,&num2);
                         printf("\n The Sum is :: %d",(num1+num2));
                         getch();
                   }
                  
We all know that It is the simplest program for addition of two number. It will simple return the sum  of two number. If  We enter two number number 10 and 12. It will return 22.
But If we remove the all header files then the remaining program will be.. 
              void main ( )
                    {
                          int num1,num2,sum; /*This is integer type variable  declaration*/
                          clrscr ( ) ;
                          printf("\n Enter Two Numbers :: ");
                          scanf("%d%d",&num1,&num2);
                         printf("\n The Sum is :: %d",(num1+num2));
                         getch();
                   }
Does it work like before ?
Answer is Yes . It will work and return right answer like before.

Why is It ?
When we write any program in C without using header files and compile the program. It (before save the program) will show many error but when we save this program with .C extension. By default compiler include all necessary header file and program will successfully run.
But Some times It may show error for some header files like math.h .

Simple step to write program without using Header files.


  1. Start your program form void main( )
  2. Complete your program.
  3.  Don't compile it now just save it with .C extension then compile.
  4. Finally Run Your Program and Get result.
It is trick. Not a concept, So for better result use header file when creating a bigger program.
I check it in Turbo C++ 3.0
Try it in Different compiler and comment here so that other can know...




Saturday, March 10, 2012

Wap to print Current date in C

No comments

/*It is complied on Turbo C++ 3.0*/  

#include <stdio.h>
#include <dos.h>
#include<conio.h>
void main()
{
   struct date d;
   getdate(&d);
   clrscr();
   textcolor(2);
   cprintf("Date in DD-MM-YEAR");
   textcolor(6);
   cprintf("\n\rThe current Date is: %d-%d-%d", d.da_day,d.da_mon,d.da_year);
   getch();
}

Saturday, March 3, 2012

Calendar Project in C/C++

No comments

/*It is complied on Turbo C++ 3.0*/

/*Calendar Project  in C  */
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>
int getNumberOfDays(int month,int year)
{
   switch(month)
   {

      case 1 : return(31);
      case 2 : if(year%4==0)
return(29);
      else
return(28);
      case 3 : return(31);

      case 4 : return(30);
      case 5 : return(31);
      case 6 : return(30);
      case 7 : return(31);
      case 8 : return(31);
      case 9 : return(30);
      case 10: return(31);
      case 11: return(30);
      case 12: return(31);
      default: return(-1);
   }
}
char *getName(int odd)
{
   switch(odd)
   {
      case 0 :return("Sunday");
      case 1 :return("Monday");
      case 2 :return("Tuesday");
      case 3 :return("Wednesday");
      case 4 :return("Thursday");
      case 5 :return("Friday");
      case 6 :return("Saturday");
      default:return("Error in getName() module.Invalid argumenpassed");
   }
}
int getOddNumber(int day,int mon,int year)
{
   int res=0,t1,t2,y=year;
   year = year-1600;
   while(year>=100)
   {
       res=res+5;
       year=year-100;
   }
   res=(res%7);
   t1=((year-1)/4);
   t2=(year-1)-t1;
   t1=(t1*2)+t2;
   t1=(t1%7);
   res = res+t1;
   res=res%7;
   t2=0;
   for(t1=1;t1<mon;t1++)
   {
      t2+=getNumberOfDays(t1,y);
   }
   t2 = t2+day;
   t2 = t2%7;
   res = res+t2;
   res = res%7;
   if(y>2000)
     res=res+1;
   res = res%7;
   return res;
}
char *getWeek(int dd,int mm,int yy)
{
   int odd;
   if(!(mm>=1 && mm<=12))
   {
      return("Invalid month value");
   }
   if(!(dd>=1 && dd<=getNumberOfDays(mm,yy)))
   {
      return("Invalid date");
   }
   if(yy>=1600)
   {
     odd = getOddNumber(dd,mm,yy);
     odd=odd%7;
     return(getName(odd));
   }
   else
   {
      return("Please give year more than 1600");
   }
}
void printMonth(int mon,int year,int x,int y)
{
   int nod,odd,cnt,d=1,x1=x,y1=y;
   if(!(mon>=1 && mon<=12))
   {
       printf("INVALID MONTH");
       getch();
       return;
   }
   if(!(year>=1600))
   {
      printf("INVALID YEAR");
      getch();
      return;
   }
   if(x<=0)
     x=wherex();
   if(y<=0)
     y=wherey();
   gotoxy(x,y);
   textcolor(RED);
   cprintf("S");
   textcolor(YELLOW);
   cprintf("   M   T   W   T   F   S");
   /*       1234567891234567891234567 */
   textcolor(7);
   cprintf("");
   y++;
   nod=getNumberOfDays(mon,year);
   odd=getOddNumber(d,mon,year);
   switch(odd)
   {
     case 0 : x=x;
     cnt=1;
     break;
     case 1 : x=x+4;
     cnt=2;
     break;
     case 2 : x=x+8;
     cnt=3;
     break;
     case 3 : x=x+12;
     cnt=4;
     break;
     case 4 : x=x+16;
     cnt=5;
     break;
     case 5 : x=x+20;
     cnt=6;
     break;
     case 6 : x=x+24;
     cnt=7;
     break;
     default : printf("INVALID DATA FROM THE getOddNumber()MODULE");
      return;
   }
   gotoxy(25,25);
   gotoxy(x,y);
   printf("%02d",d);
   for(d=2;d<=nod;d++)
   {
      if(cnt%7==0)
      {
y++;
cnt=0;
x=x1-4;
      }
      x = x+4;
      cnt++;
      gotoxy(x,y);
      printf("%02d",d);
   }
}
void main()
{
   char ch='k';
   int dd,mm,yy;
   while(ch!='0')
   {
      clrscr();
      textcolor(2);
      cprintf("\r\n1.Know the day");
      textcolor(3);
      cprintf("\r\n2.Print the month");
      textcolor(4);
      cprintf("\r\n0.EXIT");
      textcolor(5);
      cprintf("\r\nENTER YOUR CHOICE : ");
      flushall();
      fflush(stdin);
      ch=getche();
      clrscr();
      switch(ch)
      {
case '1': printf("Enter date (DD MM YYYY) : ");
 scanf("%d %d %d",&dd,&mm,&yy);
 printf("Day is : %s",getWeek(dd,mm,yy));
 flushall();
 getch();
 break;
case '2' : printf("Enter month and year (MM YYYY) : ");
  scanf("%d %d",&mm,&yy);
  printf("");
  printMonth(mm,yy,-1,-1);
  flushall();
  getch();
  break;
case '0' : exit(0);
      }
   }
}

Program to accept a year and check whether the given year IS leap year or not.

No comments
/*It is complied on Turbo C++ 3.0*/
# include <stdio.h>
# include <conio.h>
        void main( )
             {
                 int year;

                 clrscr( );
                 printf(“Enter a year:”);
                 scanf(“%d”,&year);
                 if(year%4==0& &year%100!=0|| year%400==0);
                 printf(“The above given year IS a leap year”);
                 else
                 printf(“The above given year IS not a leap year”);
                 getch();
            }

Program To Read Two Numbers And Print The Sum Of Given Two Numbers.

No comments

/*It is complied on Turbo C++ 3.0*/

#include<stdio.h>
#include<conio.h>
     void main()
          {
               float num1,num2,sum=0;
               clrscr();

               printf("\n Enter Two Number :: ");
               scanf("%f%f",&num1,&num2);
               sum=num1+num2;
               printf("\n Num1 + Num2 = %f",sum);
               getch();
          }

Program to print text

No comments

/*It is complied on Turbo C++ 3.0*/

#include<stdio.h>
#include<conio.h>
      void main()
          {

              clrscr();
              printf("\n www.LovelyCoding.blogspot.com");
              getch();
           }
Entries RSS Comments RSS

Sample Text

Pages


Copyright © Lovely Codes
Powered by Blogger
Distributed By Free Blogger Templates | Design by N.Design Studio
Blogger Theme by Lasantha - PremiumBloggerTemplates.com