Search This Blog

Showing posts with label NEP_C_Programs. Show all posts
Showing posts with label NEP_C_Programs. Show all posts

Wednesday, 2 March 2022

B10. C Program to demonstrate the difference between structure and union.

 /* B10. C Program to demonstrate the difference between structure and union */

#include<stdio.h>

struct mystruct
{
    int i;
    float f;
};

union  myunion
{
    int i;
    float f;
};

void main()
{
    struct mystruct s;
    union myunion u;

    clrscr();

    printf("Size of the struct mystruct=%d\n", sizeof(s));
    printf("Size of the union myunion=%d\n", sizeof(u));

    s.i=10;
    s.f=3.45;

    printf("st.i=%d\n",s.i);
    printf("st.f=%f\n",s.f);

    u.i=20;
    printf("ut.i=%d\n",u.i);
    u.f=8.25;
    printf("ut.f=%f\n",u.f);
    getch();
}

B9. C Program to demonstrate student structure to read and display records of n students.

 // B9. C Program to demonstrate student structure to read and display records of n students.
#include<stdio.h>
#include<conio.h>

struct student
{
    char name[20];
    int age;
    char city[20];
};

void main()
{
    struct student s[10];
    int i, n;
    clrscr();

    printf("\nEnter the number of students \n");
    scanf("%d",&n);

    printf("\nEnter details of %d student(s):\n",n);

    for(i=0;i<n;i++)
    {
        printf("\nEnter Name, age and city of %d student: \n",i+1);
        scanf("%s%d%s",&s[i].name,&s[i].age,&s[i].city);
    }
    printf("\nThe details of students: \n");
    printf("Name \t Age \t City \n");
    printf("==== \t === \t ==== \n");
    for(i=0;i<n;i++)
        printf("%s \t %d \t %s \n",s[i].name,s[i].age,s[i].city);

    getch();
}


B8. Write a C Program to Reverse a String using Pointer.

 //B8. Write a C Program to Reverse a String using Pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
    char *s;
    int len,i;
    clrscr();

    printf("\nEnter a String:\n");
    gets(s);

    len=strlen(s);
    printf("\nThe reverse of String is:\n");

    for(i=len;i>=0;i--)
        printf("%c",*(s+i));
    getch();
}

B7. C Program to read a string and to find the number of alphabets, digits, vowels, consonants, spaces and special characters.

 /* B7. C Program to read a string and to find the number of alphabets, digits, vowels,
consonants, spaces and special characters. */

#include <stdio.h>
 
void main()
{
    char   str[100];
    int i, digit, vowel, consonant, splchar, spaces;
    clrscr();

    digit = vowel = consonant = splchar = spaces = 0;

    printf("Enter a string: ");
    gets(str);

    for(i=0;str[i]!=NULL;i++)
    {

    if(str[i]>='0' && str[i]<='9')
        digit++;
    else if(str[i]==' ')
        spaces++;
    else 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')

        vowel++;
    else if((str[i]>='A' && str[i]<='Z')||(str[i]>='a' && str[i]<='z'))
        consonant++;
    else
        splchar++;
    }
 
   printf("\nDigits: %d \nVowels: %d \nSpaces: %d \nConsonants: %d \nSpecial Characters: %d",digit,vowel,spaces, consonant,splchar);
   getch();
}

B5. Write a C Program to find the trace of a square matrix using function

 //B5. Write a C Program to find the trace of a square matrix using function
#include<stdio.h>
#include<conio.h>

int trace(int[10][10],int);   // Function prototype declaration.

void main()
{
    int a[10][10],m,n,i,j,sum;
    clrscr();
    printf("Enter the order of the square matrix\n");
    scanf("%d",&m);
    printf("Enter the matrix\n");
    for(i=0;i<m;i++)
        for(j=0;j<m;j++)
            scanf("%d",&a[i][j]);    // Read matrix elements.
    sum=trace(a,m);  // Call trace function.
    printf("Trace of the matrix is %d\n",sum);
    getch();
}

int trace(int a[10][10], int m)  //Function to find trace of matrix.
{
    int i,j,sum;
    sum=0;
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            if(i==j) // Condition to check for diagonal elements.
                sum=sum+a[i][j];
        }
    }
    return sum;
}

B4. C Program to generate n prime numbers by defining isprime function.

 //B4. C Program to generate n prime numbers by defining isprime function.
#include<stdio.h>
#include<conio.h>
void isprime(int);  //Function prototype declaration.
void main()
{
     int n;
     clrscr();
     printf("How many prime numbers are required? \n");
     scanf("%d", &n);
     printf("Prime numbers are:\n");
     isprime(n);
}
void isprime(int n)
{
    int count=1,flag, i=2,j;
     /* Generating prime numbers */
     while(count <= n)
     {
          flag = 0;
          for(j=2; j <= i/2; j++)
          {
               if(i%j==0)
               {
                flag=1;
                break;
               }
          }
          if(flag==0)
          {
               printf("%d\n",i);
               count++;
          }
          i++;
     }
     getch();
}

