Skip to main content

Programming Examples

1. Write a program to determine the size of integer and float using sizeof() operator.
#
#
void main()                                                                     
  {
  clrscr();
  printf("size of int=%d\t size of float=%d ",sizeof(int),sizeof(float));
  getch();

  }


Program no. 2
WAP that asks a number and tests it whether it is multiple of 5 or not.
void main()
 {
 int a,rem;
 clrscr();
 printf("Enter number to be tested\n");
 scanf("%d",&a);
 rem=a%5;
 if(rem==0)
   printf("\nNum. is multiple of 5");
 else
   printf("\nNum. is not multiple of 5");
 getch();
 }

Program no. 3
WAP that checks whether the number entered by the user is exactly 5 but not by 11.
void main()
 {
 int a,rem1,rem2;
 clrscr();
 printf("Enter number to be tested\n");
 scanf("%d",&a);
 rem1=a%5;
 rem2=a%11;
 if(rem1==0 && rem2!=0)
   printf("\nThe number is divisible by 5  
                         but not by 11");
 else
   printf("\nThe number is not divisible
                       by 5 but not by 11");
 getch();
 }



Program 4
WAP that asks the user two integers. Check whether first number is exactly divisible by second.

void main()
 {
 int num1,num2,rem;
 clrscr();
 printf("Enter First number\n");
 scanf("%d",&num1);
 printf("Enter second Number\n");
 scanf("%d",&num2);
 rem=num1%num2;
 if(rem==0)
   printf("\nThe First number is exactly
                  divisible by second");
 else
   printf("\nThe First number is not
                exactly divisible second");
 getch();
 }

Program no. 5
WAP to find cubes and square of 10 first natural numbers.
void main()
 {
 int i,sq,cube;
 clrscr();
 for(i=1;i<=10;i++)
  {
  sq=i*i;
  cube=i*i*i;
  printf("\nsquare of %d\t%d\tCube of %d\t%d",i,sq,i,cube);
    }
getch();
 }

Program no. 6
WAP to find the sum of all numbers from 1 to 20 and display the sum.
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=20;i++)
   sum+=i;
 printf("\nThe sum is\t%d",sum);
 getch();
}

Program no 7
WAP to ask a number and print the entered number in reverse order. i.e. display 321 for number 123.
void main()
{
int num,q,r;
clrscr();
printf("\Enter number to be tested\n");
scanf("%d",&num);
do
{
q=num/10;
r=num%10;
num=q;
printf("%d",r);
}while(q!=0);
getch();
}

Program no. 8
WAP to read two integers n1 and n2. Display even numbers between those two numbers.
void main()
  {
  int n1,n2,i;
  clrscr();
  do
  {
  printf("\nEnter n1 and n2");
  scanf("%d%d",&n1,&n2);
  }while(n1>n2);
  for(i=n1;i<=n2;i++)
     {
     if(i%2==0)
            printf("%d\t",i);
     }
   getch();
  }


Program no. 9
WAP that display each two digit odd numbers, its square and cube along one line.
void main()
 {
 int i,sq;
 long int cube;
 clrscr();
 for(i=1;i<100;i++)
 {
  sq=i*i;
  cube=i*i*i;
  printf("\nThe Number=%d\tsquare=%d 
                     \tcube=%ld " ,i,sq,cube);
  }
 getch();
 }

Program no. 10
WAP that converts decimal number into binary number format.
void main()
{
int deciNum, binNum,q,r,revSum=0,i=1;
clrscr();
printf("\Enter decimal number\n");
scanf("%d",&deciNum);
do
{
q=deciNum/2;
r=deciNum%2;
deciNum=q;
revSum+=r*i; //to reorder the binary bits
i*=10;
}while(q!=0);
printf("\nThe corresponding binary
               number is\t%d",revSum);
getch();
}

Program no 11A
  Display the specified output pattern.
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
  for(j=1;j<=i;j++)
    {   printf("\t%d",j);    }
 printf("\n");
 }
getch();
}

Program no 12B
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
  for(j=1;j<=i;j++)
    {   printf("\t*");   }
 printf("\n");
 }
getch();
}

