Pages

Monday, February 17, 2014

Program which produces its own source code as output.

This c program produces its own source code as its output
such a program is called a quine.This kind of program takes no input and produces a copy of its own source code as its only output.


#include <stdio.h>
int main(){
FILE  *fp;
char c;
fp=fopen(_FILE_,"r");
do{
       c=getc(fp);
       putchar(c);

}while(c!=EOF)

fclose(fp);
return 0;

}


output

#include <stdio.h>
int main(){
FILE  *fp;
char c;
fp=fopen(_FILE_,"r");
do{
       c=getc(fp);
       putchar(c);

}while(c!=EOF)

fclose(fp);
return 0;

}


Thursday, February 13, 2014

Google's Goglogo

Its a very interesting and entertaining application.Goglogo is a Google logo maker. It allows you to create Google style search engine for yourself. You can use your name or any name of your choice in place of google.
I tried this trick and it works fine.Its fun.I am sure you will like it too.

Follow following steps.
















2.Enter your name or name of your choice.
3.Click on create my search page now.





Monday, January 02, 2012

Program for odd and even number

This program determines wether a given number is odd or even.

#include<stdio.h>

void main(){
int num;
clrscr();
printf("Enter number : ");
scanf("%d",&num);
if(num%2==0)
printf("%d is an even number");
else
printf("%d is an odd number");
getch();

}

output:

Enter number : 4
4 is an even number

Enter number : 71
71 is an odd number


This program finds out odd or even numbers within a range

#include<stdio.h>

void main(){
int min,max,num;
clrscr();
printf("Enter minimum range : ");
scanf("%d",&min);
printf("Enter maximum range : ");
scanf("%d",&max);

printf("\nEven numbers in given range are : ");
for(num=min;num<=max;num++)
if(num%2==0)
printf("%d ",num);
printf("\n\nOdd numbers in given range are : ");

for(num=min;num<=max;num++)
if(num%2!=0)
printf("%d ",num);
getch();

}

output:

Enter minimum range 2
Enter maximum range 20

Even numbers in given range are : 2 4 6 8 10 12 14 16 18 20

Odd numbers in given range are : 1 3 5 7 9 11 13 15 17 19

Adding 1 to each digit of a number

This program adds 1 to each digit of the number.


If a five digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits.For example if the number that is input is 12391 then the output should be displayed as 23402


Here first we have added one to each digit. If any digit becomes 10 then replacing it with 0

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

long r,num,temp,sum=0,i,temp_sum=0;
clrscr();

printf("Enter 5 digit number : ");
scanf("%ld",&num);

printf("\nThe number after adding 1 to each digit : ");

while(num!=0){
r=num%10;
temp=r+1;
if(temp==10)
temp=0;
num=num/10;
sum=(sum*10)+temp;

}

while(sum!=0){
i=sum%10;
sum=sum/10;
temp_sum=(temp_sum*10)+i;
}
printf("%ld",temp_sum);
getch();

}


output:

Enter 5 digit number : 12345
The number after adding 1 to each digit : 23456

Enter 5 digit number : 12935
The number after adding 1 to each digit : 23046

Tuesday, December 27, 2011

Program to find Armstrong number

Definition :

From c's programming point of view, Those numbers whose sum of the cube of its digits is equal to that number are known as Armstrong numbers .
For example

153 = 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
or
370 = 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370

But in general the definition is :

Those numbers whose sum of its digits to power of number of its digits is equal to that number are known as Armstrong numbers.

Let k be the number of digits in a number, n, and d1,d2,d3,d4... be the digits of n.
n=d1k+d2k+d3k+d4k+...


Write a C program to check whether a number is Armstrong or not.


#include<stdio.h>

void main(){
int num,sum=0,temp,i;
clrscr();
printf("Enter number: ");
scanf("%d",&num);

temp=num;
while(num!=0){
i=num%10;
num=num/10;
sum=sum+(i*i*i);

}

if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not a Armstrong number",temp);
getch();

}


Output:

Enter number: 153
153 is an Armstrong number

Enter number: 207
207 is not a Armstrong number

Armstrong number in c using for loop

#include<stdio.hgt;

void main(){
int temp,sum=0,i,num;
clrscr();

printf("Enter number : ");
scanf("%d",&num);

for(temp=num;num!=0;num=num/10){
i=num%10;
sum=sum+(i*i*i);

}
if(temp==sum)
printf("%d is an Armstrong Number ",temp);
else
printf("%d is not a Armstrong Number ",temp);
getch();

}

Thursday, December 22, 2011

Program to find sum of all digits of a number

This program calculates the sum of all digits of a number into a single digit.for instance if user has input 2345..the end result will be 2345=14=5


If a number of any digit is iput through keyboard,write a program to obtain the sum of all digits of the number into a single digit


#include<stdio.h>
void main()
{
long num,temp,total=0,r,sum=0;
clrscr();

printf("Enter the number : ");
scanf("%ld",&num);

// it will give sum of all digits
while(num!=0)
{
r=num%10;
num=num/10;
total=total+r;

}

// This will further sum up the total of digits
while(total!=0)
{
temp=total%10;
total=total/10;
sum=sum+temp;
}

printf("Total=%ld ",sum);
getch();

}

Program to find sum of first and last digit of a number of any digit

This program finds the sum of first and last digit of a number that would be of any length


If a number of any digit is input through the keyborad,write a program to obtain the sum of first and last digit of this number.


#include<stdio.h>

void main(){

/*
since integer range is between -32768 to 32767
it will support till 5 digit number only
so for higher digits you have to declare
with long
*/

long firstdigit,sum=0,r,num;
clrscr();

printf("Enter number : ");
scanf("%ld",&num);

sum=sum+num%10; // num%10 gives lastdigit

while(num!=0){
firstdigit=num; // gives first digit
num=num/10;
}

sum=sum+firstdigit;

printf("Sum of first and last digit : %ld",sum);
getch();


}