wordsclank.in

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