Saturday, October 22, 2022

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program 7 - C Program to Count Number of vowels in a given sentence

 

Write a C Program to Count Number of vowels in a given sentence





Source code


#include<stdio.h>

void main()

{

    char str[100];

    int i,count=0;

    printf("Enter the sentence to count vowels:");

 gets(str);

    for(i=0;str[i]!='\0';i++)

    {

        if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'

        ||str[i]=='I'||str[i]=='O'||str[i]=='U')

        {

            count++;

        }

       

    }

    printf("\nNumber of vowels in given sentence: %d\n",count);

}


Output


Enter the sentence to count vowels:This is a program to count vowels


Number of vowels in given sentence: 10


Tuesday, October 18, 2022

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program 6 - C Program to check whether the given string is palindrome or not using Pointers



Write a C Program to check whether the given string is palindrome or not using Pointers



 


Source code
String palindrome using pointers in c

#include<stdio.h>

#include<string.h>

void main()

{

    char str[1000];

    char *ptr1, *ptr2;

    printf("Enter the String:");

    scanf("%s",str);

    ptr1=str;

    while(*ptr1!='\0')

    {

        ++ptr1;

    }

    --ptr1;

    for(ptr2=str;ptr1>=ptr2;)

    {

        
        if(*ptr1==*ptr2)

        {

            ptr1--;

            ptr2++;

        }

        else

            break;    

    }

    if(ptr2>ptr1)

        printf("String is Palindrome");

    else

        printf("String is not a Palindrome");

}


Output

Enter the String:madam

String is Palindrome

Enter the String:hello

String is not a Palindrome



Thursday, September 29, 2022

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program 5 - C Program to sort a given set of numbers in Ascending order

  

Write a C Program to Sort a given set of numbers in Ascending order










Source Code:

#include <stdio.h>

void main()

{

    int i,j,temp,n,x[30];

    printf("Enter total number of values:");

    scanf("%d",&n);

     printf("Enter the values:");

     for(i=0;i<n;i++)

        scanf("%d",&x[i]);

    for(i=0;i<n;i++)

    {

        for(j=i+1;j<n;j++)

        {

            if(x[i]>x[j])

            {

                temp=x[i];

                x[i]=x[j];

                x[j]=temp;

            }

        }

    }

    printf("Ascending order of given values are:\n");

    for(i=0;i<n;i++)

        printf("%d\t",x[i]);

}



Output


Enter total number of values:5
Enter the values:10
3
9
6
2
Ascending order of given values are:
2       3       6       9       10
















Friday, September 23, 2022

BCA and B.Sc Programs - Programming in C Lab - Bharathiar University - Practical Program 4 - C Program to print Magic Square

 

Write a C Program to Print  Magic Square where n>3 and n is odd






In this Page:

  • Magic Square Program Source Code for Visual Studio Code
  • Magic Square Program Source Code for MS Dos Turbo C++


Source Code for Visual Studio Code:

#include <stdio.h>
#include <string.h>
void magicSquare(int n)
{
    int x[n][n];
    // set all slots as 0
    memset(x, 0, sizeof(x));
    int row = n / 2;
    int col = n - 1;
    int limit=n*n;
    int i,j;
    for (int num = 1; num <= limit;) {
        if (row == -1 && col == n) // 3rd condition
        {
row = 0;
            col = n - 2;
        }
        else {
  // 1st condition            
            if (row < 0)
                row = n - 1;

            // 1st condition             
            if (col == n)
                col = 0;
            }
        if (x[row][col]) // 2nd condition
        {
row++;
            col =col- 2;
            continue;
        }
        else
{
            x[row][col] = num; // set number
num++;
}
row--; // 1st condition
        col++;
    }
    // Print magic square
    printf("The Magic Square \n");       
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++)
            printf("%3d ", x[i][j]);
        printf("\n");
    }
}
int main()
{
    int n; 
    printf("Enter order of Magic Square:");
    scanf("%d",&n);
    if(n>3 && n%2!=0)
        magicSquare(n);
    else
        printf("n value must be odd and greater than 0");
    return 0;
}


Output:


Enter order of Magic Square:5

The Magic Square 

  9   3  22  16  15

  2  21  20  14   8

 25  19  13   7   1

 18  12   6   5  24

 11  10   4  23  17




Source Code for MS Dos Turbo C++: 

#include <stdio.h>

#include <string.h>

#include<conio.h>

void magicSquare(int n)

{

    int x[50][50];

     int row = n / 2;

    int col = n - 1;

    int limit=n*n;

    int i,j,num;

    // set all slots as 0

    memset(x, 0, sizeof(x));


    for (num = 1; num <= limit;)

    {

if (row == -1 && col == n) // 3rd condition

{

row = 0;

    col = n - 2;

}

else {

  // 1st condition

    if (row < 0)

row = n - 1;


    // 1st condition

    if (col == n)

col = 0;

    }

if (x[row][col]) // 2nd condition

{

row++;

    col =col- 2;

    continue;

}

else

{

    x[row][col] = num; // set number

num++;

}

row--; // 1st condition

col++;

    }

    // Print magic square

    printf("The Magic Square \n");

    for (i = 0; i < n; i++) {

for (j = 0; j < n; j++)

    printf("%3d ", x[i][j]);

printf("\n");

    }

}

int main()

{

    int n;

    printf("Enter order of Magic Square:");

    scanf("%d",&n);

    if(n>3 && n%2!=0)

magicSquare(n);

    else

printf("n value must be odd and greater than 3");

    getch();

    return 0;

}

Output:


Enter order of Magic Square:5

The Magic Square 

  9   3  22  16  15

  2  21  20  14   8

 25  19  13   7   1

 18  12   6   5  24

 11  10   4  23  17





Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...