Showing posts with label Uncategorized. Show all posts
Showing posts with label Uncategorized. Show all posts

Friday, 10 January 2014

Index : Pyramid Programs in C Programming

 No Program View
1 .
View
2
View
3
View
4
View
5
View
6
View
7
View
8
View
9
View
10
View
11
View
12
View
13
2
2  4
2  4  6
2  4  6  8
View

C Programs : Number System Conversion in C

Programs : Number System Conversion in C

No Program Program Link
1 Decimal to Binary Conversion C Program
2 Decimal to Octal Conversion C Program
3 Decimal to Hexadecimal Conversion C Program
4 Binary to Decimal C Program
5 Decimal to Binary using Bitwise Operator C Program

Program to Print All ASCII Value Table in C Programming

Program to Print All ASCII Values in C Programming
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
char ch;
clrscr();
for(i=0;i<256;i++)
  {
    printf("%c ",ch);
    ch = ch + 1;
  }
}

Program to Check Whether Number is Perfect Or Not

Program to Check Whether Given Number is Perfect Or Not ?
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
  printf("nEnter a number: ");
  scanf("%d",&n);
  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("n%d is a Perfect Number",i);
  else
      printf("n%d is Non Perfect Number",i);
  return 0;
}

Programs : Check for Armstrong Number in C

Armstrong Number : When Sum of Cubes of Digits Of Number Equal to Same Given Number then the number is called as Armstrong Number.
#include<stdio.h>
int main()
{
int num,temp,sum=0,rem;

printf("nEnter Number For Checking Armstrong : ");
scanf("%d",&num);

temp = num;

 while (num != 0)
 {
  rem = num % 10;
  sum = sum + (rem*rem*rem);
  num = num / 10;
 }

if(temp == sum)
    printf("n%d is Amstrong Number",temp);
else
    printf("n%d is Amstrong Number",temp);
return(0);
}

Output :
Enter Number For Checking Armstrong : 153
153 is Amstrong Number
Explanation :
153 = [1*1*1] + [5*5*5] + [3*3*3]
    = 1 + 125 + 27
    = 153

Check Whether Number is Prime or not

#include<stdio.h>
int main()
{
    int num,i,count=0;
    printf("nEnter a number:");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++){
        if(num%i==0){
         count++;
            break;
        }
    }
   if(count==0)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   return 0;
}

Check Whether Given Number is Palindrome or Not ????

Check Whether Given Number is Palindrome or Not ????
#include<stdio.h>
#include<string.h>
int main()
{
int num,i,count=0;
char str1[10],str2[10];

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

sprintf(str1,"%d",num); // Convert Number to String

strcpy(str2,str1); // Copy String into Other
strrev(str2); // Reverse 2nd Number

count = strcmp(str1,str2);

if(count==0)
     printf("%d is a prime number",num);
else
     printf("%d is not a prime number",num);
return 0;
}

C Program to print Tower of Hanoi using recursion !!

#include<stdio.h>
#include<conio.h>
void TOH(int n,char x,char y,char z);
void main()
{
 int n;
 printf("nEnter number of plates:");
 scanf("%d",&n);
 TOH(n-1,'A','B','C');
 getch();
}
void TOH(int n,char x,char y,char z)
{
 if(n>0)
 {
  TOH(n-1,x,z,y);
  printf("n%c -> %c",x,y);
  TOH(n-1,z,y,x);
 }
}
Following Image will explain you more about tower of hanoi :

Find Sum of Digits of the Number using Recursive Function in C Programming

#include<stdio.h>
 int rem,sum; 
int calsum(int n)
 {if(n!=0){ rem=n%10; sum=sum+rem; calsum(n/10);}
  return sum;}//------------------------------------------------intmain(){int num,val; clrscr();printf("nEnter a number: ");scanf("%d",&num); val=calsum(num);printf("nSum of the digits of %d is: %d",num,x);return0;}
Output :
Enter a number: 123
Sum of the digits of 123 is : 6

C Program to generate the Fibonacci Series starting from any two numbers

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

int main()
{
int first,second,sum,num,counter=0;
clrscr();

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

printf("nEnter First Number : ");
scanf("%d",&first);

printf("nEnter Second Number : ");
scanf("%d",&second);

printf("nFibonacci Series : %d  %d  ",first,second);

while(counter< num)
    {
    sum=first+second;
    printf("%d  ",sum);
    first=second;
    second=sum;
    counter++;
    }
getch();
}

Output :
Enter the term : 5
Enter First Number : 1

Enter Second Number : 3

Fibonacci Series : 1 3 4 7 11 18 29

To Display of message using function in C Programming

Program : To Display of message using function
#include<stdio.h>
void message(); //Prototype Declaration
void main()
{
message(); // Function Call
}

void message() // Function Definition
{
printf("Hi , How are you ? ");
}
Output :
Hi , How are you ?

