Showing posts with label While Loop. Show all posts
Showing posts with label While Loop. Show all posts

Friday, September 23, 2011

WAP to add digits of entered number


/*
  Name: WAP to add digits of entered number
  Author: Parveen Malik
  Date: 23/09/11 09:41
*/


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


int main()
{
    long num;
    int sum=0,rem;
    printf("Enter a number \n");
    scanf("%ld",&num);
    while(num!=0)
    {
                 rem=num%10;
                 sum=sum+rem;
                 num=num/10;
                 }
                 
                 printf(" Sum of digits of entered number is : %d",sum);
                 getch();
                 return 0;
                 }


OUTPUT : 



WAP to retrieve command line arguments


/*
  Name: WAP to retrieve command line arguments
  Copyright:  Copyright Paul Griffiths 2000
  Author:  Parveen Malik
  Date: 23/09/11 01:40

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

int main(int argc, char *argv[]) {
    int n = 0;

    while ( n < argc ) {
	printf("Command line argument %d is %s\n", (n+1), argv[n]);
	++n;
    }

    return EXIT_SUCCESS;
}

WAP to find first 1000 prime numbers


/*
  Name: WAP to find first 1000 prime numbers 
  Copyright:  Copyright Paul Griffiths 2000
  Author:  Parveen Malik
  Date: 23/09/11 01:37


*/


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




int prime(int number, int * primes);


int main(void) {
    int primes[998] = { 3, 0 };
    int n = 5, i;
    int count = 0, found;


    printf("%8d%8d%8d", 1, 2, 3);               /*  Print first 3 primes     */




    /*  Find the next 997  */


    while ( count < 997 ) {
        i = 0;
        found = 1;




        /*  Test if number divides by any of the primes already found  */


        while ( primes[i] ) {
            if ( (n % primes[i++]) == 0 ) {   /*  If it does...              */
                found = 0;                    /*  ...then it isn't prime...  */
                break;                        /*  ...so stop looking         */
            }
        }


        if ( found ) {
            printf("%8d", n);                /*  If it's prime, print it...  */
            primes[i] =  n;                  /*  ...and add it to the list   */
            ++count;
        
        
            /*  Start a new line every 8 primes found  */
        
            if ( ((count + 3) % 8) == 0 )
                putchar('\n');
        }


        n += 2;     /*  There's no point testing even numbers, so skip them  */
    }


    putchar('\n');
getch();
    return EXIT_SUCCESS;
}


OUTPUT : 


Thursday, September 22, 2011

ATM Programming Demo


/*
  Name: ATM programming demo 
  Author: Parveen Malik
  Date: 22/09/11 09:47
  Description: 
*/


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


void main(void)
{ 
     unsigned long amount=1000,deposit,withdraw;
int choice,pin=0,k=0;
char another='y';


while(pin!=1234)
{
printf("Enter pin:");
scanf("%d",&pin);
}


do
{


printf("********Welcome to ATM Service**************\n");
printf("1. Check Balance\n");
printf("2. Withdraw Cash\n");
printf("3. Deposit Cash\n");
printf("4. Quit\n");
printf("******************?**************************?*\n\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nYour Balance is Rs : %lu ",amount);
break;
case 2:
printf("\nEnter the amount to withdraw: ");
scanf("%lu",&withdraw);
if(withdraw%100!=0)
{
printf("\nPlease enter amount in multiples of 100");
}
else if(withdraw>(amount-500))
{


printf("\nInsufficient Funds");
}
else
{


amount=amount-withdraw;


printf("\n\nPlease collect cash");


printf("\nYour balance is %lu",amount);
}
break;
case 3:
printf("\nEnter amount to deposit");
scanf("%lu",&deposit);
amount=amount+deposit;
printf("Your balance is %lu",amount);
break;
case 4:
printf("\nThank you for using ATM");
break;
default:
printf("\nInvalid Choice");
}
printf("\n\n\nDo you want another transaction?(y/n): ");
fflush(stdin);
scanf("%c",&another);
if(another=='n'||another=='N')
k=1;
}
while(!k);
printf("\n\nHave a nice day");
getch();


}



OUTPUT :

Tuesday, September 20, 2011

WAP to store vowels from one file to another


/*
  Name: WAP to store vowels from one file to another
  Author: Parveen Malik
  Date: 20/09/11 08:45
*/


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


void main()  
 {  
 char c;  
 FILE *f,*f1;  
 f=fopen("STRING","w");   
 printf("Enter String :\n ");  
 while(scanf("%c",&c)!=EOF)  
 putw(c,f);  
 fclose(f);  
 f=fopen("STRING","r");  
 f1=fopen("VOWELS","w");  
 while((c=getw(f))!=EOF)  
 if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u')||(c=='A')||(c=='E')||(c=='I')||(c=='O')||(c=='U'))  
 putw(c,f1);  
 fclose(f);  
 fclose(f1);  
 f1=fopen("VOWELS","r");  
 printf("\nVOWELS : \n");  
 while((c=getw(f1))!=EOF)  
 printf(" %c",c);  
 getch();  
 }


OUTPUT : 



Sunday, September 18, 2011

WAP to display the message "Hello World" without a Semicolon.

There are many ways to do this but i'm showing you three ways to print "Hello World" without putting any semicolon.


Solution:1

#include <stdio.h>

#include <conio.h> 
void main( )
{
if(printf("Hello World"))
{
}

getch();
}


Solution:2



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

void main( )
{
swicth(printf("Hello World"))
{
}

getch();
}





Solution:3



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

