Wednesday 31 August 2011

C program to get last two digits of year


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








C program to extract last two digits of a given year
and print it



#include

int

Split number into digits in c programming


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}







Extract digits from integer in c language



#include

int main(){

  int num,temp,

C program to count number of digits in a number


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








Code 1:

Count
the number of digits in c programming language



#include

int main(){

Monday 29 August 2011

Find out Factioral value


Ans.

#include<stdio.h>
#include<conio.h>
void main()
{
 int fact=1,num;
 clrscr();
 printf("\nEnter Number : ");
 scanf("%d",&num);
 while(num>=1)
 {
  fact=fact*num;
  num--;
 }
 printf("\nFactorial value of %d is %d",num,fact);
}

       Output of above program : 
Enter Number : 5
Factorial value of 5 is 120
 

operation's number(sum,reverse,store in other variable)

Q. Write a program to insert a number and do following tasks:
 a.Display in reverse order?
 b.Reverse in another variable and display it?
 c.Display sum of its all digit?

Ans.

#include<stdio.h>
#include<conio.h>
void main()
{
 int x,num,rem,sum=0,var=0;
 clrscr();
 printf("\nPress Number 1 for reverse number");
 printf("\nPress Number 2 for reverse number in another variable");
 printf("\nPress Number 3 for sum of entered number");
 scanf("%d",&x);
 switch(x)
 {
 case 1 :
  printf("\nEnter Number : ");
  scanf("%d",&num);
  while(num>=1)
  {
   rem=num%10;
   printf("Reverse number : %d",rem);
   num=num/10;
  }
  break;
 case 2 :
  printf("Enter Number : ");
  scanf("%d",&num);
  while(n>=1)
  {
   rem=n%10;
   var=var*10+rem;
   n=n/10;
  }
  printf("%d",var);
  break;
 case 3 :
  printf("\nEnter Number : ");
  scanf("%d",&num);
  while(num>=1)
  {
   rem=num%10;
   sum=sum+rem;
   num=num/10;
  }
  printf("Sum of %d is %d",num,sum);
  break;
 default :
   printf("You enter wrong number!!!");
   break;
 }
}

       Output of above program : 

Press Number 1 for reverse number
Press Number 2 for reverse number in another variable
Press Number 3 for sum of entered number

1
Enter Number : 4812
Reverse number : 2184
 

Print table in following format

Q. Write a program to printout following table:
 (where num=5)
       
         5*1=5
         5*2=10
         5*3=15
         5*4=20
         5*5=25
         5*6=30
         5*7=35
         5*8=40
         5*9=45
         5*10=50

Ans.
 
#include<stdio.h>
#include<conio.h>
int main()
{
 int x=1,num,res;
 printf("Enter Number : ");
 scanf("%d",&num);
 while(x<=10)
 {
   res=num*x;
   printf("\n%d*%d=%d",num,x,res);
   x++;
 }
 getch();
 return 0;
}

       Output of above program : 
Enter Number : 5

         5*1=5
         5*2=10
         5*3=15
         5*4=20
         5*5=25
         5*6=30
         5*7=35
         5*8=40
         5*9=45
         5*10=50

Sunday 28 August 2011

Print one to ten using for loop

Q. Write a program to printout one to ten counting using for loop?

Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
  int x;
  for(x=1; x<=10; x++)
    printf("\n%d",x);
  getch();
  return 0;
}

     Output of above program : 
  1
  2
  3
  4
  5
  6
  7
  8
  9
  10

Print one to ten program

Q. Write a program to printout one to ten counting using while loop?
Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 int x=1;
 while(x<=10)
 {
   printf("\n%d",x);
   x++;
 }
 getch();
 return 0;
}

       output of above program : 

  1
  2
  3
  4
  5
  6
  7
  8
  9
  10

comparison of three numbers

