Computer Programming And Utilization(CPU) - Jan - 2011

Question 3

(a) Write a C program to determine a given number is ‘odd’ or ‘even’ and print the following message NUMBER IS EVEN Or NUMBER IS ODD (i) Without using else option. (ii) With else option.


#include<stdio.h>
#include<conio.h>
main()
{
 int number;
 clrscr();
 printf("Enter Number : ");
 scanf("%d",&number);
 /* Without else option */
 switch(number%2)
 {
  case 0:
   printf("\nNUMBER IS EVEN");
   break;
  default:
   printf("\nNUMBER IS ODD");
   break;
 }
 /* With else option */
 if( (number%2) == 0)
  printf("\nNUMBER IS EVEN");
 else
  printf("\nNUMBER IS ODD");
 getch();
}
    

(b) Explain the break and continue statements using suitable example.


#include<stdio.h>
#include<conio.h>
main()
{
 int number,i,k=0;
 clrscr();
 printf("Enter Number : ");
 scanf("%d",&number);
 printf("Sum of first %d EVEN Numbers :",number);
 for(i=0;;i++)
 {
  if( (i%2) != 0)
   continue;
  k+=i;
  if(i == number)
   break; 
 }
 printf("%d",k);
 getch();
}
    

(c) Explain getchar() and gets() functions with suitable example.


#include <stdio.h>
void main(void)
{
    int c;
    char string[80];
    clrscr();
    while ((c = getchar()) != '\n')
       printf("%c", c);
    printf("\n\n\nInput a string:");
    gets(string);
    printf("\nThe string input was: %s\n", string);
    getch();
}
    

(a) Write a C program to evaluate the square root for five numbers using the goto statement.


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
 int number,i=1;
 clrscr();
 START:
 printf("\n\nEnter Number : ");
 scanf("%d",&number);
 printf("\nRoot is : %f",sqrt(number));
 if(i != 5)
 {
  i++; goto START;
 }
 getch();
}
    

(b) Write the basic differences between the while loop and do-while loop with suitable example.


#include<stdio.h>
#include<conio.h>
main()
{
 int number=0;
 clrscr();
 do
 {
  printf("\nDo loop execute atleast one time whether condition is true or false.");
 }
 while(number != 0);
 while(number != 0)
 {
  printf("This statement doesn't execute..");
 }
 printf("\nWhile loop first check condition and then execute.");
 getch();
}
    
c) Explain putchar() and puts() functions with suitable example.


#include <stdio.h>
void main(void)
{
    char i, j;
    char string[] = "\n\n\nThis is an example output string by puts() functionn\n";
    clrscr();
    putchar('*');
    for (i=0; i<10; i++)
        putchar('_');
    putchar('*');
    putchar('\n');
    puts(string);
    getch();
}
    

Question 4

(a) Write a C program in which a 5 digit positive integer is entered through keyboard; write a function to calculate the sum of digits of the 5 digit number with using recursion.


#include<stdio.h>
#include<conio.h>
main()
{
 int number,i=1;
 clrscr();
 printf("Enter 5 digit Number : ");
 scanf("%d",&number);
 printf("%d",recursiveFunction(number));
 getch();
}
int recursiveFunction(int number)
{
 int reminder;
 reminder = number % 10;
 number = number / 10;
 if(number == 0)
 {
  return reminder;
 }
 else
 {
  return (reminder + recursiveFunction(number));
 }
}
    

Question 5

(b) What is friend function? Write simple C++ program which shows that friend function as a bridge between classes.


#include <iostream.h>
class friendClassA
{
  int X,Y;
 public:
  friend int addition(friendClassA fca);
  void set( int a, int b);
};
void friendClassA :: set( int a, int b)
{
 X = a;
 Y = b;
}
int addition(friendClassA fca)
{
 return fca.X + fca.Y;
}
main()
{
 friendClassA fca;
 fca.set(15,20);
 cout <<  "The Addition of X and Y is : "  << addition(fca);
}
    

No comments:

Post a Comment