Program no. 13C
 void main()
{
int i,j;
clrscr();
for(i=4;i>0;i--)
{
  for(j=i;j>0;j--)
    {    printf("\t%d",j);   }
 printf("\n");
 }
getch();
}

Program no. 14E
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
 {
  for(j=1;j<=i;j++)
     printf("\t*");
 printf("\n");
 }

for(i=3;i>0;i--)
 {
  for(j=i;j>0;j--)
     printf("\t*");
 printf("\n");
 }
getch();
}

Program 15
WAP to display all prime numbers between 30 and 500. Also count the total number of prime numbers.
void main()
 {
 int i,num,count=0;;
 clrscr();
for(num=30;num<=500;num++)
  {
    i=2;
    while(i<=num-1)
      {
      if(num%i==0)
{      
break;
}
     i++;
      }

if(num==i)
   {
   printf("\t%d",num);
   count++;
   }
}
printf("\nThe number of prime numbers within the range \%d",count);
  getch();
 }

Program no 16
WAP that asks two numbers from key board and find the value one number raised to the power  of another.
void main()
{
int base, power,i,result=1;
clrscr();
printf("\nEnter base then power\n");
scanf("%d%d",&base,&power);
for(i=1;i<=power;i++)
    result*=base;
 printf("\nThe result is\t%d ",result);
 getch();
 }

Program no. 17
WAP to display multiplication table of 1-10.
void main()
{
int i,j;
clrscr();
for(i=1;i<=10;i++)
 {
   for(j=1;j<=10;j++)
     {  printf("%d\t",i*j);   }
 printf("\n");
 }
 getch();
 }

Program no. 18
WAP to calculate sine value of certain degree using sine series.
sin(x)=x-x3/3!+ x5/5!-………….
long int fac(int n)
  {
  int i;
  long int f=1;
  for(i=1;i<=n;i++)
     f*=i;
  return (f);
  }
float sinfun(float x)
   {
    float r,term,y;
    int n=3,i;
    x=(22*x)/(180*7);
    term=x;
    r=x;
    for(i=1;i<10;i++)
      {
       term*=x*x;
       y=term/(fac(n));
       if(i%2!=0)
                y=-y;
              r+=y;
              n+=2;
      }
    return(r);
   }

  void main()
    {
     float x, result;
    clrscr();
    printf("Enter degree\n");
    scanf("%f",&x);
    result=sinfun(x);
    printf("\nValue is%f",result);
     getch();
    }

Program no. 19
WAP that asks an integer number and calculates the sum all digits in the number.
     void main()
     {
     int num,r,q,sum=0;
     clrscr();
     printf("Enter a number\n");
     scanf("%d",&num);
     do
     {
     q=num/10;
     r=num%10;
     num=q;
     sum+=r;
     }while(q!=0);
     printf("\nThe sum of digits is\t%d",sum);
     getch();
     }