Q. Write a program to compare three numbers which is greatest,middle and lowest?

Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z;
 printf("Enter values of x, y and z : ");
 scnaf("%d%d%d",&x,&y,&z);
 if(x>=y && x>=z)
   printf("\nx=%d is greatest",a);
 else if(y>=z)
   printf("\ny=%d is greatest",y);
 else
   printf("\nz=%d is greatest",z);
 getch();
 return 0;
}

      Output of above program :
Enter values of x,y and z : 30
20
100

z=100 is greatest

C program without main function






Can
you write a c program without using main function?





We can write a c program without
using main function. We can compile it but we cannot execute a program without
a main function. Since in C execution of any program start from main function.  Examples of c program without a main is all
the c library functions.  Function printf
is an example of library function which has been written

find greatest number using conditional operator

Q. Using conditional operators write a program to find out in three Numbers which is greatest number?
Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 int n1,n2,n3;
 prinft("Enter Numbers n1, n2, n3 : ");
 scanf("%d"%d%d",n1,n2,n3);
 printf("Greatest=%d",n1>n2?n1>n3?x:z:y>z?y:z);
 getch();
 return 0;
}


       Output of above program : 
Enter Numbers n1, n2, n3 :10
20
30

Greatest=30

Simple Swapping C Program

Q. Write a simple program to insert two values through keyboard and interchange then values?
Example:if x=10
           y=20
        then after interchange
          x=20
          y=10
       
Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z;
 printf("Enter Values x and y : ");
 scnaf("%d%d",&x,&y);
 z=y;
 y=x;
 x=z;
 printf("\nValue of x=%d",x);
 printf("\nValue of y=%d",y);
 getch();
 return 0;
}

        Output of above program : 
Enter Values x and y : 25
35
Value of x= 35
Value of y= 25

Add two numbers

Q. Write a program to add two numbers?

Ans.

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,sum;
 clrscr();
 printf("Enter Numbers a and b : ");
 scnaf("%d%d",&a,&b);
 sum=a+b;
 printf("\nsum(a+b)=%d",sum);
 getch();
}

     Output of above program : 

Enter Numbers a and b : 100
200

sum(a+b)=300

Sample C Program

Q. Write a simple C program,in which user entered his name,age through keyboard and display on console?

Ans.

/*c program to accept name & age from user and display it*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char nam[20];
 int age;
 printf("Enter Your Name : ");
 scnaf("%s",&nam); // gets(nam)
 printf("Enter Your Age : ");
 scanf("%d",&age);
 printf("\nUser Name is %s",nam); 
 /*You also use puts(nam)*/
 printf("\nAnd %s age is %d",nam,age);
 getch();
 return 0;
}

       Output of above program : 

Enter your name : JamesBond
Enter your age : 35

And JamesBond age is 35


c faqs with answers pdf



This PDF doc keeps faq or frequently asked questions and answers  of c programming language. To free download the pdf doc go to the File -> Download Original

C in depth pdf




This PDF doc keeps c programming questions and answer with explanation in depth. To free download the pdf doc go to the File -> Download Original

C language questions and answers pdf


C Programming language questions and answers in PDF format free to download

This PDF doc keeps C language questions and answers with explanation. To free download the pdf doc go to the File -> Download Original

C language questions and answers with explanation

C multiple choice questions and answers pdf



C
programming language multiple choice questions and answers in pdf format for
interview free download






This PDF doc keeps mcq or multiple choice questions and answers with explanation of c programming language. To free download the pdf doc go to the File -> Download Original

C objective questions and answers pdf




Objective
c tutorial for beginners to learn objective type questions with answers in c
programming language in pdf format. C objective
questions and answers pdf Download free





This PDF doc keeps objective questions and answers with explanation of c programming language. To free download the pdf doc go to the File -> Download Original

c test questions and answers pdf



Online
written test questions and answers in c programming language in pdf form free
to download



This PDF doc keeps  sample test questions and answers of c programming language. To free download this pdf doc go to File -> download Original 

Test your C skills pdf


