Friday 20 November 2009

C question and answer with explanation









31. SUM OF SQUARES OF THE SERIES 12+22+32+--------+n2
void main()
{
  long int r;
  clrscr();
  printf("\nEnter the range: ");
  scanf("%ld",&r);
  printf("\nSum of the squares of the series is: %ld",((r*(r+1))*(2*r+1))/6);
  getch();
}
32. SUM OF CUBES OF THE SERIES 13+23+33+---------+n3
void main()
{
  int r;
  clrscr();
  printf("\nEnter the number range: ");
  scanf("%d",&r);
  printf

C questions and answer with explanation




1. PERFECT NUMBER.
void main()
{
  int n,i=1,sum=0;
  clrscr();
  printf("\nEnter a number:-");
  scanf("%d",&n);
  while(i  {
            if(n%i==0)
            sum=sum+i;
            i++;
  }
    if(sum==n)
            printf("\nThe no %d is a perfect number",i);
    else
            printf("\nThe no %d is not a perfect number",i);
  getch();
}
2. ARMSTRONG NUMBER.
void main()
{
  int 

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

C questions with answer

Variable naming rule in c programming language:
(q) What will be output of the following program?
void main()
{
char * emp name=”raja”;
printf("%s",emp name);
getch();
}
Output: Compilation error
Explanation:
Error: Invalid variable name. Except underscore there should not be any special character in name of variable event blank space.
(q) What will be output of the following program?
void main()

C questions and answer








(51) What will be output if you will compile and execute the following c code?

struct marks{
int p:3;
int c:3;
int m:2;
};
void main(){
struct marks s={2,-6,5};
printf("%d %d %d",s.p,s.c,s.m);
}

(a) 2 -6 5
(b) 2 -6 1
(c) 2 2 1
(d) Compiler error
(e) None of these






Answer: (c)