wordsclank.in: C
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Tuesday, 5 June 2018

C program to count the number of vowels, consonants, spaces and digits in a sentances

Vowels, consonant, C program

In this program we will count the number of vowels, consonants, digits or blank space present in a sentence given by user as input.



Program related to string:


Problem Statement:

C program to count the number of vowels, consonants, spaces and digits in a sentence.

Logic :


  • The program  took a string sentence from the user.
  • At first, the variables vowels, consonants, number and spaces are initialized to 0
  • Within a  for loop  we check for  vowels, consonants, number and spaces and when found we increment the corresponding variable by 1.
  • At last, we are printing the corresponding count on the screen. 





Source Code:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include
#include
#include

#define SIZE 100 //size of the string

void main()
{
 char sentence[SIZE],ch;
 int i;
 int vowels=0,consonants=0,number=0,spaces=0;
 printf("Enter the sentance \n");
 gets(sentence);
 for(i=0; i<strlen(sentence); i++)
 {
  if(sentence[i]=='A' || sentence[i]=='a' || sentence[i]=='E' || sentence[i]=='e' || 
        sentence[i]=='I' || sentence[i]=='i' || sentence[i]=='O' || sentence[i]=='o' || 
     sentence[i]=='U' || sentence[i]=='u')
        {
            vowels++;
        }
        else if((sentence[i]>='a' && sentence[i]<='z') || (sentence[i]>='A && sentence[i]<='Z'))
        {
            consonants++;
        }
        else if(sentence[i]>='0' && sentence[i]<='9')
        {
            number++;
        }
        else if (sentence[i]==' ')
        {
            spaces++;
        }
    }
    printf("The number of vowel is %d ,consonant is %d, number is %d and blank spaces is %d \n",vowels,consonants,number,spaces);
    
}

Output:


vowels,consonants in c programing


More topics on C programs:

Read more ...

Friday, 13 April 2018

C Program to find Perfect Numbers between intervals given by user

What is Perfect Number? 


www.wordsclank.inperfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum).


Also, a perfect number is that number whose sum of its proper positive divisor(including the number itself) is equals to the twice the number.


For example 28,
divisor of 28:1,2,4,7,14,28

sum of divisors(excluding the number itself):1+2+4+7+14=28 i.e, equals to the number 28.

or, sum of divisors(including the number itself):1+2+4+7+14+28=56 i.e, equals to twice the number(2 * 28 = 56).


Logic behind the program:

  • Take the two intervals from the user.
  • Run a for loop from i to last interval, where i is first interval and increment  i by 1 in each iteration.
  • Withing that for loop run another for loop which will find the perfect numbers and print it.

Similar Problem:


Problem Statement:

Write a program in C which will find all the perfect numbers between two intervals given by the user.






Source Code:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include

void main()

 {

   int i, j, beg, end, sum;

   printf("Enter the first interval:");

   scanf("%d", &beg);
   
   printf("Enter the last interval:");

   scanf("%d", &end);
   

   for(i = beg; i<=end; i++)

   {
     sum = 0;
     
     for(j=1; j<i; j++)
     
     {

   if(i % j == 0)

                 sum = sum + j;
          
             }

     if( sum == i) 

             {

                  printf("%d \t",i);

             }
}


 }

Output:



More Topics on C program


Read more ...

Tuesday, 13 March 2018

C program to store information of n student using structure

A structure is a user defined data type available in C that helps to combine a lot of different data type under a single name which is easier to handle.
For example: A structure student which will store roll number of integer data type, name of character data type, marks of float type.
structure,structure in c

Problem statement:

C program to store information of n student using structure



Source code

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include

struct student
{
 char name[100];
 int roll;
 float mark;
}str[100];

int main()
{
 int n, i;
 printf("Enter the number of student: ");
 scanf("%d",&n);
 
 printf("Enter the informations of the student:\n");
 
 for(i=0;i<n;i++)
 {
  str[i].roll = i+1;
  
  printf("For roll number %d \n",str[i].roll);
  
  printf("Enter name: ");
  scanf("%s", str[i].name);
  
  printf("Enter marks: ");
  scanf("%f",&str[i].mark);
  
  printf("\n");
 }
 
 printf("The information of %d students are as follows:\n",n);
 for(i=0;i<n;i++)
 {
  printf("Roll number : %d\n",i+1);
  printf("Name:");
  puts(str[i].name);
  printf("Marks:%f",str[i].mark);
  printf("\n");
 }
 return 0;
}



Output

structure, structure in c
Read more ...

Sunday, 11 March 2018

C program to find the length of a string without strlen( ) function

This program calculates the length  of a string manually using a for loop.We are not using strlen( ) over here.
string, strlen(), String in C



SOURCE CODE

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include
int main()
{
    char i, str[100];

    printf("Enter the string: ");
    scanf("%s", str);

    for(i = 0; str[i] != '\0'; i++);
    
    printf("Length of string: %d", i);
    return 0;
}

OUTPUT

string,string in c,strlen()

Explanation:

Here within the main() we have declared one character variable i and one character string srt[].Then we print a message for the user to input the string and stores it in str[]. Then we are running a for loop which run from i=0 until str[i]!='/0' i.e, null and it will count the i i.e, the number of elements in the string.Next we just print the value of i , i.e, the number of elements in the string.

More Topics on C program

Read more ...

Saturday, 10 March 2018

C program to find the Armstrong Number between intervals

A  number of n digit is called Armstrong number if sum of the power of n of its digits is equal to the number itself.
For example a number of three digits integer is an Armstrong number if the sum of the cubes of its digits is equal to the number itself. 153 is an Armstrong number since 1*1*1 + 5*5*5 + 3*3*3 = 371. 
armstrong number,armstrong number in C


Most Related:

PROBLEM STATEMENT

Write a program in C to find the Armstrong Number between intervals.




SOURCE CODE


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include
#include

int main()
{
    int a, b, i, num1, num2, rem, n = 0, result = 0;

    printf("Enter the first interval: ");
    scanf("%d", &a);
    printf("Enter the last interval: ");
    scanf("%d", &b);
    printf("Armstrong numbers between %d and %d are: ", a, b);

    for(i = a + 1; i <= b; i++)
    {
        num1 = i;
        num2 = i;

        //checking number of digits
        while (num1 != 0)
        {
            num1 = num1 / 10;
            n++;
        }

        while (num2 != 0)
        {
            rem = num2 % 10;
            result = result + pow(rem, n);
            num2 = num2 / 10;
        }

        if (result == i) 
 {
            printf("%d ", i);
        }
        n = 0;
        // setting n as o for next iteration
        result = 0;

    }
    return 0;
}



OUTPUT

armstrong number,armstrong number in C

Read more ...

Thursday, 8 March 2018

C program to check whether a certain number is Armstrong or not

A  number of n digit is called Armstrong number if sum of the power of n of its digits is equal to the number itself.
For example a number of three digits integer is an Armstrong number if the sum of the cubes of its digits is equal to the number itself. 153 is an Armstrong number since 1**3 + 5**3 + 3**3 = 371. 
armstrong number in C,armstrong number


Most Related:






SOURCE CODE



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include
#include


int main()

{

    int num, rem, altnum, result = 0, n = 0 ;


    printf("Enter an integer: ");

    scanf("%d", &num);


     altnum = num;

    

    while (altnum != 0)

    {

        altnum /= 10;

        n++;

    }


    altnum = num;


    while (altnum != 0)

    {

        rem = altnum % 10;

        result = result + pow(rem, n);

        altnum /= 10;

    }


    if(result == num)

        printf("%d is an Armstrong number.", num);

    else

        printf("%d is not an Armstrong number.", num);


    return 0;

}



OUTPUT

armstrong number,armstrong number in C


Read more ...

Sunday, 4 March 2018

C Program to Check Whether a Number is Palindrome or Not

palindromic number is a number that remains the same when its digits are reversed.The term palindromic is derived from palindrome, which refers to a word (such as rotor or racecar) whose spelling is unchanged when its letters are reversed.
For example:12321 is same when its digits are reversed.


Palindrome number,Palindrome number in C






SOURCE CODE

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include
#include

void main()


{


    int num, i, reverse = 0, temp, rem;


    printf("Enter number: ");


    scanf("%d",&num);


    temp = num;


    while(num ! = 0)


    {


       rem = num % 10;


       num = num / 10;


       reverse=rem+(reverse*10);


    }


    if(reverse==temp)


    {


       printf(" Palindrome Number");


    }


    else


    {


       printf("Not a Palindrome Number");


    }


   getch();


}


OUTPUT

palindrome number,palindrome number in C

Read more ...

Saturday, 3 March 2018

C Program to display the Fibonacci Series up-to n number of terms

Before we start coding lets know what exactly Fibonacci Sequence is?
A series of a number in which each number is sum of the two preceding numbers.
For example,  1,1,2,3,5,8,13,21,34,55,89.. is the simplest fibonacci series.

Fibonacci Sequence,Fibonacci Sequence in c

Most related






SOURCE CODE


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include< stdio.h>

void main()
{
 int i, n, a = 0, b = 1, nxt;
 printf("Enter the number of terms: \n");
 scanf("%d",&n);
 
 printf("The Fibonacci series upto %d term is:\n",n);
 
 for(i = 1; i <= n; i++)
 {
  printf("%d \t",a);
  nxt = a + b;
  a = b;
  b = nxt;
 }
}


OUTPUT

Fibonacci Sequence,Fibonacci Sequence in C

Related Topics




  • C program to store information of n student using structure
  • C program to find the length of a string without strlen( ) function
  • C program to find the Armstrong Number between intervals
  • C program to check whether a certain  number is Armstrong or not
  • C Program to Check Whether a Number is Palindrome or Not
  • C Program to display the Fibonacci Series up-to a certain number
  • C program to find the factorial of a number
  • C Program to Find Prime Numbers Between Intervals Using Functions.
  • C program: perfect or not checking
  • C program to identify a number as even or odd



  • Read more ...

    C Program to display the Fibonacci Series up-to a certain number

    Before we start coding lets know what exactly Fibonacci Sequence is?
    A series of a number in which each number is sum of the two preceding numbers.
    For example,  1,1,2,3,5,8,13,.. is the simplest fibonacci series.

    wordsclank.blogspot.com
    Most related






    SOURCE CODE

    >
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #include<stdio.h>
    void main()
    {
     int i, n, a=0, b=1, nxt;
     
     printf("Enter the number:\n");
     scanf("%d",&amp;n);
     
     printf("The Fibonacci series is: 1, \t ");
     // first two term will always 1
     
     nxt = a + b;
     
     while(nxt &lt;= n)
     {
      printf("%d, \t",nxt);
      a = b;
      b = nxt;
      nxt = a + b;
     }
     
    }
    

    OUTPUT

    Words Clank,wordsclank

    Explanation

    Within the main() function first we declarer i, n, a=0, b=1, nxt  as integer variable.Next we display a message for the user to give input.Then we store the value given by the user in n.Then again we display a message that  "The Fibonacci series is: 1, ".Here we have already printed the first term as 1 because we know that the first term of a Fibonacci series is always 1.Then in nxt we store the value of a+b.Then  we are running a  while loop that will run until nxt less then equall to number n.Within the loop first we are printing the value of nxt then we are assigning the value of a to b and then we are assigning the value of nxt to b and then again we are assigning the value of nxt as a+b.

    Related Topics

    Read more ...

    C program to find the factorial of a number

    Before we start coding lets have a brief look on what factorial really means.?
    The factorial of a integer n is nothing but the product of the integer and all the integer below it.
    For example n!=(n)*(n-1)*(n-2)...*2*1
     or, n!=1*2*3*4....(n-1)*n
    note that 0!=1
    wordsclank.bmlogspot.co





    SOURCE CODE


    1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    #include<stdio.h>
    void main()
    {
     int i, num;
     int factorial;
     
     printf("Enter the integer :\n");
     scanf("%d",&num);
     if(num > 0)
     {
      for(i = 1; i <= num; i++)
      {
       factorial = factorial * i;
      }
      printf("The factorial of %d is %d",num,factorial);
     }
     else if(num == 0)
     {
      printf("The factorial of %d is 1",num,factorial);
      //0! is always 1
     }
     else
     {
      printf("Error! Negative number's factorial doesnt exist. Enter a positive number:\n");
      //error message for user for entering negavitive number
     }
     
    }
    

    OUTPUT


    wordsclank.blogspot.com

    Explanation

    Within the main function first we declared three integer variable i , n and factorial . Next we display a message to take input from the user and store it to the integer variable n.Next we use a if -else conditional checking that will check the number as positive or negative. If the number is negative then it will display a  message that "Error! Negative number's factorial doesn't exist. Enter a positive number:".And if the number is positive it will continue.Then under the for loop it will multiply number and numbers below it one by one and store it in factorial integer variable.when the controls comes out of the loop then it will display it.
    I

    Related Topics



    Read more ...

    Monday, 29 January 2018

    C Program to Find Prime Numbers Between Intervals Using Functions.


    A Prime number an integer which is divisible only by 1 and itself.In other word a whole number who have two factor 1 and the number itself.
    For example 2, 3, 5, 7, 11, 13, 17, 23, ...

    prime number, prime number in c


    Here we make a user defined function primecheck() that will check and print all the prime numbers between the given interval.

    PROBLEM STATEMENT:

    Write a  Program in C to find prime numbers between intervals using functions.





    SOURCE CODE:


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    #include
    
    void primecheck(int x,int y);
    
    void main()
    
    {
    
          int n, m;
    
          printf("Enter the first number:");
    
          scanf("%d",&n);
    
          printf("Enter the secound number:");
    
          scanf("%d",&m);
    
          primecheck(n, m);
    
    }
    
    
    void primecheck(int x,int y)
    
    {
    
          int i,count=0,j;
    
          for(i = x; i <= y; i++)
    
          {
    
                for(j=1; j <= i; j++)
    
                {
    
      
    
                      if(i%j==0)
    
                      {
    
                            count = count + 1;
    
                      }
    
                }
    
           if(count == 2)
    
           {
    
                 printf("%d",i);
    
           }
    
           count=0;
    
          }
    
    }
    


    OUTPUT:
    prime number, prime number in c

    If the user's first number is greater than the second number then the numbers need to be swapped first.


    More topics on C programming:




      Read more ...

      Saturday, 8 April 2017

      C program: perfect or not checking

       A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum).
      For example 28. Its divisor are 1, 2, 4, 7, 14, and 28. And the sum of its divisors i.e, 1+2+4+7+14 is also 28. So 28 is a perfect number


      perfect number, perfect number in c


      PROBLEM STATEMENT:

      Write a program in C to check whether a number is perfect or not.





      SOURCE CODE:

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      #include
      
      void main()
      
       {
      
         int i, num, sum = 0;
      
         printf("Enter the number:");
      
         scanf("%d", &num);
      
         for(i = 1; i < num; i++)
      
         {
      
               if(num % i == 0)
      
                sum = sum + i;
      
         }
      
         
          if( sum == num) 
      
          {
      
               printf("%d is perfect",num);
      
          }
      
          else
      
          {
      
               printf("%d not perfect",num);
      
          }
      
       }


      OUTPUT

      perfect number, perfect or not in c


      Explanation

      Here within the main() function first we declared two integer variable num and sum. We initialized sum as 0 .Then we display a message for the user for taking input and store it in the num variable. Then we are running a for loop from i=0 to iwithin which there is a conditional for loop. We are finding perfect divisor by checking if the remainder is zero i.e, num % i==0 then we are adding the value of i in sum  where i is divisor.Next again we are running a if-else loop to declared the as perfect or not .If the sum is equals to the number then we are printing the number as perfect else it is not perfect.

      Read more ...