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

Sunday, December 15, 2019

Write a program to find gcd of two numbers using recursion.

About: 
The process in which a function calls itself is called recursion and the corresponding function is called as recursive function.
For example: In below program, gcd() is used for calculating gcd of n1 and n2 and we are calling the same function again and again until the value of n2 is equal to 0.

Program:
#include <stdio.h>
int gcd(int n1, int n2) {
if (n2 != 0)
return gcd(n2, n1 % n2);
else
return n1;
}
void main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, gcd(n1, n2));
}

Write a program to find gcd of two numbers.

About:
The HCF or GCD of two integers is the largest integer that can exactly divide both numbers (without a remainder).
For example:81 and 153
The gcd(81,153)=9.

Program:
#include<stdio.h>
void main()
{
int n1,n2,gcd;
printf("Enter any two no:");
scanf("%d%d",&n1,&n2);
for(int i=1;i<=n1&&i<=n2;++i){
if(n1%i==0&&n2%i==0)
gcd=i;
}
printf("The gcd of %d and %d=%d",n1,n2,gcd);
}

Write a program to check Armstrong Number or not.

About:
A positive integer is called an Armstrong number (of order n) if
abcd...=an+bn+cn+dn+.....
In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example, 153 is an Armstrong number because
153=1*1*1+5*5*5+3*3*3

Program:
#include<stdio.h>
void main()
{
int a,n,s=0,l;
printf("Enter any three-digit no:");
scanf("%d",&n);
int b=n;
while(n){
a=n%10;
s=s+(a*a*a);
n=n/10;
}
if(b==s)
printf("Given number is Armstrong");
else
printf("Not Armstrong number");
}

Write a program for Fibonacci series.

For example:0 1 1 2 3 5 8 13......

Program:
#include <stdio.h>
void main()
{
    int s=0,n,a,b;
    a=0,b=1;
    printf("enter any no:");
    scanf("%d",&n);
    printf("Fibonacci series:\n");
    printf("%d",a);
    printf("\t%d",b);
    printf("\t");
    for(int i=0;i<n;++i){
        s=a+b;
        a=b;
        b=s;
        printf("%d",s);
        printf("\t");
    }
}

Monday, August 26, 2019

WAP to display pyramids of numbers.

//To print:
          1
       2   2
   3    3    3
4    4    4    4

#include<stdio.h>
void main()
{
    int i,j,k,n,t=1;
    printf("Enter any number:");
    scanf("%d", &n);
    for(i=1 ; i<=n ; i++)
    {
         for(j=1 ;j<=n-i ; j++)
         {
               printf("\t");
         }
         for(k=1 ; k<=2*i-1 ; k++)
         {
              if(k%2!=0)
              {
                  printf("%d",t);
               }
              else
              {
                  printf("\t");
               }
          }
          printf("\n");
          t=t+1;
     }
     getch();
}

Output:
Enter any number:4
          1
       2    2
    3    3    3
4    4    4    4

WAP to display triangle of 1 and 0.

//Program to display:
1
1  0
1  0  1
1  0  1  0
1  0  1  0  1

#include<stdio.h>
void main()
{
    clrscr();
    int i,j,n,k=0,t=1;
    printf("Enter any number:");
    scanf("%d",&n);
    for( i=1; i<=n ; i++)
    {
         for(j=1 ; j<=i ; j++)
         {
              if(j%2 != 0)
                  printf("%d", t);
              else
                  printf("%d", k);
          }
          printf("\n");
    }
    getch();
}

Output:
Enter any number:5
1
1  0
1  0  1
1  0  1  0
1  0  1  0  1

   

WAP ton display Floyd's Triangle.

//Program
1
2  3
4  5  6
7  8   9  10

#include<stdio.h>
void main()
{
    clrscr();
    int i,j,n,a;
    a=1;
    printf("Enter any number:");
    scanf("%d", &n);
    for( i=1 ; i<=n ; i++)
    {
         for( j=1 ; j<=i ; j++)
         {
              printf("%d",a);
              printf("  ");
              a=a+1;
         }
         printf("\n");
    }
    getch();
}

Output:
Enter any number:4
1
2  3
4  5  6
7  8  9  10
    

WAP to display triangle.

//Program to display
A
A  B
A  B  C
A  B  C  D

#include<stdio.h>
void main()
{
    char s;
    int a=65, n, i, j;
    clrscr();
    printf(" Enter any number:");
    scanf("%d", &n);
    for( i=0; i< n ; i++ )\
    {
         for( j=0; j<=i ; j++)
         {
              s=a+j;
              printf("%c",s,"\t");
          }
         printf("\n");
    }
getch();
}

Output:
Enter any number:4
A
A  B 
A  B  C
A  B  C  D

WAP to convert a given number of days into years, weeks, days.

//Program
#include<stdio.h>
void main()

    clrscr();
    int tdays, years, weeks, days, num1;
    prinf("Enter total number of days:");
    scanf("%d",&tdays);
    years = tdays / 365;
    num1 = tdays % 365;
    weeks = num1 / 7;
    days = num1 % 7;
    printf("\n Years = %d ", years);
    printf("\n Weeks =%d ", weeks);
    printf('\n Days=%d ", days);
    getch( );
}

Output:
Enter total number of days:365
Year = 1
Weeks = 52
Days = 365

Friday, August 16, 2019

WAP to check whether the year is leap or not.

// Program
//The year which is divisible by 400,100 and 4 is called leap year.

