Monday 8 December 2014

To find the sum of individual digits of a given number

Objective : 1


To find the sum of individual digits of a given number

Sum of the individual digits means adding all the digits of a number Ex: 123 sum of digits is 1+2+3=6


#include<stdio.h>
main()
{
Int n,s,p; clrscr();

printf("enter the vaue for n:\n"); scanf("%d",&n);
s=0;
if(n<0)
printf("The given number is not valid"); else
{
while(n!=0)    /* check the given value =0 or not */
{
p=n%10;
n=n/10;
s=s+p;
}
printf("sum of individual digits is %d",s);
}
getch();
}

Output:


1.Enter the value for n: 333
 Sum of individual digits is 9

2.Enter the value for n: 4733
Sum of individual digits is 17

3. Enter the value for n: -111
The given number is not valid


Conclusion : The program is error free

VIVA QUESTIONS:

1) What is the mean of sum of the individual digits?
Ans: Sum of the individual digits means adding each digit in a number

2) What is positive integer?
Ans: if the integer value is greater than zero then it is called positive integer

3) Define preprocessor ?
Ans: Before compiling a process called preprocessing is done on the source code by a program called the preprocessor.





Monday 17 November 2014

c program on quadratic equation by using switch statement


#include<stdio.h>
#include<conio.h>
void main()
{
  float a,b,c,r1,r2,rp,ip,disc;
  int k;
  clrscr();
  printf("enter values of abc\n");
  scanf("%f%f%f", &a,&b,&c);
  if(a==0)
  {
  printf("value should not be zero\n");
  getch();
  exit(0);

}
disc=b*b-4*a*c;
if(disc>0)
  k=1;
else if(disc==0)
  k=0;
else
  k=-1;
switch(k)
{
  case 1:{
               printf("roots are real and unequal\n");
               r1=-b+sqrt(disc)/(2*a);
               r2=-b-sqrt(disc)/(2*a);
               printf("root 1=%.2f\n",r1);
               printf("root 2=%.2f\n,r2);
               break;
             }
   case 0:{
             printf(roots are real and equal\n");
             r1=r2=-b/(2*a);
             printf("root 1=%.2f\n",r1);
               printf("root 2=%.2f\n,r2);
               break;
             }
case -1:{
printf(roots are complex\n");
             rp=-b/(2*a);
            ip=sqrt(-disc)/(2*a);

             printf("root 1=%.2f+i*%.2f\n",rp,ip);
             printf("root 1=%.2f-i*%.2f\n",rp,ip);
               break;
         }
 getch();
}

c programs