PK SOLVED

  • Home
  • Request solution
  • About

17 Jan 2017

C program to change case of string

 C     No comments   


In this post, you learn how to change the case of string. If you entered "A", the program will change it's case to "a" and vise-versa. Refer following examples:

#Example: Uppercase to Lowercase (strlwr)

  
  #include <stdio.h>
  #include <string.h>
  void main()
  {
     char inpString[1000];
     char lwrString[1000];
     
     printf("Enter a string to convert to lower case:\n");
     gets(inpString);
     lwrString = strlwr(inpString);
 
     printf("Your string in lower case: \"%s\"\n",lwrString);
 
  }

Output

  Enter a string to convert to lower case:
  HELLO
  Your string in lower case:
  hello

#Example: Lowercase to Uppercase (strupr)

  #include <stdio.h>
  #include <string.h>
  void main()
  {
     char inpString[1000];
     char uprString[1000];
     
     printf("Enter a string to convert to upper case:\n");
     gets(inpString);
     uprString = strlwr(inpString);
 
     printf("Your string in upper case: \"%s\"\n",uprString);
 
  }

Output

  Enter a string to convert to upper case:
  hello
  Your string in upper case:
  HELLO
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

13 Jan 2017

C program to implement structure

 C     No comments   


Structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record. Structure helps to construct a complex data type in more meaningful way. It is somewhat similar to an Array. The only difference is that array is used to store collection of similar datatypes while structure can store collection of any type of data.
Struct keyword is used create structure in C programming.

Structure is used to represent a record. Suppose you want to store record of Student which consists of student name, address, roll number and age. You can define a structure to hold this information.

Refer following example to understand working and use of structure.

#Example:

  #include <stdio.h>
  struct student
  {
     char name[50];
     char add[50];
     int roll;
     int age;
  } s;

  void main()
  {
     printf("Enter information:\n\n");

     printf("Enter name: ");
     scanf("%s", s.name);

     printf("Enter address: ");
     scanf("%s", s.add);

     printf("Enter roll number: ");
     scanf("%d", &s.roll);

     printf("Enter age: ");
     scanf("%d", &s.age);


     printf("\n\nDisplaying Information:\n\n");

     printf("Name: ");
     puts(s.name);

     printf("Address: ");
     puts(s.add);

     printf("Roll number: %d\n",s.roll);

    printf("Age: %d\n", s.age);

  }

Output

  Enter information:
  Enter name: Pankaj
  Enter address: Nanded
  Enter roll number: 22
  Enter age: 22
  Displaying Information:
  Name: Pankaj
  Address: Nanded
  Roll number: 22
  Age: 22
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

10 Jan 2017

C program to check whether it is positive number or not

 C     No comments   

C program to check whether it is positive number or not:



This program takes a number from the user and checks whether that number is either positive or negative or zero.

#Example

  #include <stdio.h>
  void main()
  {
     double number;

     printf("Enter a number: ");
     scanf("%lf", &number);

     // true if number is less than 0
     if (number < 0.0)
        printf("You entered a negative number.");

     // true if number is greater than 0
     else if ( number > 0.0)
        printf("You entered a positive number.");

     // if both test expression is evaluated to false
     else
        printf("You entered 0.");
  }

Output

   Enter a number: 5
   You entered a positive number.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

C program to check leap year

 C     No comments   

C program to check leap year:


A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.

#Example

  #include <stdio.h>
  void main()
  {
     int year;

     printf("Enter a year: ");
     scanf("%d",&year);

     if(year % 4 == 0)
     {
        if( year % 100 == 0)
        {
            // year is divisible by 400, hence the year is a leap year
            if ( year % 400 == 0)
                printf("%d is a leap year.", year);
            else
                printf("%d is not a leap year.", year);
        }
        else
            printf("%d is a leap year.", year );
     }
     else
        printf("%d is not a leap year.", year);
  }

Output

   Enter a year: 2012
   2012 is a leap year.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

C program to find whether it is prime number or not

 C     No comments   

C program to find whether it is prime number or not:



Prime number is the number which can only divided by one (1) and itself.

For Example: 2, 3, 5, 7, 11 are some prime numbers. These numbers can only be divided by 1 or itself.

#Example

  #include <stdio.h>
  void main()
  {
     int num, reminder;
     
     printf("Enter a positive number: ");
     scanf("%d",&num);
     
     //stores reminder in reminder var.
     reminder = num % 2;

     if(remonder == 0)
     {
        printf("%d is a prime number.",num);
     }
     else
     {
        printf("%d is not a prime number.",num);
     }
  } 

