TAB

Showing posts with label FAQ Programs. Show all posts
Showing posts with label FAQ Programs. Show all posts

C-Program For Addition_Of_Digits_Number



C-Code to sum the digits of a number

#include<stdio.h>
#include<conio.h>
void main()
{
  int n,sum=0,i;
  printf("Enter the number: ");
  scanf("%d",&n);
  while(n)
  {
      i=num%10;
      num=num/10;
      sum=sum+i;
  }
  printf("Sum of digits of %d is %d",n,sum);
  getch();
}

Output:
Enter the number: 567
Sum of digits of 567 is 18
___________________________________________________________

Sum of digits of a number

#include<stdio.h>
#include<conio.h>
void main()
{
  int n,sum=0,i;
  printf("Enter the number: ");
  scanf("%d",&n);

  for(;n!=0;n=n/10)
 {
      i=n%10;
      sum=sum+i;
  }
  printf("Sum of digits of  %d is %d",n,sum);
  getch();
}

Output:
Enter a number: 567
Sum of digits of number:  18
___________________________________________________________

Addition of digits of a number to a single digit


#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum,d;
sum=0;
printf("Enter the number: ");
scanf("%d",n);
while((n>9) || (n==10))
{
while(n>0)
{
d=n%10;
n=n/10;
s=s+d;
}
n=sum;
}
printf("Sum of digits to a single number of a given number %d is %d",n,sum);
getch();
}

Output:
Enter the number: 567
Sum of digits to a single number of a given number 567 is 9

C-Programs For Perfect Number

Definition of perfect number or What is perfect number? 

A perfect number is a positive integer that is equal to the sum of its proper positive divisors, i.e., the sum of its positive divisors excluding the number itself. It is also known as aliquot sum. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors including itself. 

For example 6 is perfect number since divisor of 6 are 1, 2 and 3.  Sum of its divisor is
1 + 2+ 3 =6

C-Program For String Concatenation


#include<stdio.h>
void stringConcat(char[],char[]);
 
int main(){
    char str1[100],str2[100];
    int compare;
    printf("Enter first string: ");
    scanf("%s",str1);
    printf("Enter second string: ");
    scanf("%s",str2);
    stringConcat(str1,str2);
    printf("Concatenated String: %s",str1);
    return 0;
}
void stringConcat(char str1[],char str2[]){
    int i=0,j=0;
   
   
    while(str1[i]!='\0')
     {
         i++;
    }
    while(str2[j]!='\0')
     {
         str1[i] = str2[j];   
         i++;
         j++;
    }
    str1[i] = '\0';
}
output:
Enter first string: FresherStation
Enter second string: .blogspot.in
String after concatenation: FresherStation.blogspot.in

C program to calculate roots of a quadratic equation

C program to calculate roots of a quadratic equation
 
Code 1: C program to calculate roots of a quadratic equation

#include<stdio.h>
#include<math.h>
int main(){
  float a,b,c;
  float d,r1,r2;  
 
  printf("Enter a, b and c of quadratic equation: ");
  scanf("%f%f%f",&a,&b,&c);
   
  d = b * b - 4 * a * c;
  
  if(d < 0){
    printf("Roots are complex number.\n");
    printf("Roots of quadratic equation are: ");
    printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
    printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));
  
    return 0; 
  }
  else if(d==0){
   printf("Both roots are equal\n");
   r1 = -b /(2* a);
   printf("Root of quadratic equation is: %.3f ",r1);
   return 0;
  }
  else{
   printf("Roots are real numbers.\n");
  
   r1 = ( -b + sqrt(d)) / (2* a);
   r2 = ( -b - sqrt(d)) / (2* a);
   printf("Roots of quadratic equation are: %.3f , %.3f",r1,r2);
  }
  return 0;
}
output:
Enter a, b and c of quadratic equation: 2 4 1
Roots are real numbers.
Roots of quadratic equation are: -0.293, -1.707
____________________________________________________________
Code2: C-Program to find a b and c in a quadratic equation
#include<stdio.h>
#include<math.h>
int main(){
  float a,b,c;
  float d,r1,r2;  
  printf("Enter quadratic equation in the format ax^2+bx+c: ");
  scanf("%fx^2%fx%f",&a,&b,&c);
   
  d = b * b - 4 * a * c;
  
  if(d < 0){
    printf("Roots are complex number\n");
   
    return 0;
  }
 
   root1 = ( -b + sqrt(d)) / (2* a);
   root2 = ( -b - sqrt(d)) / (2* a);
   printf("Roots of quadratic equation are: %.3f , %.3f",r1,r2);
  return 0;
}
output:
Enter quadratic equation in the format ax^2+bx+c: 2x^2+4x+-1
Roots of quadratic equation are: 0.000, -2.000

C-Program To Find Armstrong Number