B3. C Program to demonstrate pointers in C

 /* B3. C Program to demonstrate pointers in C */
#include<stdio.h>

void main()
{
    int i=10, *ip;
    float f=3.4, *fp;
    char c='a', *cp;

    clrscr();

    printf("i=%d\n",i);
    printf("f=%f\n",f);
    printf("c=%c\n",c);

    ip=&i;
    printf("\nAddress of i=%u\n", ip);
    printf("Value stored in i=%d\n", *ip);

    fp=&f;
    printf("\nAddress of f=%u\n", fp);
    printf("Value stored in f=%f\n", *fp);

    cp=&c;
    printf("\nAddress of c=%u\n", cp);
    printf("Value stored in c=%c\n", *cp);

    getch();
}

B2. C Program to demonstrate string functions(Atleast 3)

 /* B2. C Program to demonstrate string functions(Atleast 3)*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char str1[50],str2[50];
    int n,i,length1,length2;
    clrscr();

    printf("Enter first string\n");
    scanf("%s",&str1);
    printf("Enter second string\n");
    scanf("%s",&str2);
    printf("\nString1 Entered=%s\n",str1);
    printf("String2 Entered=%s\n",str2);

    length1=strlen(str1);   // Find the length of str1
    length2= strlen(str2);  // Find the length of str2
    printf("\nString Length of %s is %d\n",str1,length1);
    printf("String Length of %s is %d\n",str2,length2);
    strcat(str1,str2);  // Concatenate str1 & str2
    printf("\nString after concatenation:%s\n",str1);

    strcpy(str1,str2); // Copy str2 in str1
    printf("\nString after strcpy:%s", str1);

    getch();

}

B1. C Program to find the length of a string without using built in function.

 /* B1. C Program to find the length of a string without using built in function */

#include<stdio.h>
#include<conio.h>
void main()
{
    char str[50];
    int i,length=0;
    clrscr();
    printf("Enter a string:\n");
    gets(str);
    for(i=0;str[i]!='\0';i++)
        length++;
    printf("Length of %s is %d\n",str,length);
    getch();
}

Wednesday, 15 December 2021

10. C Program to perform addition and subtraction of Matrices .

 /*10. C Program to perform addition and subtraction of Matrices */

#include<stdio.h>

void main()

{

     int a[10][10],b[10][10],add[10][10],sub[10][10],i,j,m,n;

     clrscr();

     printf("Enter the order of matrix\n");

     scanf("%d%d",&m,&n);

     printf("Enter the elements of first matrix:\n");

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

     {

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

          {

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

          }

     }

     printf("Enter the elements of second matrix:\n");

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

     {

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

          {

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

          }

     }

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

     {

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

          {

              add[i][j]=a[i][j]+b[i][j];

              sub[i][j]=a[i][j]-b[i][j];

          }

     }

     printf("Matrix Addition:\n");

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

     {

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

          {

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

          }

          printf("\n");

     }

     printf("Matrix Subtraction:\n");

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

     {

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

          {

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

          }

          printf("\n");

     }

     getch();

}

9. Write a C Program to remove Duplicate Element in a single dimensional Array.

 /*9. Write a C Program to remove Duplicate Element in a single dimensional Array.*/

#include <stdio.h>

void main()

{

    int arr[20];   // Declare an array of size 20

    int size;

    int i, j, k;       // Loop control variables

    clrscr();

 

    printf("Enter size of the array:");

    scanf("%d", &size); // Read size of the array

 

    printf("Enter elements in array:\n");

    for(i=0; i<size; i++)  // Read array elements

    {

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

    }

 

    //Find duplicate elements in the array

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

    {

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

     {

         // Check for duplicate elements

         if(arr[i] == arr[j])

         {

          // Delete the current duplicate element

                for(k=j; k < size - 1; k++)

                {

                    arr[k] = arr[k + 1];

                }

          // Decrement size after removing duplicate element

                size--;

          j--;

            }

        }

    }

 

    //Display array after deleting duplicate elements

    printf("\nArray elements after deleting duplicates: ");

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

    {

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

    }

 

    getch();    

}

8. Write a C program to read marks scored in 3 subjects by n students and find the average of marks and result (Demonstration of single dimensional array)

 /* 8. Write a C program to read marks scored in 3 subjects by n students and find the average of marks and result (Demonstration of single dimensional array) */

#include<stdio.h>

void main()