Output

  Enter a positive number: 5
  5 is a prime number.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

C program to print Fibonacci series

 C     No comments   

C program to print fibonacci series:


The Fibonacci sequence is a series where the next term is the sum of pervious two terms. The first two terms of the Fibonacci sequence is 0 followed by 1.
The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21

 #Example

  #include <stdio.h>
  void main()
  {
     int i, n, t1 = 0, t2 = 1, nextTerm = 0;

     printf("Enter the number of terms: ");
     scanf("%d", &n);

     printf("Fibonacci Series: ");

     for (i = 1; i <= n; ++i)
     {
        // Prints the first two terms.
        if(i == 1)
        {
            printf("%d, ", t1);
            continue;
        }
        if(i == 2)
        {
            printf("%d, ", t2);
            continue;
        }
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
        printf("%d, ", nextTerm);
     }
  }

Output:

  
  Enter the number of terms: 10
  Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

C program to check whether it is armstrong number or not

 C     No comments   

C program to check whether it is armstrong number or not:


Armstrong number is the number which is equal to the sum of cube of every element of it.

For Example: number - 371

3 * 3 * 3 = 27
7 * 7 * 7 = 343
1 * 1 * 1 = 1
Sum = 371

#Example

  #include <stdio.h>
  int power(int, int);
  void main()
  {
    int n, sum = 0, temp, remainder, digits = 0;
 
    printf("Enter a number: ");
    scanf("%d", &n);
 
    temp = n;
    // Count number of digits
    while (temp != 0) 
    {
      digits++;
      temp = temp/10;
    }
 
    temp = n;
 
    while (temp != 0) 
    {
      remainder = temp%10;
      sum = sum + power(remainder, digits);
      temp = temp/10;
    }
 
    if (n == sum)
      printf("%d is an Armstrong number.\n", n);
    else
      printf("%d is not an Armstrong number.\n", n);
  }
 
  int power(int n, int r) 
  {
     int c, p = 1;
 
     for (c = 1; c <= r; c++) 
       p = p*n;
 
     return p;   
  }

Output:

  Enter a number: 371
  371 is an Armstrong number.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

7 Jan 2017

C program to swap two number without temporary variable

 C     No comments   

C program to swap two numbers #without temporary variable:


Swapping of numbers can be done by two way. To see the second way click here.

Here we will swap two numbers without using any other variable. We will be using mathematical technique to swap numbers.

Note: Input value here is considered as integer only.

#Example:


  #include <stdio.h>
  void main()
  {
     int firNum, secNum;
     
     printf("Enter first Number: ");
     scanf("%d",&firNum);
     printf("\nEnter second Number: ");
     scanf("%d",&secNum)

     firNum = firNum + secNum;
     secNum = firNum - secNum;
     firNum = firNum - secNum;

    printf("\nAfter swapping, first Number: %d",firNum);
    printf("\nAfter swapping, second Number: %d",secNum);
 }

Output:

 Enter first Number: 28
 Enter second Number: 30
 After swapping, first Number: 30
 After swapping, second Number: 28

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

C program to swap two numbers using temporary variable

 C     No comments   

C program to swap two numbers using #temporary variable:


Swapping of numbers can be done by two way. To see the second way click here.

Here we will swap two numbers  using other variable i.e. temporary variable.

Note: Input value here is considered as integer only.

#Example:


  #include <stdio.h>
  void main()
  {
     int firNum, secNum, tempVar;
     
     printf("Enter first Number: ");
     scanf("%d",&firNum);
     printf("\nEnter second Number: ");
     scanf("%d",&secNum)

     tempVar = firNum;
     firNum = secNum;
     secNum = tempVar;

    printf("\nAfter swapping, first Number: %d",firNum);
    printf("\nAfter swapping, second Number: %d",secNum);
 }

Output:

 Enter first Number: 28
 Enter second Number: 30
 After swapping, first Number: 30
 After swapping, second Number: 28

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

C program to check whether entered character is Vowel or consonant

 C     No comments   

C program to check whether entered character is Vowel or consonant:


In this example we will  check whether the character entered by user is Vowel or Consonant by using If...Else statement.

There are 5 Vowels in English alphabet i.e. A E I O U except these all are Consonants.

Note: Inpute value here is considered as character only.