C Program to Add two Numbers Using Function in C Programming

Add Two Numbers By Using Function : [ C Program to Add two Numbers Using Function in C Programming ]
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
 void main()
 {
 int a,b,c;
 printf("nEnter the two numbers : ");
 scanf("%d %d",&a,&b);

 /* Call Function Sum With Two Parameters */
 c = sum(a,b);

 printf("nAddition of two number is : ");
 getch();
 }

 int sum ( int num1,int num2)
 {
 int num3;
 num3 = num1 + num2 ;
 return(num3);
 }
Output :
Enter the two numbers : 12 15
Addition of two number is : 27

C Program to calculate sum of numbers 1 to N using recursion

#include<stdio.h>
#include<conio.h>
int add(int);
//-------------------------------------
void main()
{
int i,num;
int sum;
clrscr();
printf("Input a number : ");
scanf("%d",&num);
sum=add(num);
printf("nSum of Number From 1 to %d :%d",num,sum);
}
//---------------------------------------
int add(int m)
{
int sum;
if(m==1)
    return(1);
else
   sum=m+add(m-1);
return(sum);
}

Output :
Input a number : 5
Sum of Number From 1 to 5 :15

C Program to Display same Source Code as Output

Program :
#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;
}
Explanation :
fp = fopen(__FILE__,"r");
[arrowlist]
  • __FILE__ is Standard Predefined Macros in C Programming.
  • This macro will expand to the name of current file path.
  • Suppose we have saved this source code at path -
[/arrowlist]
c://tc/bin/file1.c
then
fp = fopen(__FILE__,"r");
will be expanded as -
fp = fopen("c://tc/bin/file1.c","r");

C Program to Multiply two Matrices using Recursion !!

Introduction :
[arrowlist]
  • We can multiply 2 matrices without using function.
  • Visit this article to know Detailed Steps for Matrix Multiplication.
[/arrowlist] Visual Representation :
Program :
#include<stdio.h>
#define MAX 10

void matMult(int [MAX][MAX],int [MAX][MAX]);
int r1,r2,c1,c2;
int c[MAX][MAX];

int main(){

    int a[MAX][MAX],b[MAX][MAX],i,j,k;

    printf("Enter the row and column of first matrix: ");
    scanf("%d%d",&r1,&c1);
    printf("Enter the row and column of second matrix: ");
    scanf("%d%d",&r2,&c2);

    if(c1!=o){
         printf("Matrix mutiplication is not possible");
    }
  else{

      printf("Enter the First matrix: ");
      for(i=0;i<r1;i++)
      for(j=0;j<c1;j++)
           scanf("%d",&a[i][j]);

      printf("Enter the Second matrix: ");
      for(i=0;i<r2;i++)
      for(j=0;j<c2;j++)
           scanf("%d",&b[i][j]);

      printf("nThe First matrix is: n");
      for(i=0;i<r1;i++){
      printf("n");
      for(j=0;j<c1;j++){
           printf("%dt",a[i][j]);
      }
      }

      printf("nThe Second matrix is: n");
      for(i=0;i<r2;i++){
      printf("n");
      for(j=0;j<c2;j++){
           printf("%dt",b[i][j]);
      }
      }

      matMult(a,b);

  }

  printf("nThe multiplication of two matrixes is: n");
  for(i=0;i<r1;i++){
      printf("n");
      for(j=0;j<c2;j++){
           printf("%dt",c[i][j]);
      }
  }
  return 0;
}

void matMult(int a[MAX][MAX],int b[MAX][MAX]){

    static int sum,i=0,j=0,k=0;

    if(i<r1) //row of first matrix
    {
      if(j<c2) //column of second matrix
      {
         if(k<c1)
         {
             sum=sum+a[i][k]*b[k][j];
             k++;
             matMult(a,b);
         }
         c[i][j]=sum;
             sum=0;
             k=0;
             j++;
             matMult(a,b);
      }
        j=0;
        i++;
        matMult(a,b);
    }
}
output :
Enter the row and column of first matrix: 2 2
Enter the row and column of second matrix: 2 2
Enter the First matrix: 1 2 3 4
Enter the Second matrix: 1 2 1 2

The First matrix is:

1       2
3       4
The Second matrix is:

1       2
1       2
The multiplication of two matrixes is:

3       6
7       14

How to Verify Multiplication ?

Visit This External Tool Which Will Calculate Multiplication of Two Matrices

Printing Message On Screen – Using Cout in C++

Information Keep in Mind :
  1. cout is an object of class ostream that represents the standard output stream.
  2. It corresponds to the cstdio stream stdout.
General Purpose : 
  1. Prints Message on Screen.
  2. Printing Value of Variable on Screen.
Version 1 : .h Style
#include<iostream.h>
int main()
{
cout <<"Hello,Welcome to C++";
return(0);
}

Output :
Hello,Welcome to C++