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