Monday, April 27, 2020

C Programming slip 30

C Programming BCA Practical Slips Solution
Slip 30
Q1. Write a program to find sum of digits of a given input number using user defined
 Function [15 Marks]
# include<stdio.h> 
  
/* Function to get sum of digits */
int getSum(int n) 
{  
   int sum = 0; 
   while (n != 0) 
   { 
       sum = sum + n % 10; 
       n = n/10; 
   } 
   return sum; 

int main() 
  int n ; 
  printf("\n enter no=");
  scanf("%d",&n); 
  printf(" %d ", getSum(n)); 
  return 0; 
Q2. Write a program to accept a number and count number of even, odd and zero digits
 within that number. [25 Marks]
#include <stdio.h>
int main()
{    
      int nodd,neven,num,digit,zero=0 ;
      printf("Enter four digit number: ");
      scanf("%d",&num);
      while (num> 0)
      {
            digit = num % 10; /* separate LS digit from number */
            num /= 10;
            if(digit != 0 && digit % 2 == 0)
            {
                  neven++;
            }
            else if(digit==0)
            {
                  zero++;
            }
            else
            {
                  nodd++;
            }
      }
      printf("\nOdd digit : %d \nEven digit : %d\nZeros : %d", nodd, neven,zero);
      return 0;              
}

C Programming slip 29


C Programming BCA Practical Slips Solution
Slip 29
Q1. Write a C program to calculate factorial of a number using user defined function.
[15 Marks]
#include <stdio.h>
int main()
{    int n, i;
    unsigned long long factorial = 1;
    printf("Enter an integer: ");
    scanf("%d",&n);
    // show error if the user enters a negative integer
           for(i=1; i<=n; ++i)
        {
            factorial *= i;              // factorial = factorial*i;
        }
        printf("Factorial of %d = %llu", n, factorial);
        return 0;
}
Q2. Write a program, which accepts a number n and displays each digit separated by tabs.
Example: 6702 Output = 6 7 0 2 [25 Marks]
#include<stdio.h>
int main()
{
      int digit, num,rem;
      printf("Enter positive integer number: ");
      scanf("%d", &num);    
     while(num>0)
         {
          rem=rem*10+num%10;
           num=num/10;
          }
      printf("\nYou have entered: ");
      while (rem > 0)
      {            digit = rem % 10;   
            printf("\t %d",digit);
            rem=rem/10;
      }
      return 0;
}

C Programming slip 28

C Programming BCA Practical Slips Solution

Slip 28
Q1. Write a program to accept a string and then count the occurrences of a specific
 character of a string. [15 Marks]
#include <stdio.h>
#include <string.h>
  // Driver code 
int main() 
    char str[50]= "geeksforgeeks"; 
    char c ; 
    int res = 0; 
   printf("\n enter character ");
   scanf("%c",&c);
    for (int i=0;i<strlen(str);i++) 
          // checking character in string 
        if (str[i] == c) 
            res++; 
            printf("occurence of %c=%d",c,res++);
    return 0; 
}
Q2. Write a program to accept two numbers as range and display multiplication table
 of all numbers within that range. [25 Marks]
#include <stdio.h>
int main()
{    int i,j;
        for(j=1; j<=5; j++)
    {  
        for(i=1; i<=10; i++)
        { //printf("\t ");
        printf(" \n %d * %d = %d ",j, i, j*i);
        }
    printf("\n");
    }
    return 0;
}

C Programming slip 27

Slip 27
Q1. Write a program to count the occurrences of vowel from a input string. [15 Marks]
#include <stdio.h>
int main()
{
    char line[150];
    int i, vowels;
    vowels = 0;
    printf("Enter a line of string: ");
    scanf("%[^\n]", line);
    for(i=0; line[i]!='\0'; ++i)
    {        if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
           line[i]=='o' || line[i]=='u' || line[i]=='A' ||
           line[i]=='E' || line[i]=='I' || line[i]=='O' ||
           line[i]=='U')
        {
            ++vowels;
        }
            }
    printf("Vowels: %d",vowels);
       return 0;
}
Q2. Create a structure Item (Ino, Iname, Price). Accept details of n Items and write a
 menu driven program to perform the following operations options. [25 Marks]
