Search the Whole World Here.,.,

Showing posts with label C Language. Show all posts
Showing posts with label C Language. Show all posts

C Program : Factorial Program using Recursion


#include<stdio.h>
long factorial(int n) {  
  
  if (n == 0)  
    return 1;  
  else  
    return(n * factorial(n-1));  
    }  
   
void main(){  
 
     int number;  
     long fact;  

     printf("Enter a number: ");  
     scanf("%d", &number);   
   
     fact = factorial(number);  
     printf("Factorial of %d is %ld\n", number, fact);  
 
     return 0;  
}  





Output:
Enter a number: 6
Factorial of 5 is: 720

C Program : Factorial Program using loop





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

          return 0;  
         }   


Output:
Enter a number: 5
Factorial of 5 is: 120

C Program : Palindrome



Let's see the palindrome program in C. In this c program, we will get an input from the user and check whether number is palindrome or not.




#include  
int main(){    


           int n,r,sum=0,temp;    
           printf("enter the number=");    
           scanf("%d",&n);    
           temp=n;  
  
           while(n>0){    
                   
                  r=n%10;    
                  sum=(sum*10)+r;    
                  n=n/10;    
                 }    

          if(temp==sum)    
          printf("palindrome number ");    
 
          else    
          printf("not palindrome");   

          return 0;  
       }   



Output:
enter the number=151
palindrome  number
enter the number=5621
not palindrome  number

C Program : Prime Number


#include  
int main(){   

 
       int n,i,m=0,flag=0;    
       printf("Enter the number to check prime:");    
       scanf("%d",&n);    
       m=n/2;
    
       for(i=2;i<=m;i++){
    
               if(n%i==0){   
                    printf("Number is not prime");
                    flag=1;    
                    break;    
                     }    
            }   
 
     if(flag==0)    
     printf("Number is prime");     

     return 0;  
    }



Output:
Enter the number to check prime:56
Number is not prime
Enter the number to check prime:23
Number is prime

C Program : Fibonacci Series using Recursion







#include    
void printFibonacci(int n){ 

   
    static int n1=0,n2=1,n3;    
    if(n>0){ 
   
         n3 = n1 + n2;    
         n1 = n2;    
         n2 = n3;    
         printf("%d ",n3);    
         printFibonacci(n-1);    
    }    
}   
 
int main(){    

    int n;    
    printf("Enter the number of elements: ");    
    scanf("%d",&n);    
    printf("Fibonacci Series: ");    
    printf("%d %d ",0,1);    
    printFibonacci(n-2);//n-2 because 2 numbers are already printed   
 
  return 0;  
 }    


Output:
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

C Program : Fibonacci Series without Recursion


#include  
#include  

void main(){    
            int n1=0,n2=1,n3,i,number; 
            clrscr();  
 
            printf("Enter the number of elements:");    
            scanf("%d",&number);    

            printf("\n%d %d",n1,n2);//printing 0 and 1
            for(i=2; i<number; ++i){
                                   
                        n3=n1+n2;    
                        printf(" %d",n3);    
                        n1=n2;    
                        n2=n3;    
                      }  
                        
                        getch();  
               }    




Output:
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

C Program : Addition of two numbers using Scanf Function





#include<stdio.h>
#include<conio.h>

void main(){
                int a,b,sum;
                clrscr();

                printf("Enter 1st no. ");
                scanf("%d",&a);

                printf("Enter 2nd no. ");
                scanf("%d",&b);

                sum = a+b;
                printf("Sum of a & b is  %d",sum);
                   
                         getch();
                 }


Watch this video for more :


C Program : Addition of two numbers





#include<stdio.h>
#include<conio.h>
void main(){

                      int a,b,c;
                      clrscr();

                      a = 10;
                      b = 20;
                      c = a+b;

                                  printf("The Sum of a & b is %d",c);
                                  getch();
                        
                        }



For more you can see videos: 


C - Variables



1. Variable in C Programming is called as container to store the data.

2. Variable name may have different data types to identify the type of value stored.

3. Suppose we declare variable of type integer then it can store only integer values.

4. Variable is considered as one of the building block of C Programming which is also called as identifier.

5. A Variable is a name given to the memory location where the actual data is stored.