C-Program To Find Armstrong Number

Definition of Armstrong number:
Definition for c programming point of view:

Those numbers which sum of the cube of its digits is equal to that number are known as Armstrong numbers. For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
Other Armstrong numbers: 370,371,407 etc.


#include<stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("\nEnter a number:-");
scanf("%d",&num);
temp=num;
while(num!=0)
{
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}

if(sum==temp)
printf("\nThe number %d is an armstrong number",temp);
else
printf("\nThe number %d is not an armstrong number",temp);
return 0;
}


C-Program To Reverse A String


C-Program To Reverse A String
Code 1:String reverse in c without using strrev

#include<stdio.h>
int main(){
    char str[50];
    char reverse[50];
    int i=-1,j=0;
    printf("Enter a string : ");
    scanf("%s",str);
   
    while(str[++i]!='\0');
    while(i>=0)
     reverse[j++] = str[--i];
    reverse[j]='\0';
  
    printf("Reverse of string is : %s",reverse);
  
    return 0;
}
output:
Enter a string :Fresherstation.blogspot.in
Reversed string is :ni.topsgolb.noitatsrehserf
 
Code 2:Reverse a string in c without using temp

#include<stdio.h>
#include<string.h>
 
int main(){
    char str[50];
    char *reverse;
    printf("Enter a string ");
    scanf("%s",str);
    reverse = strrev(str);
   
    printf("Reversed string is %s",reverse);
   
    return 0;
}
output:
Enter a string :Fresherstation.blogspot.in
Reversed string is :ni.topsgolb.noitatsrehserf
 
Code 3:Reverse a string in c using pointers

#include<stdio.h>
int main(){
    char str[50];
    char reverse[50];
    char *sptr = str;
    char *rptr = reverse;
    int i=-1;
    printf("Enter a string ");
    scanf("%s",str);
   
    while(*sptr){
     sptr++;
     i++;
    }
    while(i>=0){
     sptr--;
     *rptr = *sptr;
     rptr++;
     --i;
    }
    *rptr='\0';
  
    printf("Reversed string is : %s",reverse);
  
    return 0;
}
output:
Enter a string :Fresherstation.blogspot.in
Reversed string is :ni.topsgolb.noitatsrehserf

C-Program To Check For Palindrome

C-Program To Check For Palindrome

Code 1:C program to find whether a number is palindrome or not

#include<stdio.h>
int main()
{
    int num,r,sum=0,temp;
    printf("Enter a number: ");
    scanf("%d",&num);
    temp=num;
    while(num)
    {
         r=num%10;
         num=num/10;
         sum=sum*10+r;
    }
    if(temp==sum)
         printf("It is a palindrome");
    else
         printf("It is not a palindrome");
    return 0;
}

output:
Enter a number: 191
  It is a palindrome
____________________________________________________________
 
Code 2:C-Program to check for palindrome using for loop

#include<stdio.h>
int main()
{
    int num,v,sum=0,temp;
    printf("Enter a number: ");
    scanf("%d",&num);
   
    for(temp=num;num!=0;num=num/10)
    {
         v=num%10;
         sum=sum*10+v;
    }
    if(temp==sum)
         printf("IT is a palindrome");
    else
         printf("IT is not a palindrome");
    return 0;
}
output:
Enter a number: 191
IT is a palindrome

____________________________________________________________
 
Code 3: C program to find palindrome numbers in given Range

#include<stdio.h>
int main()
{
    int num,v,sum,temp;
    int min,max;
    printf("Enter the minimum range: ");
    scanf("%d",&min);
    printf("Enter the maximum range: ");
    scanf("%d",&max);
    printf("Palindrome numbers in given range are: ");
     
     for(num=min;num<=max;num++)
       {
         temp=num;
         sum=0;
         while(temp){
             v=temp%10;
             temp=temp/10;
             sum=sum*10+v;
         }
         if(num==sum)
             printf("%d ",num);
    }
    return 0;
}
output:
Enter the minimum range:1
Enter the maximum range:20
Palindrome numbers in given range are: 1 2 3 4 5 6 7 8 9 11

____________________________________________________________

Code 4:C-Program to check for palindrome using using recursion

#include<stdio.h>
int checkPalindrome(int);

int main()
{
    int num,sum;
    printf("Enter a number: ");
    scanf("%d",&num);
    sum = Palin(num);
    if(num==sum)
         printf("IT is a palindrome");
    else
    printf("IT is not a palindrome");
    return 0;
}
int Palin(int num)
{
    static int sum=0,v;
    if(num!=0){
         v=num%10;
         sum=sum*10+v;
         Palin(num/10);
    }
    return sum;
}
output:
Enter a number:192
IT is not a palindrome

Related Posts

Related Posts Plugin for WordPress, Blogger...