#include<stdio.h>
void main()
{
     int a,year;
     printf("Enter any year:");
     scanf("%d",&year);
     if((year%400)==0)
     {
            printf("Leap year");
      }
     else if((year%100)==0)
     {
            printf("Leap year");
      }
     else if((year%4)==0)
     {
            printf("Leap year");
     }
     else
            printf("Not leap year");
     getch();
}

Sample Input/Output:

Enter any year:2004
Leap year

WAP to swap two numbers using third variable.

//Program

#include<stdio.h>
void main()
{
      int a,b,temp;
      clrscr();
      printf("Enter any two numbers:");
      scanf("%d %d",&a,&b);
      prinft("Before swapping of numbers: a=%d and b=%d",a,b);
      temp=a;
      a=b;
      b=temp;
      printf("After swapping of numbers: a=%d and b=%d",a,b);
      getch();
}


Sample Input/Output:

Enter any two numbers:10
20
Before swapping of numbers a=10 and b=20
After swapping of numbers a=20 and b=10

WAP to check whether the given number is palindrome or not.

// Program

#include<stdio.h>
void main()
{
     int a,n,r,d;
     r=0;
     clrscr();
     printf("Enter any number:");
     scanf("%d",&n);
     while(n)
     {
          a=n;
          while(a>0)
          {
               d=a%10;
               r=(r*10)+d;
               a=a/10;
           }
      if(r==n)
           printf("The number is palindrome");
     else
           printf("The number is not palindrome");
     }
     getch();
}

Sample Input/Output:

Enter any number:1221
The number is palindrome

WAP to find a reverse of number.

// Program

#include<stdio.h>
void main()
{
    int a,b,n;
    clrscr();
    printf("Enter any number:");
    scanf("%d",&n);
    b=0;
    while(n!=0)
    {
          a=n%10;
          b=(b*10)+a;
          n=n/10;
     }
    printf("Reverse of a number=%d",b);
    getch();
}

Sample Input/Output:

Enter any number:1234
Reverse of a number=4321

WAP to find the sum of digits of entered number.

//Program

#include<stdio.h>
void main()
{
     int n,i,r;
     int s=0;
     clrscr();
     printf("Enter any number:");
     scanf("%d",&n);
     while(n!=0)
     {
          r=n%10;
          s=s+r;
          n=n/10;
     }
     printf("Sum of digits=%d",s);
     getch();
}

Sample Input/Output:

Enter any number:1234
Sum of digits=10

WAP to check whether the entered character is vowel or not using switch case statement.

// C program

#include<stdio.h>
void main()
{
     char ch;
     clrscr();
     printf("Enter any character:");
     scanf("%c",&ch);
     switch(ch)
     {
          case 'a' || 'A':
          case 'e' || 'E':
          case 'i' || 'I':
          case 'o' || 'O':
          case 'u' || 'U':
                                printf("The character is a vowel");
                                break;
          default: printf("The character is not vowel");
                       break;
      }
      getch();
}

Sample Input/Output:

Enter any character: A
The character is a vowel

WAP to find factorial of a number.

// C program 

#include<stdio.h>
void main()
{
     int a,f=1;
     clrscr();
     printf("Enter a number:");
     scanf("%d",&a);
     for(int i=1,i<=a;++i);
     {
              f=f*i;
      }
      printf("Factorial of a number is %d",f);
      getch();
}

Sample Input/Output:

Enter a number: 5
Factorial of a number is 120
     

WAP to find the largest of three number's.

// C program

#include<stdio.h>
void main()
{
    int a,b,c;
    clrscr();
    printf("Enter any three numbers:");
    scanf("%d %d %d",&a,&b,&c);
    if((a>b)&&(a>c))
        printf("%d",a," is a largest number");
   elseif((b>a)&&(b>c))
        printf("%d",b," is a largest number");
   else
        printf("%d",c," is a largest number");
  getch();
}

Sample Input/Output:

Enter any three numbers:10
20
30
30 is a largest number

WAP for finding a number is even or odd

// C program

#include<stdio.h>
void main()
{
    int n;
    clrscr();
    printf("Enter the number:");
    scanf("%d",&n);
    if ((n%2)==0)
         printf("\n The number is even.");
    else
         printf("\nThe number is odd.");
    getch();
}

Sample Input/Output:

Enter the number:8
The number is even.

WAP for calculation of perimeter and area of a rectangle.

// C program

#include<stdio.h>
int main()
{
    int l,b,a,p;
    printf("Enter the value of length and breadth:");
    scanf("%d %d",&l,&b);
    p=2*(l+b);
    a=l*b;
    printf("\nThe perimeter of a rectangle=%d",p);
    printf("\nThe area of a rectangle=%d",a);
    return 0;
}

Sample Input/Output:

Enter the value of length and breadth:10
5

The perimeter of a rectangle=30
The area of a rectangle=50

 
    

WAP for addition,subtraction,multiplication and division of two numbers.

// C program to add,subtract,multiply and divide.

#include<stdio.h>
int main()
{
    int a,b;
    printf("Enter two numbers:");
    scanf("%d",&a);
    scanf("%d",&b);
    printf("Addition=%d",a+b);
    printf("\nSubtraction=%d",a-b);
    printf("\nMultiplication=%d",a*b);
    printf("\nDivision=%d",a/b);
    return 0;
}

Sample Input/Output:

Enter two numbers:20
20
Addition=40
Subtraction=0
Multiplication=400
Division=1

Write a program to find gcd of two numbers using recursion.

About:  The process in which a function calls itself is called recursion and the corresponding function is called as recursive function. ...