Monday, November 24, 2014

C Excellant pro



C Program to Illustrate Pass by Value

 
#include 
#include   
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int num1 = 10, num2 = 20;
printf("Before swapping num1 = %d num2 = %d\n", num1, num2);
swap(num1, num2);
printf("After swapping num1 = %d num2 = %d \n", num2, num1);
return 0;
}

Output
 
Before swapping num1 = 10 num2 = 20
After swapping num1 = 20 num2 = 10

C Program to Illustrate Pass by Reference


#include 
#include   
void cube( int *x);
int main()
{
int num = 10;
cube(&num);
printf("the cube of the given number is %d", num);
return 0;
}
void  cube(int *x)
{
*x = (*x) * (*x) * (*x);
}

Output
 
Enter file name: pgm2.c
There are 43 lines in pgm2.c  in a file

C Program to Print Armstrong Number from 1 to 1000

 
#include 
#include  
main()
{
int number, temp, digit1, digit2, digit3;
printf("Print all Armstrong numbers between 1 and 1000:\n");
number = 001;
while (number <= 900)
{
digit1 = number - ((number / 10) * 10);
digit2 = (number / 10) - ((number / 100) * 10);
digit3 = (number / 100) - ((number / 1000) * 10);
temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 * digit3);
if (temp == number)
{
printf("\n Armstrong no is:%d", temp);
}
number++;
}
}
 
Output
 
Print all Armstrong numbers between 1 and 1000:
 
Amstrong no is:1
Amstrong no is:153
Amstrong no is:370
Amstrong no is:371
Amstrong no is:407

C Program to Check whether a given Number is Perfect Number

 
#include 
#include  
int main()
{
int number, rem, sum = 0, i;
printf("Enter a Number\n");
scanf("%d", &number);
for (i = 1; i <= (number - 1); i++)
{
rem = number % i;
if (rem == 0)
{
sum = sum + i;
}
}
if (sum == number)
printf("Entered Number is perfect number");
else
printf("Entered Number is not a perfect number");
return 0;
}
 
Output
 
Enter a Number
6
Entered Number is perfect number

C Program to Check whether a given Number is Armstrong

 
#include 
#include
#include 
void main()
{
int number, sum = 0, rem = 0, cube = 0, temp;
printf ("enter a number");
scanf("%d", &number);
temp = number;
while (number != 0)
{
rem = number % 10;
cube = pow(rem, 3);
sum = sum + cube;
number = number / 10;
}
if (sum == temp)
printf ("The given no is armstrong no");
else
printf ("The given no is not a armstrong no");
}
 
Output
 
Enter a number370
The given no is armstrong no
 
Enter a number1500
The given no is not a armstrong no

Display its own Source Code as its Output

#include 
#include   
int main()
{
FILE *fp;
char ch;
fp = fopen(__FILE__,"r");
do
{
ch = getc(fp);
putchar(ch);
}
while (ch != EOF);
fclose(fp);
return 0;
}

No comments:

Post a Comment