Test your C programming language skills  for written test exam  and interview pdf free download 





This PDF documents keeps tricky questions and answers to test your c skills.  To free download the pdf doc go to the File  ->  Download Original 

Saturday 20 August 2011

count the array elements


Write a C Program to count the array elements



#include<conio.h>
#include<stdio.h>
void main()
{
int a[10],i,c=0;
clrscr();
printf("enter array elements=");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
c++;
}
printf("counting elements of an array is:%d",c);
getch();
}



Program Coded by: Ashish Garg, Patiala (India)

Friday 19 August 2011

Subtraction of Two Matrices


C Program for subtraction of two matrices 

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

//Read Matrix
void read_mat(float a[][10],int m,int n)
{
int i,j;
printf("\n\nEnter %d X %d matrix below:\n",m,n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
}

//Write Matrix
void write_mat(float a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%10.2f",a[i][j]);
printf("\n");
}
}

//Subtract matrices
void sub_mat(float a[][10],float b[][10],float c[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j] - b[i][j];
}

//main function
void main()
{
float x[10][10],y[10][10],z[10][10];
int m,n;
clrscr();

//Accept two matrices
printf("\n\nEnter order of matrix \n");
scanf("%d %d",&m,&n);
read_mat(x,m,n);
read_mat(y,m,n);

//call sub_mat() function
sub_mat(x,y,z,m,n);
printf("\n\nSUBTRACTION OF THE GIVEN MATRICES IS:\n");
write_mat(z,m,n);
getch();
}

Find Inverse of a Given Matrix


Write a Program to find inverse of a given matrix

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

//Read Matrix
void read_mat(float a[][10],int n)
{

int i,j;
printf("\n\nEnter %d X %d matrix below:\n",n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);

}

//Write Matrix
void write_mat(float a[][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%10.2f",a[i][j]);
printf("\n");
}
}

//Swap Rows
void swap_rows(float a[][10],int n,int i,int j)
{
int k,temp;
for(k=0;k<n;k++)
{
temp=a[i][k];
a[i][k]=a[j][k];
a[j][k]=temp;
}
}

//Multiplication of rows
void row_mult(float a[][10],int n,int i,float x)
{
int k;
for(k=0;k<n;k++)
a[i][k]*=x;
}

// Subtraction of rows
void row_sub(float a[][10],int n,int i,int j,float x)
{
int k;
for(k=0;k<n;k++)
a[j][k]-=x*a[i][k];
}

//Inverse of matrix
void inverse(float a[][10],float ia[][10],int n)
{
int i,j;
for(i=0;i1.0e-6)
break;
if(j==n)
{
printf("Inverse does not exist");
getch();
exit(0);
}
swap_rows(a,n,i,j);
swap_rows(ia,n,i,j);
}
row_mult(ia,n,i,1/a[i][i]);
row_mult(a,n,i,1/a[i][i]);
for(j=0;j<n;j++)
if(i!=j)
{
row_sub(ia,n,i,j,a[j][i]);
row_sub(a,n,i,j,a[j][i]);
}
}
}

//main function
void main()
{
float a[10][10],b[10][10];
int n;
clrscr();

//Accept Matrix
printf("\n\nEnter order of the square matrix \n");
scanf("%d",&n);
read_mat(a,n);

//inverse the matrix
inverse(a,b,n);
printf("\n \nInverse of the given square matrix is : \n");
write_mat(b,n);
getch();
}

Factorial off a number using "do while" loop


#include<stdio.h>
#include<conio.h>
void main()
{
int n,f=1;
clrscr();
printf("enter any number=");
scanf("%d",&n);
do
{
f=f*n;
n--;
}
while(n>0);
printf("factorial number is=%d",f);
getch();
}
Courtesy: Ashish Garg
Patiala, India

Add numbers using command line arguments (CLA)

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

