C question and answer




16. PRINTING ASCII VALUE
void main()
{
  int i;
  clrscr();
  for(i=0;i<=255;i++)
  {
            printf("%d -> %c ",i,i);
            delay(10);
  }
  getch();
}
17. CHECKING LEAP YEAR
void main()
{
  int year;
  clrscr();
  printf("Enter any year->");
  scanf("%d",&year);
  if(((year%4==0)&&(year%100!=0))||(year%400==0))
            printf("%d is a leap year",year);
  else
            printf("%d is not a leap year",year);
  getch();
}
18. CONVERSION OF DECIMAL TO BINARY
void main()
{
  int n,m,no=0,a=1,rem;
  clrscr();
  printf("Enter any decimal number->");
  scanf("%d",&n);
  m=n;
  while(n!=0)
  {
            rem=n%2;
            no=no+rem*a;
            n=n/2;
            a=a*10;
  }
   printf("The value %d in binary is->",m);
   printf("%d",no);
  getch();
}
19. CONVERSION OF BINARY TO DECIMAL
void main()
{
  long int no,n=0,j=1,rem,no1;
  clrscr();
  printf("Enter any number any binary form->");
  scanf("%ld",&no);
  no1=no;
  while(no!=0)
  {
            rem=no%10;
            n=n+rem*j;
            j=j*2;
            no=no/10;
  }
  printf("\nThe value of binary no. %ld is ->%ld",no1,n);
  getch();
}
20. SWAPING OF TWO ARRAYS
void main()
{
  int a[10],b[10],c[10],i;
  clrscr();
  printf("Enter First array->");
  for(i=0;i<10;i++)
  scanf("%d",&a[i]);
  printf("\nEnter Second array->");
  for(i=0;i<10;i++)
            scanf("%d",&b[i]);
  printf("Arrays before swapping");
  printf("\nFirst array->");
  for(i=0;i<10;i++)
  {
            printf("%d",a[i]);
  }
  printf("\nSecond array->");
  for(i=0;i<10;i++)
  {
            printf("%d",b[i]);
  }
  for(i=0;i<10;i++)
  {
            //write any swapping technique
            c[i]=a[i];
            a[i]=b[i];
            b[i]=c[i];
  }
  printf("\nArrays after swapping");
  printf("\nFirst array->");
  for(i=0;i<10;i++)
  {
            printf("%d",a[i]);
  }
  printf("\nSecond array->");
  for(i=0;i<10;i++)
  {
            printf("%d",b[i]);
  }
  getch();
}
21. FINDING NCR FACTOR
void main()
{
  int n,r,ncr;
  clrscr();
  printf("Enter any two numbers->");
  scanf("%d %d",&n,&r);
  ncr=fact(n)/(fact(r)*fact(n-r));
  printf("The NCR factor of %d and %d is %d",n,r,ncr);
  getch();
}
 int fact(int n)
{
  int i=1;
  while(n!=0)
  {
            i=i*n;
            n--;
  }
  return i;
}
22. PASCAL’S TRIANGLE
void main()
{
  int line,i,j,k;
  clrscr();
  printf("Enter the no. of lines");
  scanf("%d",&line);
  for(i=1;i<=line;i++)
  {
            for(j=1;j<=line-i;j++)
                        printf(" ");
            for(k=1;k<i;k++)
                        printf("%d",k);
            for(k=i;k>=1;k--)
                        printf("%d",k);
            printf("\n");
  }
  getch();
}
23. CONVERSION FROM UPPERCASE TO LOWER CASE
void main()
{
  char str[20];
  int i;
  clrscr();
  printf("Enter any string->");
  scanf("%s",str);
  printf("The string is->%s",str);
  for(i=0;i<=strlen(str);i++)
  {
            if(str[i]>=65&&str[i]<=90)
            str[i]=str[i]+32;
  }
  printf("\nThe string in uppercase is->%s",str);
  getch();
}
24. CONVERSION FROM LOWER CASE TO UPPER CASE
void main()
{
  char str[20];
  int i;
  clrscr();
  printf("Enter any string->");
  scanf("%s",str);
  printf("The string is->%s",str);
  for(i=0;i<=strlen(str);i++)
  {
            if(str[i]>=97&&str[i]<=122)
            str[i]=str[i]-32;
  }
  printf("\nThe string in lowercase is->%s",str);
  getch();
}
25. DELETE THE VOWELS FROM A STRING
void main()
{
  char str[20],s[20];
  int i,j=0;
  clrscr();
  printf("Enter any string->");
  scanf("%s",str);
  printf("The string is->%s",str);
  for(i=0;i<=strlen(str);i++)
  {
            if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
            str[i]=' ';
            else
            s[j++]=str[i];
  }
  s[j]='\0';
  printf("\nThe string without vowel is->%s",s);
  getch();
}
26. ADDITION OF MATRICES
void main()
{
  int a[3][3],b[3][3],c[3][3],i,j;
  clrscr();
  printf("Enter the First matrix->");
  for(i=0;i<3;i++)
            for(j=0;j<3;j++)
                        scanf("%d",&a[i][j]);
  printf("\nEnter the Second matrix->");
  for(i=0;i<3;i++)
              for(j=0;j<3;j++)
                        scanf("%d",&b[i][j]);
  printf("\nThe First matrix is\n");
  for(i=0;i<3;i++)
  {
            printf("\n");
            for(j=0;j<3;j++)
                        printf("%d\t",a[i][j]);
  }
  printf("\nThe Second matrix is\n");
  for(i=0;i<3;i++)
  {
            printf("\n");
            for(j=0;j<3;j++)
                        printf("%d\t",b[i][j]);
   }
   for(i=0;i<3;i++)
            for(j=0;j<3;j++)
                        c[i][j]=a[i][j]+b[i][j];
   printf("\nThe Addition of two matrix is\n");
   for(i=0;i<3;i++)
   {
            printf("\n");
            for(j=0;j<3;j++)
                        printf("%d\t",c[i][j]);
   }
getch();
}
27. STRING PALINDROME
#include"string.h"
void main()
{
  char *str,*rev;
  int i,j;
  clrscr();
  printf("\nEnter a string:");
  scanf("%s",str);
  for(i=strlen(str)-1,j=0;i>=0;i--,j++)
            rev[j]=str[i];
            rev[j]='\0';
  if(strcmp(rev,str))
            printf("\nThe string is not a palindrome");
  else
            printf("\nThe string is a palindrome");
  getch();
}
28. COPY DATA FROM ONE FILE TO ANOTHER FILE
#include"stdio.h"
void main()
{
  FILE *p,*q;
  char file1[20],file2[20];
  char ch;
  clrscr();
  printf("\nEnter the source file name to be copied:");
  gets(file1);
  p=fopen(file1,"r");
  if(p==NULL)
  {
            printf("cannot open %s",file1);
            exit(0);
  }
  printf("\nEnter the destination file name:");
  gets(file2);
  q=fopen(file2,"w");
  if(q==NULL)
  {
            printf("cannot open %s",file2);
            exit(0);
  }
  while((ch=getc(p))!=EOF)
             putc(ch,q);
  printf("\nCOMPLETED");
  fclose(p);
  fclose(q);
 getch();
}
29. ADDITION & SUBTRACTION OF TWO COMPLEX NUMBERS
void main()
{
  int a,b,c,d,x,y;
  clrscr();
  printf("\nEnter the first complex number:");
  scanf("%d%d",&a,&b);
  printf("\nEnter the second complex number:");
  scanf("%d%d",&c,&d);
  if(b<0)
            printf("%d-i\n",a-b);
  else
            printf("d+i\n",a+b);
  if(d<0)
            printf("d-i\n",c-d);
  else
            printf("%d+i\n",c+d);
  printf("\nADDITION ");
  x=a+c;
  y=b+d;
  if(y>0)
            printf("%d-i%d",x,-y);
  else
            printf("%d+i%d",x,+y);
  printf("\n\nSUBTRACTION ");
  x=a-c;
  y=b-d;
  if(y<0)
            printf("%d-i%d",x,-y);
  else
            printf("%d+i%d",x,+y);
  getch();
}
30. SUM OF THE SERIES 1+2+3+---------+n
void main()
{
  int r;
  clrscr();
  printf("\nEnter the number range: ");
  scanf("%d",&r);
  printf("\nSum of the series is: %d",(r*(r+1))/2);
  getch();
}
Post a Comment