void main( )
{
while(printf("Hello World"))  /*infinite loop*/
{
}

getch();
}

WAP to print all Armstrong numbers between 1 to 1000


/*
  Name: WAP to print all Armstrong numbers between 1 to 1000
  Author: Parveen Malik
  Date: 18/09/11 10:45
*/


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


int main()
{
int no, temp, rem, sum;


printf("Armstrong numbers between 1 and 1000 are:\n");
for(no=1; no<=1000; no++)
{
temp=no;
sum=0;
while(temp>0)
{
rem=temp%10;
sum=sum+(rem*rem*rem);
temp=temp/10;
}
if(no==sum)
printf("\n%d", no);
}
getch( );
return 0;
}
    
OUTPUT: 



WAP to print binary triangle



/*
  Name: WAP to print binary triangle 
  Author: Parveen Malik
  Date: 18/09/11 05:40
*/


#include<stdio.h>
int main()
{ 
    int i,j,row,mod,temp=1;
char inputstr[35];  /* make longer than reasonable input will expected */
                   /* error checking should still be employed */
                   /* to clear buffer overflow, if input is too long */
                   
while ( temp==1)
{
      row=0;
      while(row<=0)
      {
                   printf("\n Enter the number of rows you want : ");
                   fflush(stdout); /* printf didn't have newline, need to flush */
                   fgets(inputstr,34,stdin);
                             /* error checking should go here for buffer overflow */
                             
       row = atoi (inputstr);
       if(row<=0)
       printf("invalid input, try again \n");
       }
      for ( i=0;i<=row-1;i++)
      {  
          mod=i%2;
          for(j=0;j<=i;j++)
          {
                           printf("%d",mod);
                           if(mod==0)
                           mod=1;
                           else 
                           mod=0;
                           }
                           printf("\n");
                          
                                                  }
                                                     }
                                                     
                                                     return 0;
                                                     }



OUTPUT: 


WAP to generate prime numbers


/*
  Name: WAP to generate the prime numbers 
  Author: Parveen Malik
  Date: 18/09/11 01:14
*/


#include<stdio.h>
#include<conio.h>
int main(void)
{
    int i,num,x,flag;
    printf("Enter the value upto which prime numbers is to be generated : ");
    scanf("%d",&num);
    for(i=1;i<=num;i++)
    {
                       x=2;
                       flag=0;
                       while(x<=i/2)
                       {
                                    if(i%x==0)
                                    {
                                              flag=1;
                                              break;
                                              }
                                              x++;
                                              }
                                              if(flag==0)
                                              printf("%5d",i);
                                              }
                                              getch();
                                              return 0;
                                              }


OUTPUT : 



WAP to generate fibonacci series


/*
  Name: WAP to generate fibonacci series
  Author: Parveen Malik
  Date: 18/09/11 01:08
*/

#include<stdio.h>
#include<conio.h>
int main(void)
{
    int f=0,s=1,temp,num,count;
    printf("Enter the value upto which series is to be generated :");
    scanf("%d",&num);
    count=3;
    printf("%5d%5d",f,s);
    while(count<=num)
    {
                     temp=f+s;
                     printf("%5d",temp);
                     f=s;
                     s=temp;
                     count++;
                     }
                     getch();
                     return 0;
                     }

OUTPUT : 

WAP to find the sum of the digits of a five number digit number



/*
  Name: WAP to find the sum of the digits of a five number digit number
  Author: Parveen Malik
  Date: 18/09/11 01:04
*/


#include<stdio.h>
#include<conio.h>
int main(void)
{ 
    long num,rem,temp;
    int sum=0;
    printf("Enter any five digit number : ");
    scanf("%ld",&num);
    temp=num;
    while(num>0)
    {
                rem=num%10;
                num=num/10;
                sum+=+rem;
                }
                printf("Sum of %ld\'s digit is = %d",temp,sum);
                getch();
                return 0;
                }


OUTPUT :

Thursday, January 6, 2011

Program to convert Celsius to Farenheit

/*  Name: Program to convert Celsius to Farenheit

Author: Parveen Malik

Date: 06/01/11 15:36*/


#include<stdio.h>

#include<conio.h>

void main()

{

int lower,upper,steps;

int celsius;

float farn;

lower=0;

upper=100;      //you can chanage these parameters like lower,upper and steps

steps=5;

celsius=lower;

printf("Celsius\t\tFarenheit\n\n");         //   /t tab

while(celsius<=upper)

{

farn=((9.0/5.0)*celsius)+32.0;       // formula to Celsius to Farenheit

printf("%d\t==>\t%.2f\n",celsius,farn);

celsius+=steps;

}

getch();

}



[caption id="attachment_156" align="aligncenter" width="655" caption="OUTPUT"]Program to convert Celsius to Farenheit[/caption]


Tuesday, January 4, 2011

Program to convert Farenheit to Celsius


/*Name: Program to convert Farenheit to Celsius

Author: Parveen Malik

Date: 05/01/11 10:37

*/


#include<stdio.h>

#include<conio.h>


void main()

{

int lower,upper,steps;

int farn;

float celcius;


lower=0;

upper=100; //you can chanage these parameters like lower,upper and steps

steps=5;


farn=lower;

printf("Fareneit\tCelcius\n");                 //   \t is used to give tab

while(farn<=upper)

{

celcius=5.0/9.0*(farn-32.0);                   // formula to convert Farenheit to Celcius

printf("%d\t\t%.2f\n",farn,celcius);

farn+=steps;

}

getch();

}




 Program to convert Farenheit to Celsius[/caption]