void main(int argc,char *argv[])
{
int sum=0,i;
//Compare if proper number of arguments have been entered
if(argc<3)

{
printf("Insufficient number of arguments:\n");
getch();
return 0;
}

//Add all the numbers entered using atoi function
for(i=1;i<argc;i++)
{
sum+=atoi(argv[i]);
}

//print the sum
printf("Ans=%d",sum);
getch();
}


Factorial of Number Using While Loop

#nclude <stdio.h>
#include <conio.h>
void main()
{
int i=1,n,fact=1;
clrscr();
printf("enter a number");
scanf("%d",&n);
while(i<=n)
{
fact=fact*i;
printf("%d",i);
i++;
}
printf("fact=%d",fact);
getch();
}

Thursday 18 August 2011

Multiply and swap 2 nmbers using bitwise operators



#include<stdio.h>
#include<conio.h>
void main(void)
{
int a,b;
clrscr();
printf("Input value of A: ");
scanf("%d",&a);
printf("Input value of B: ");
scanf("%d",&b);
a=a^b;
b=b^a;
a=b^a;
printf("After Swapping:\n");
printf("Value of A: %d",a);
printf("\nValue of B: %d",b);
getch();
}

Wap to reverse words



‎#include <stdio.h>
#include <conio.h>
#include <string.h>
const int opt = 50;
int main(){
int i=0,j=0,c=0,vali[opt] = {0};
char str1[opt],str2[opt]="",str3[opt]="",ch;
printf("please enter your sentence :");
gets(str1);
for(i=0;str1[i] != '\0';i++){
if(str1[i] == ' '){
vali[j+1] = i;
j++;
}
}
c = j;
for(i=0;i<=j && c!=0;i++){
strcat(str2,&str1[vali[c]+1]);
strcpy(&str1[vali[c]],str3);
strcat(str2," ");
c--;
}
strcat(str2,str1);
printf("\nyour reversed sentence is :");
puts(str2);
getch();
return 0;
}

Pyramid using nested for loops




#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
}






OUTPUT


5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

Whether the given no. Is palindrome or not



  #include<stdio.h>
  #include<conio.h>
  void main()
  {
   int n,r,t,sum=0;
   clrscr();
   printf("    OUTPUT:\n");
   printf("\tEnter a no.");
   scanf("%d",&n);
   t=n;
   while(n!=0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
   if(sum==t)
   printf("\tThe no. %d is a pallindrome",t);
   else
   printf("\tThe no. %d is not a pallindrome",t);
   getch();
  }

Whether the given no. is armstrong or not


  #include<stdio.h>
  #include<conio.h>
  void main()
   {
    int n,r,t,sum=0;
    clrscr();
    printf("  OUTPUT :\n");
    printf("\tEnter a no.");
    scanf("%d",&n);
    t=n;
    while(n!=0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
   if(sum==t)
   printf("The no. %d is armstrong",t);
   else
   printf("The no. %d is not an armstrong",t);
   getch();
  }

Concatenate Two Strings



  #include<stdio.h>
  #include<conio.h>
  #include<string.h>
   void main()
    {
     char c[100];
     char a[50];
     char b[50];
     clrscr();
     printf("Enter a string1:");
     gets(a);
     printf("Enter a string2:");
     gets(b);
     strcat( a,b);
     printf("%s",a);
     getch();
    }

Print Second Largest Among Given Three No.s



  #include<stdio.h>
  #include<conio.h>
  void main()
   {
    int a,b,c;
    clrscr();
    printf("   OUTPUT :\n");
    printf("Enter any three no.s:");
    scanf("%d%d%d",&a,&b,&c);
    if(a>b&&a>c)


if(b>c)
printf("%d is the second largest no.",b);
else
printf("%d is the second largest no.",c);


    if(b>a&&b>c)


if(a>c)
printf("the second largest no. is % d",a);
else
printf(" the second largest no. is %d",c);


    if(c>a&&c>b)


if(b>a)
printf("second largest no. is %d",b);
else
printf("second largest no. is %d",a);


    getch();
   }