#Example

  #include <stdio.h>
  void main()
  {
    char alpVal;
    printf("Enter a character: ");

    scanf("%c",&alpVal);

    if(alpVal == "a" || alpVal == "e" || alpVal == "i" || alpVal == "o" || alpVal == "u")
    {
       printf("%c is vowel character.",alpVal);
    }
    else if("alpVal == "A" || alpVal == "E" || alpVal == "I" || alpVal == "O" || alpVal == "U")
    {
       printf("%c is vowel character.",alpVal);
    }
    else
    {
       printf("%c is consonant character.",alpVal);
    }
  } 

Output:

Enter a character: A 

A is vowel character.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

C program to print ASCII value of character

 C     No comments   

C program to print ASCII value of character:


In computers a character holds a integer value for it instead of the character, i.e. called ASCII value.

For Example: ASCII Value of character "A" is 65.

Like wise the number between  0 to 127  is used for every character in computer.

Here we will see the C program which will return the ASCII code of any character that we will enter.

#Example:

  #include <stdio.h>
  void main()
  {
     char ascVal;
     printf("Enter a character: ");
     scanf("%c",&ascVal);

     //%c prints actual character 
     //%d prints integer value of the character i.e. ASCII value of character.
     printf("ASCII value for character %c is %d.",ascVal,ascVal);
  }

Output:

  Enter a character: A
  ASCII value for character A is 65.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

6 Jan 2017

C program to print palindrome number

 C     No comments   

C program to print palindrome whether it is palindrome number or not:


Palindrome number is an integer which reverse is equal to the original of it.

C Program:


  
  #include <stdio.h>
  void main()
  {
    int n, reversedInteger = 0, remainder, originalInteger;

    printf("Enter a number: ");
    scanf("%d", &n);

    originalInteger = n;

    // reversed integer is stored in variable 
    while( n!=0 )
    {
        remainder = n%10;
        reversedInteger = reversedInteger*10 + remainder;
        n /= 10;
    }

    // palindrome if orignalInteger and reversedInteger are equal
    if (originalInteger == reversedInteger)
        printf("%d is a palindrome number.", originalInteger);
    else
        printf("%d is not a palindrome number.", originalInteger);
    
  }

Output

Enter a number: 53235
53235 is a palindrome number.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

3 Jan 2017

C program to print even & odd number

 C     No comments   


Even: 

Any integer that can be divided exactly by 2 is an even number.
The last digit is 0, 2, 4, 6 or 8
Example: −24, 0, 6 and 38 are all even numbers

Odd:

Any integer that cannot be divided exactly by 2 is an odd number.
The last digit is 1, 3, 5, 7 or 9
Example: −3, 1, 7 and 35 are all odd numbers
So, now lets see a C-Program to print a even and odd numbers:

C-Program to print Even-Odd Number:

 
 #include <stdio.h> 
 #include <conio.h> 
 void main()
 {
    int num = 0;
    
    printf("Enter a number: ");
    scanf("%d",$num);
    
    if(num % 2 == 0)
        printf("%d is even.", num);
    else
        printf("%d is odd.", num);
 }

Output:

   Enter a number: 8
   8 is even.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Home

Contact Form

Name

Email *

Message *

About me

Hi, this is Pankaj Takale, a programming lover. This blog created to help programmers who are getting stuffs while doing their best.

Follow me here to be in touch

Popular Posts

  • C program to find whether it is prime number or not
    C program to find whether it is prime number or not: Prime number is the number which can only divided by one (1) and itself. ...
  • C program to implement structure
    Structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record. Stru...
  • C program to change case of string
    In this post, you learn how to change the case of string. If you entered "A", the program will change it's case to ...
  • C program to check leap year
    C program to check leap year: A leap year is exactly divisible by 4 except for century years (years ending with 00). The century yea...
  • C program to print Fibonacci series
    C program to print fibonacci series: The Fibonacci sequence is a series where the next term is the sum of pervious two terms. The fir...

Blog Archive

  • ▼  2017 (13)
    • ▼  January (13)
      • C program to change case of string
      • C program to implement structure
      • C program to check whether it is positive number o...
      • C program to check leap year
      • C program to find whether it is prime number or not
      • C program to print Fibonacci series
      • C program to check whether it is armstrong number ...
      • C program to swap two number without temporary var...
      • C program to swap two numbers using temporary vari...
      • C program to check whether entered character is Vo...
      • C program to print ASCII value of character
      • C program to print palindrome number
      • C program to print even & odd number

Copyright © PK SOLVED