Following are the types of variable in C :

  • Local Variables
  • Global Variables


Local Variables

    1. Local Variable is Variable having Local Scope.

      2. Local Variable is accessible only from function or block in which it is declared.

        3. Local variable is given Higher Priority than the Global Variable.



        Global Variables

        1. Global Variable is Variable that is Globally available.

        2. Scope of Global variable is throughout the program [ i.e in all functions including main() ]

        3. Global variable is also visible inside function , provided that it should not be re-declared with same name inside function because "High Priority is given to Local Variable than Global".

        4. Global variable can be accessed from any function.

        Features of C




        Fast and Efficient


        Programs written in  C are efficient and  fast. This is due to its variety of data type and powerful operators.


        Portable

        C is highly portable. This means that programs once written can be run on another machines with little or no modification.

        Function and Rich Libraries

        C program is basically a collection of functions that are supported by C library. We can also create our own functions and add it to the C library.

        Modularity

        Modular programming is a software design technique that increases the extent to which software is composed of separate parts called modules.

        Easy to Extend

        In C, New feature can be added at any time by programmer.

        Case Sensitive

        It is a case sensitive language, that it can differentiate the character is uppercase or lowercase.

        Origin of C





        In 1970 a programmer, Dennis Ritchie , created a new language called C.


        1. The name came about  because it superseded the old programming language he was using : B

        2. C was designed with one goal in mind : writing  operating system.

        3. The language was extremely simple and flexible and soon was used for many different types of programs.

        4. It quickly became one of the most popular programming languages in the world.

        List of C keywords



        Keywords are reserved words which have standard, predefined meaning in C. They cannot be used as program-defined identifiers.

            *Generally all keywords are in lowercase although uppercase of same names            can be used as identifiers.


        List of C keywords are as follows:


        1. auto 
        2. break 
        3. case 
        4. char 
        5. const 
        6. continue
        7. default
        8. do 
        9. double 
        10. else 
        11. enum 
        12. extern 
        13. float 
        14. for 
        15. goto 
        16. if
        17. int 
        18. long 
        19. register 
        20. return 
        21. short 
        22. signed 
        23. sizeof 
        24. static 
        25. struct 
        26. switch 
        27. typedef 
        28. union 
        29. unsigned 
        30. void 
        31. volatile 
        32. while 


        Write C Program to print Hello World





        /*Program to print  a Hello World*/

        //header file
        #include
        #include

        //main function
        int main(){

                        printf("Hello, World!");
                        getch();
                        }





        For more help watch the video...







        These Are The Fastest Growing Programming Languages


        A lot of new programming languages are making their way to the top and JavaScript is holding the top position for the past few years. Seems like the situation is about to change and JavaScript may soon be dethroned by Python. Python is one of the fastest growing languages and by 2019, it would have a clear domination in the programming community.
        This is a prediction made by Stack Overflow, a programming Q&A center. With more than 50 million amateur and professional developers visiting the website, Stack Overflow is one of the qualified companies to make such a survey. Based on the huge increase in the questions from developers on Python and the data they have collected, Stack Overflow made this survey possible.
        Stack Overflow data scientist David Robinson said in a blog post that, “The term ‘fastest-growing’ can be really hard to define precisely, but we make the case that Python has a solid claim to become the fastest-growing major programming language.

          
        Just in the past five years, Python went from least to top six most popular programming languages with a whopping 2.5x increase in the questions and views on the language. Stack Overflow also stated that these statistics are related to developers from countries with “high-income”  and represent the trend in the United States, United Kingdom, Canada, Germany and other such countries.
        The secret behind the growth of Python is its flexibility and versatility. Python is generally used by web and desktop developers, develops/sysadmin and recently even data scientists and machine-learning with Python became popular.
        “It seems like Python is used in every domain — system operations, web development, deployment, scientific modeling, etc etc. There is no other language that is so versatile,” says Jacqueline Kazil, board director of the Python Software Foundation (PSF).








        Structure of C Program

        Structure of C Program



        • Documentation Section
        • Link Section
        • Definition Section
        • Global Declaration


        main()         Function Section
        {
          
            Declaration Expressions

        }

        ----------

        Sub Program Section


        Function 1;
        Function 2;

        Ads by Google