Program no. 20
WAP to read 20 numbers and find maximum and minimum number of them.
void main()
  {
  int nums[10],i,max=0,min;
  clrscr();
  printf("Enter 20 numbers\n");
  for(i=0;i<20;i++)
      scanf("%d",&nums[i]);

      for(i=0;i<20;i++)
             { if(max<nums[i])
                 max=nums[i];  }
      min=max;
       for(i=0;i<20;i++)
             {
              if(min>nums[i])
                 min=nums[i];
              }
            printf("\nThe maximum number
                              is%d",max);
            printf("\nThe minimum number is%d",min);
      getch();
     }

Program no. 21
WAP to read array of length N. Display total count for positive, negative numbers and zeros separately.
void main()
  {
  int nums[100],i,n,positives=0,negatives=0,zeros=0;
  clrscr();
  printf("How many numbers do u want?\n");
  scanf("%d",&n);
  printf("\nEnter ur numbers\n");
  for(i=0;i<n;i++)
      scanf("%d",&nums[i]);
      for(i=0;i<n;i++)
             {
              if(nums[i]<0)
                negatives++;
              else if(nums[i]>0)
                positives++;
              else
                zeros++;
             }
printf("\nPositives=%d\tNegatives=%d\t
    Zeros=%d",positives,negatives,zeros);
      getch();
     }

Program no. 22
WAP to read a list of numbers from keyboard. Add even numbers and odd numbers separately and display the result.
void main()
  {
  int nums[100],i,n,evenSum=0,oddSum=0;
  clrscr();
  printf("How many numbers do u
                want?\n");
  scanf("%d",&n);
  printf("\nEnter ur numbers\n");
  for(i=0;i<n;i++)
      scanf("%d",&nums[i]);

      for(i=0;i<n;i++)
             {
              if(nums[i]%2==0)
                evenSum+=nums[i];
              else
               oddSum+=nums[i];
             }

printf("\nThe sum of even
             numbers\t%d",evenSum);
printf("\nThe product of odd
               numbers\t%d",oddSum);
      getch();
     }

Program no. 23
WAP that multiplies each element of a matrix of order m*n by 3.
void main()
{
int m,n,m1[20][20],result,i,j;
clrscr();
printf("\nEnter order of matrix\t");
scanf("%d",&m);
printf("*");
scanf("%d",&n);
printf("\nEnter Your matrix\n");
for(i=0;i<m;i++)
  for(j=0;j<n;j++)
    scanf("%d",&m1[i][j]);

 for(i=0;i<m;i++)
   for(j=0;j<n;j++)
     m1[i][j]=3*m1[i][j];

 for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
     printf("%d\t",m1[i][j]);
   printf("\n");
    }
getch();
}

Program no 24
WAP to raise the power of each element of matrix of order p*q by 2.
void main()
{
int p,q,m1[20][20],result,i,j;
clrscr();
printf("\nEnter order of matrix\t");
scanf("%d",&p);
printf("*");
scanf("%d",&q);
printf("\nEnter Your matrix\n");
for(i=0;i<p;i++)
  for(j=0;j<q;j++)
    scanf("%d",&m1[i][j]);

 for(i=0;i<p;i++)
   for(j=0;j<q;j++)
     m1[i][j]=m1[i][j]*m1[i][j];

 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
     printf("%d\t",m1[i][j]);
   printf("\n");
    }
getch();
}

Program no. 25
WAP to read n numbers in an array. Find the sum of even numbers only and the product of odd numbers only.
void main()
  {
int nums[100],i,n,evenSum=0,
                    oddProduct=1;
  clrscr();
  printf("How many numbers do u
               want?\n");
  scanf("%d",&n);
  printf("\nEnter ur numbers\n");
  for(i=0;i<n;i++)
      scanf("%d",&nums[i]);

      for(i=0;i<n;i++)
             {
              if(nums[i]%2==0)
                evenSum+=nums[i];
              else
               oddProduct*=nums[i];
             }

printf("\nThe sum of even
              numbers\t%d",evenSum);
printf("\nThe product of odd
           numbers\t%d",oddProduct);
      getch();
     }

Program no. 26
WAP to read an array of two dimensions. Then double the content of each element of the array and display the result. Your program must have three separate functions for reading, processing and displaying the array
int i,j;
void readArray(int m, int n, int matrix[20][20])
  {
  for(i=0;i<m;i++)
    for(j=0;j<n;j++)
    scanf("%d",&matrix[i][j]);
  }

void processArray(int m, int n,int matrix[20][20])
   {
      for(i=0;i<m;i++)
       for(j=0;j<n;j++)
            matrix[i][j]=2*matrix[i][j];
   }

void displayArray( int m, int n, int matrix[20][20])
   {
     for(i=0;i<m;i++)
      {
       for(j=0;j<n;j++)
             printf("%d\t",matrix[i][j]);
     printf("\n");
      }
   }

void main()
{
int m,n,m1[20][20];
clrscr();
printf("\nEnter order of matrix\t");
scanf("%d",&m);
printf("*");
scanf("%d",&n);
printf("\nEnter Your matrix\n");
readArray(m,n,m1);
processArray(m,n,m1);
displayArray(m,n,m1);
getch();
  }

Program no. 27
WAP that asks order of two matrixes and read these matrixes. Find multiplication matrix if order of those matrixes is suitable for multiplication.
void main()
{
int matrix1[10][10],matrix2[10][10],
             product[10][10],i,j,k;
int m,n,p,q,partialSum=0;
clrscr();
do
{
 printf("\nEnter order of firt matrix\n");
 scanf("%d%d",&m,&n);
 printf("\nEnter order of second matrix\n");
 scanf("%d%d",&p,&q);
}while(n!=p);
printf("\nEnter elements of first matrix\n");
for(i=0;i<m;i++)
 for(j=0;j<n;j++)
   scanf("%d",&matrix1[i][j]);

printf("\nEnter elements of second
           matrix\n");
for(i=0;i<p;i++)
 for(j=0;j<q;j++)
   scanf("%d",&matrix2[i][j]);

for(i=0;i<m;i++)
  for(j=0;j<p;j++)
  {
    for(k=0;k<n;k++)
      {
      partialSum+=matrix1[i][k]*matrix2[k][j];
      }
    product[i][j]=partialSum;
    partialSum=0;
    }
printf("\nThe product matrix is\n");
for(i=0;i<m;i++)
 {
  for(j=0;j<q;j++)
    printf("\t%d",product[i][j]);
  printf("\n");
  }
getch();
}

Program no. 28
WAP to find out greatest number among twenty numbers using pointer.
void main()
  {
  int nums[10],i,max=0;
  clrscr();
  printf("Enter 20 numbers\n");
  for(i=0;i<20;i++)
      scanf("%d",(nums+i));

      for(i=0;i<20;i++)
             {
              if(max<nums[i])
                 max=*(nums+i);
              }

  printf("\nThe maximum number
                is%d",max);
   getch();
     }
Program no. 29
WAP to test a string for palindrome.
#include<stdlib.h>
void main()
 {
 char st[40];
 int len,i,pal=1;
 clrscr();
 printf("Enter string of ur choice\n");
 gets(st);
 len=strlen(st);
 for(i=0;i<(len/2);i++)
     {
      if(st[i]!=st[len-i-1])
               pal=0;
     }

   if(pal==0)
      printf("\n Not Palindrom");
   else
     printf("\nPalindrom");
     getch();
 }

Program no. 30
WAP to read a string from keyboard and count the vowels, consonants, semicolon and commas separately in that string.
void main()
{
char inputString[200];
int len,i=0,countVowel=0,countCons=0,
     countComma=0,countSemi=0;
clrscr();
printf("Enter ur string of text\n");
gets(inputString);
len=strlen(inputString);
for(i=0;i<len;i++)
{
if(inputString[i]=='a'||inputString[i]=='e'||inputString[i]=='i'||inputString[i]=='o'||inputString[i]=='u')
    countVowel++;
 else if(inputString[i]==',')
  countComma++;
 else if(inputString[i]==';')
 countSemi++;
 else
  countCons++;

}
printf("\nThe number of
          vowels\t%d",countVowel);
printf("\nThe number of
          consonants\t%d",countCons);
printf("\nThe number of 
          commas\t%d",countComma);
printf("\nThe number of
           semicolon\t%d",countSemi);
getch();
}

Structure and union

Program 1:
         Create a structure named student that has name, roll, marks and remarks as members. Assume appropriate types and size of member. Write a program using structure to read and display the data entered by the user.
void main()
  {
    struct student{               {
                        char name[20];
                        int roll;
                        float marks;
                        char remark;
                        };
   struct student s;
   printf("Enter name:\t");
  gets(s.name);
  printf("\nEnter roll:\t");
  scanf("%d",&s.roll);
  printf("\n Enter marks\t");
  scanf("%f",&s.marks);
  printf("Enter remark p for pass or f for fail\t");
  s.remark=getche();
  printf("\n\n\nThe Detail Information is\n");         printf("\Name=%s\tRoll=%d\nMarks=%f\tRemark=%c",s.name,s.roll,s.marks,s.remark);
  getch();
  }

Program 2:
        Modify the above program such that it works to keep records of five students. Use array of structure.
void main()
  {
  struct student {
                       char name[20];
                        int roll;
                       float marks;
                       char remarks;
                        };
  struct student s[5];  int i;
 for(i=0;i<5;i++)
    {
   printf("\n\n\nEnter information of Student [%d]\n",i);
   printf("Enter name:\t");
   scanf("%s",&s[i].name);
   printf("\nEnter roll:\t");
   scanf("%d",&s[i].roll);
   printf("\n Enter marks\t");
   scanf("%f",&s[i].marks);
   printf("Enter remarks p for pass or f for fail\t");
   s[i].remarks=getche();
   }
    printf("\n\nYour Entered Information is:\n");
  for(i=0;i<5;i++)
  {
    printf("\nStudent %d\n",i);      
printf("\Name=%s\tRoll=%d\nMarks=%d\tRemarks=%c",s[i].name,s[i].roll,s[i].marks,s[i].remarks);
    }
    getch();
  }

Program 3:
         Create a structure named mark that has subject and marks as its members. Include this structure as a member for student structure created in program1. Use this structure to read and display student’s name, roll, subject, marks and remarks.
void main()
  {
  struct mark{
               char subject[20];
               float marks;
                };
  struct student {
            char name[20];
            int roll;
            struct mark studentM;
            char remark;
             };
    struct student s;
      printf("Enter name:\t");
      scanf("%s",s.name);
      printf("\nEnter roll:\t");
      scanf("%d",&s.roll);
      printf("\nEnter Subject Name:\t");
      scanf("%s",s.studentM.subject);
      printf("\n Enter marks\t");
     scanf("%f",&s.studentM.marks);
     printf("Enter remark p for pass or f for fail\t");
     s.remark=getche();
    printf("\n\n\nThe Detail Information is\n");
printf("\Name=%s\tRoll=%d\nSubject=%s\tMarks=%f\nRemark=%c",s.name,
                                 s.roll,s.studentM.subject,s.studentM.marks,s.remark);
  getch();

  }

Comments

Popular posts from this blog

What is manipulator in C++?

Answer: Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. Table-2 shows some important manipulator functions that are frequently used. To access these manipulators, the file < iomanip.h > should be included in program.                               Table-2               Manipulators                                                    Equivalent ios function               set...

Network Topology

A network topology is the arrangement of the computers cable and other components on a network. The major goal of network topology is to find out the most economical and efficient way to connect all the users to the network resources while providing adequate capacity to handle users demand, maintain system reliability and decrease the delay.    Topology is the map of physical network. The type of topology we use defect the type and capabilities network hardware’s its management and possibilities for future expansion. There are many types of network topology, some of these are:   Bus Topology Star Topology Mesh Topology   Ring Topology Tree Topology

What is Servlet in Java?

A servlet is a small program that executes on a server. Just as applets dynamically extend the functionality of a web browser, servlets dynamically extend the functionality of a web server. It is helpful to understand that as useful as applets can be, they are just one half of the client/server equation. Not long after the initial release of Java, it became obvious that Java would also be useful on the server side. The result was the servlet. Thus, with the advent of the servlet, Java spanned both sides of the client/server connection.