{

     int sub1[10],sub2[10],sub3[10],total[10],avg[10],n,i;

     printf("Enter no of students:");

     scanf("%d",&n);

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

     {

          printf("Enter the marks of Student-%d:\n",i+1);

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

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

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

          total[i]=sub1[i]+sub2[i]+sub3[i];

          avg[i]=total[i]/n;

     }

     printf("Results are as follows:\n");

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

     {

          printf("Student-%d is as follows:\n",i+1);

          if((total[i]*100/300)>=70)

              printf("Distinction\n");

          else if((total[i]*100/300)>=60)

              printf("First Class\n");

          else if((total[i]*100/300)>=50)

              printf("Second Class\n");

          else if((total[i]*100/300)>=40)

              printf("Pass\n");

          else

              printf("Fail\n");

     }

     getch();

}

6.Write a C Program to read percentage of marks and to display appropriate grade (using switch case)

/*6.Write a C Program to read percentage of marks and to display appropriate grade (using switch case)*/

#include<stdio.h>

void main()

{

     int percent;

     clrscr();

     printf("Enter Marks in Percentage:");

     scanf("%d",&percent);

     switch(percent/10)

     {

          case 10:

          case  9:

          case  8:

          case  7:printf("Distinction!\n");

              break;

          case  6:

              printf("First Class!\n");

              break;

          case 5: printf("Second Class!\n");

              break;

          case 4: printf("Pass!\n");

              break;

          default:printf("Fail!\n"); break; } getch(); }

5. C Program to read numbers from keyboard continuously till the user presses 999 and find sum of positive numbers.

/*5. C Program to read numbers from keyboard continuously till the user presses 999 and find sum of positive numbers */

#include<stdio.h>

void main()

{

     int num, sum=0;

     clrscr();

     while(1)

     {

          printf("Enter any number:");

          scanf("%d",&num);

          if(num>0 && num!=999)

          {

              sum=sum+num;

          }

          if(num==999)

          {

              printf("Sum of Positive numbers=%d",sum);

              getch();

              exit(0);

          }

     }

     getch();

}


Tuesday, 23 November 2021

6.Write a C Program to read percentage of marks and to display appropriate grade (using switch case)

 /*6.Write a C Program to read percentage of marks and to display appropriate grade (using switch case) */

#include<stdio.h>

void main()

{

     int percent;

     clrscr();

     printf("Enter Marks in Percentage:");

     scanf("%d",&percent);

     switch(percent/10)

     {

          case 10:

          case  9:

          case  8:

          case  7:printf("Distinction!\n");

              break;

          case  6:

              printf("First Class!\n");

              break;

          case 5: printf("Second Class!\n");

              break;

          case 4: printf("Pass!\n");

              break;

          default:printf("Fail!\n"); break; } getch(); }

4. Write a C Program to read a number, find the sum of the digits, reverse the number and check it for palindrome.

/*4. Write a C Program to read a number, find the sum of the digits, reverse the number and check it for palindrome */

#include<stdio.h>

void main()

{

     int num,temp,digit,sum=0,rev=0;

     clrscr();

     printf("Enter a number:");

     scanf("%d",&num);

     temp=num;

     while(num>0)

     {

          digit=num%10;

          num=num/10;

          sum=sum+digit;

          rev=rev*10+digit;

     }

     printf("\nSum of Digits=%d",sum);

     printf("\nReverse Number=%d",rev);

     if(rev==temp)

     {

          printf("\n%d is Palindrome.",temp);

     }

     else

          printf("\n%d is Not a Palindrome.",temp);

     getch();

}

3. C Program to demonstrate library functions in math.h (at least 5)

/*3. C Program to demonstrate library functions in math.h(at least 5)*/

#include<stdio.h>

#include<math.h>

void main()

{

     clrscr();

     printf("Mathematical Functions:\n");

     printf("Sin(45)=%f\n",sin(45));

     printf("Sqrt(4)=%f\n",sqrt(4));

     printf("Power(4,2)=%lf\n",pow(4,2));

     printf("Log(5)=%f\n",log(5));

     printf("Floor(4.3)=%f\n",floor(4.3));

     printf("Ceil(4.2)=%f\n",ceil(4.2));

     getch();

}

2. Write a C Program to read three numbers and find the biggest of three.

/*2. Write a C Program to read three numbers and find the biggest of three */

#include<stdio.h>

void main()

{

     int a,b,c,big;

     clrscr();

     printf("Enter three numbers:\n");

     scanf("%d%d%d",&a,&b,&c);

     if(a>b && a>c)

     {

          big=a;

     }

     else if(b>c)

     {

          big=b;

     }

     else

     {

          big=c;

     }

     printf("Biggest number is %d",big);

     getch();

}

FREE Hit Counters