1. Display all Items having price > 800
2. Display Item record with Ino=2
#include<stdio.h>
#include<stdlib.h>
struct details
{
     char name[30];
     int eid;
     int salary;
}emp[5];
void emp_search(int r)
{
     int id,i;
     printf("\nEnter Employee-Id to be Searched : ");
     scanf("%d",&id);
     printf("----------------------------------------\n");
     for(i=0;i<r;i++)
     {
          if(emp[i].eid==id)
          {
               printf("Employee Id : %d",emp[i].eid);
               printf("\nName        : %s",emp[i].name);
               printf("\nSalary      : %d\n",emp[i].salary);
          }
     }
}
void display(int r)
{
     int i;
     printf("\nList of All Employees:\n");
     printf("-------------------------------\n");
     printf("Emp-Id\tEmp-Name  Salary\n");
     printf("--------------------------------\n");
     for(i=0;i<r;i++)
     {
          printf("%d\t%s\t  %d\n",emp[i].eid,emp[i].name,emp[i].salary);
     }
}
void greater(int r)
{
     int i;
     printf("\nDetails of Employee Whose Salary > 10000\n");
     printf("------------------------------------------------");
     for(i=0;i<r;i++)
     {
          if(emp[i].salary>800)
          {
               printf("\n Employee Name : %s",emp[i].name);
               printf("\n Employee-Id   : %d",emp[i].eid);
               printf("\n Salary        : %d\n",emp[i].salary);
          }
     }
}
int main()
{
     int n,i,ch;
     printf("/*How Many Employee Record You Want to Add*/\n\nEnter Limit  : ");
     scanf("\n %d",&n);
     for(i=0;i<n;i++)
     {
          printf("-----------------------------------------");
          printf("\n\tEnter Details of Employee-%d",i+1);
          printf("\n-----------------------------------------");
          printf("\nName of Employee : ");
          scanf("%s",emp[i].name);
          printf("Employee-Id      : ");
          scanf("%d",&emp[i].eid);
          printf("Salary : ");
          scanf("%d",&emp[i].salary);
     }
     while(1)
     {
          printf("-----------------------------------------\n");
          printf("\t\tMenu\n");
          printf("-----------------------------------------");
          printf("\n 1:Search Employee by E-ID");
          printf("\n 2:List of All Employee");
          printf("\n 3:Display Employee Name whose Salary > 800 ");
          printf("\n 4:Exit");
          printf("\n----------------------------------------\n");
          printf("Enter Your Choice : ");
          scanf("\n %d",&ch);
          switch(ch)
          {
               case 1: emp_search(n);
               break;
               case 2: display(n);
               break;
               case 3: greater(n);
               break;
               case 4: exit(0);
          }
     }
     return 0;
}

C Programming slip 26

C Programming BCA Practical Slips Solution

Slip 26
Q1. Write a C program to calculate length of string without using standard functions.
[15 Marks]
#include <stdio.h>
int main()
{
    char s[1000];
    int i;

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

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

    printf("Length of string: %d", i);
    return 0;
}

Q2. Write a program to display the elements of an array containing n integers in the
 Reverse order using a pointer to the array. [25 Marks]
#include<stdio.h>
void main() 
{   int size, i, arr[30];
   int *ptr;
    ptr = &arr[0];
   printf("\nEnter the size of array : ");
   scanf("%d", &size);
   printf("\nEnter %d integers into array: ", size);
   for (i = 0; i < size; i++) {
      scanf("%d", ptr);
      ptr++;
   }
   ptr = &arr[size - 1];
   printf("\nElements of array in reverse order are :");
    for (i = size - 1; i >= 0; i--) {
      printf("\nElement%d is %d : ", i, *ptr);
      ptr--;
   }
    getch();
}