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

Tokens or Lexical units in C Programming.

Tokens:


The smallest individual unit in a program is known as a Token or a lexical unit.
Tokens are the basic building blocks of C language which are constructed together to write a C program.
C has the following tokens:
                                  

                    Image result for tokens in c


Keywords:

Keywords are the words that convey a special meaning to the language compiler.These are reserved for special purpose and must not be used as normal identifier names.
Example: new,float,int,char,void,main,etc.

Identifiers:

An identifier is an arbitrarily long sequence of letters and digits.The first character must be a letter; the underscore( _ ) counts as a letter. Upper and lower-case letters are different. All characters are significant.
Example: Some valid identifiers are:
                Myfile, _Ds, File13,etc.

                The following are some invalid identifiers:
                 break   -     reserved keyword
                 29Clct   -    starting with a digit
                 My.file   -    contains special character

Literals ( or Constants):

Literals (often referred to as constants) are data items that never change their values during a program run.

C allows several kind of literals:
1. integer constant
2. floating constant
3. character constant

1. Integer Constants:

An integer constants are whole numbers without any fractional part. It must have at one digit and must not contain any decimal point. It may contain either + or - sign. A number with no sign is assumed to be positive. Commas cannot appear in an integer constant.

 C allows three types of integer constants:
1. Decimal ( base 10 ) : An integer constant consisting of a sequence of digits but it does not begin with digit 0 ( zero ).
For example: 1234,34,etc.
2. Octal ( base 8 ) : It is a constant in which the integer constant begins with digit 0 ( zero ).
For example: 9(in decimal) can be written as 011 (in octal).
3. Hexadecimal ( base 16 ) : It is a constant in which the integer begins with ox or OX.
For example: 11(in decimal) can be written as OXB (in hexadecimal).

2. Character Constants:

An character constants must contain one character and must be enclosed in single quotation marks.
For example: 'C' or 'a'.

C allows you to have certain nongraphic characters in character constants. Nongraphic characters are those characters that cannot be typed directly from keywords e.g., backspace, tabs, carriage return etc.These nongraphic characters can be represented by using escape sequences. 
An escape sequence is represented by a backslash (\) followed by one or more characters. It represents a single character and hence consumes one byte in ASCII representation.
For example: '\b' is used for Backspace, '\n' is used for newline, '\t' is used for horizontal tab, etc.

3. Floating Constants:

Floating constants are also called real constants.
A real constants in fractional form must have at least one digit before a decimal point and at least one digit after the decimal point. It may also have either + or - sign preceding it. A real constant with no sign is assumed to be positive.
For example: 2.0, 17.5, -23.0, -0.00325, etc.
A real constant in exponent form has two types: a mantissa and an exponent. The mantissa must be either an integer or a proper real constant. The mantissa is followed by a letter E or e and the exponent. The exponent must be an integer.
For example: 152E05, 1.52E07, 152E+8, -0.172E-3, etc.

String:


A string literal is a sequence of characters surrounded by double quotes.
For example: "abc", "\ab", etc.

Punctuators (or special symbols):

The following characters are used as punctuators ( also known as separators):      
               [    ]    (   )    {   }   ,   :   ;    *    ....   =   #

Operators:

Operators are tokens that trigger some computation when applied to variables and other objects in an expression. Unary operators are those operators that require one operator to operate upon.
For example: & (Address operator), + (Unary plus), - (Unary minus), ++ (Incremental operator), -- (Decremental operator), etc.
Binary operators are those operators that require two operands to operate upon.
For example: Arithmetic operators ( + , - . * , / , % )
                      Shift operators (<< , >>)
                      Bitwise operators ( & , ^ , | )
                      Logical operators ( && , || )
                      Relational operators (< ,<= ,> ,>= ,== ,!=)
                      Conditional operators (?, :)

The order of precedence for operators is as follows:
             
Image result for order of precedence in c


















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. ...