Monday 6 April 2015

C Basic Programming

Program - 1



Q1. WAP to print “helloo friends…”?

Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
            clrscr();
            printf("helloo friends…");
            getch();
}





























                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

 


Program - 2

Q.2 WAP to print sum of two numbers?

Ans.
                        #include<stdio.h>
#include<conio.h>
void main()
{
                                    int  a, b, sum;
                                    clrscr();
                                    a=10;           
                                    b=20;
                                    sum=a+b;
                                    printf("\nA=10\nB=20\nSum is =%d",sum);
                                    getch();
}
           

 






























Program - 3

Q.3 WAP to Print SI Given Principle=Rs 20000, Rate = 2%, Time = 2 years?

Ans.
            #include<stdio.h>
#include<conio.h>
void main()
{
                                    int p,r,t,SI;
                                    clrscr();
                                    p=20000;
                                    r=2;
                                    t=2;
                                    SI=(p*r*t)/100;
                                   
printf("\nPrinciple=Rs.20,000\nRate=2%\nTime=2yrs");
                                    printf("\nSI=%d",SI);
                                    getch();
}

 





























Program - 4

Q4. WAP to choose a number from the user and add them?

Ans.   #include<stdio.h>
#include<conio.h>
void main()
{
                                    int a,b,sum;
                                    clrscr();

                                    printf("Enter a Number:");
                                    scanf("%d",&a);
                                    printf("Enter another Number:");
                                    scanf("%d",&b);
                                    sum=a+b;

                                    printf("\nSum of Number is :%d",sum);
                                    getch();
}
                       























Program - 5

Q5. WAP to print a program to calculate the Area and Perimeter of Triangle?
 Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
                                                int l,b,area,per;
                                                clrscr();

                                                printf("Enter the Length:");
                                                scanf("%d",&l);
                                                printf("Enter the Breadth:")
                                                scanf("%d",&b);
                                                area=l*b;
                                                per=2*(l+b);
                                                printf("\nArea :%d\nPerimeter :%d",area,per);
                                                getch();
}
























Program - 6

Q6. WAP to swap the given numbers?
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
                                                int a,b,c;
                                                clrscr();

                                                printf("Enter a Number in A :");
                                                scanf("%d",&a);
                                                printf("Enter another Number in B :");
                                                scanf("%d",&b);
                                                c=a;     /*the value of 'a' is stored in 'c'*/
                                                a=b;     /*the value of 'b' is stored in 'a' now*/
                                                b=c;     /*nd value of 'c' is stored in 'b' */

                                                printf("\nNow A is:%d\nAnd B is:%d",a,b);
                                                getch();
}

 

























Program - 7

Q7. WAP to accept the marks of 5 Subjects, find the total and percentage?
Ans.
            #include<stdio.h>
#include<conio.h>
void main()
{
                                    int p,c,m,e,ped,sum,per;
                                    clrscr();

                                    printf("Enter the Marks of Physics:");
                                    scanf("%d",&p);
                                    printf("Enter the Marks of Chemistry:");
                                    scanf("%d",&c);
                                    printf("Enter the Marks of Maths:");
                                    scanf("%d",&m);
                                    printf("Enter the Marks of English:");
                                    scanf("%d",&e);
                                    printf("Enter the Marks of Physical Education:");
                                    scanf("%d",&ped);
                                    sum=p+c+m+e+ped;

                                    printf("\nSum is:%d",sum);
                                    per=(sum)/5;

                                    printf("\nPercentage is:%d",per);
                                    getch();
}















Program - 8

Q8. WAP to print the Gross Salary?
      
ð DA =  40% of  Basic Salary
ð HRA = 20% of Basic Salary
Ans.
                        #include<stdio.h>
#include<conio.h>
void main()
{         
                                                float g,b,d,hra;
                                                clrscr();

                                                printf("Enter the Basic Salary:");
                                                scanf("%f",&b);
                                                d=(40*b)/100;
                                                hra=(20*b)/100;
                                                g=b+d+hra;
                                                printf("Gross salary is :%f", g);
                                                getch();


}













 








Program - 9

Q9. WAP to convert and print this distance in Meters, Feet, Cm and Inches?
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
                                                float d , m,cm,feet,inches;
                                                clrscr();

                                                printf("Enter the Distance:");
                                                scanf("%f",&d);
                                                meter=d*1000;
                                                cm=m*100;
                                                feet=cm/30;
                                                inches=feet*12;

                                                printf("\nMeter=%f",meter);
                                                printf("\nCm=%f",cm);
                                                printf("\nFeet=%f",feet);
                                                printf("\nInches=%f",inches);
                                                getch();
}

                           


















Program - 10

Q10. WAP to calculate, area and perimeter of rectangle?
Ans.
            #include<stdio.h>
#include<conio.h>
void main()
{
                                    float l,b,area,pm,r,Area,c,pi;
                                    clrscr();

                                    printf(“Enter the Length:”);
                                    scanf(“%f”,&l);
                                    printf(“Enter The Breadth:”);
                                    scanf(“%f”,&b);
                                    area=l*b;

                                    pm=2*(l+b);

                                    printf(“\nThe Area is :%f”,area);
                                    printf(“\nPerimeter:%f”,pm);
                                    printf(“\n\nEnter the Radius”);
                                    scanf(“%f”,&r);
                                    pi=3.14;
                                    Area=pi*(r*r);
                                    c=2*pi*r;
                                    printf(“\nThe area of Circle :%f”,Area);
                                    printf(“\nCircumfrence :%f”,c);
                                    getch();


}













Program - 11

Q11. WAP to find the largest of three numbers?
Ans.
            #include<stdio.h>
#include<conio.h>
void main()
{
            int a,b,c;
            clrscr();
            printf(“Enter the values”);
            scanf(“%d%d%d”, &a, &b, &c);
{          if((a>b)&&(a>c))
            printf(“%d”, a);
}
{          else if((c>b)&&(c>a))
            printf(“%d”, c);
}
{          else
            printf(“%d”, b);
}
getch();
}
 
























Program - 12

Q12. WAP to find out whether a given number is even or odd?
Ans. 
#include<stdio.h>
#include<conio.d>
void main()
{
int a ;
clrscr();

printf("Enter the no.: " );
scanf("%d",&a);
                        {          if (a%2=0)
                                                printf("Its Even ");
                                                else
                                                printf("Its odd");
                                    }
                         getch();
                         }

 





























Program - 13

Q13. WAP to find out whether it’s a leap year or not?
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int yrs ;
clrscr();

printf(“Enter a Year: “ );
scanf(“%d”,&yrs);
                                    if (yrs%4==0)
                                    printf(“It’s a Leap Year “);
                                    else
                                     printf(“Its Not a Leap  year “);

                         getch();
                         }

 



























Program - 14

Q14. The marks obtained by a student in five subjects are input through the keyboard. The student gets a division as per following rules:
ð Percentage above or equal to 60 – First Division
ð Percentage above or equal to 50 – Second Division
ð Percentage above or equal to 40 – Third Division
ð Percentage less than 40 – Fail
WAP to calculate the division obtained by the student.
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int p,c,m,e,biotech, sum,per ;
clrscr();

printf("Enter the marks of Physics: " );
scanf("%d",&p);
printf("Enter the marks of Chemistry: " );
scanf("%d",&c);
printf("Enter the marks of Maths: " );
scanf("%d",&m);
printf("Enter the marks of English : " );
scanf("%d",&e);
printf("Enter the marks of biotech: " );
scanf("%d",&biotech);

sum=p+c+m+e+ped;
per=sum/5;

                  if (per>=60)
                                    printf("Ist Division ");

                                     else if (per>=50)    /*if(per>=50 && per<=59)*/
                                    printf("IInd Division ");

                                     else if (per>=40)
                                     printf("IIIrd Division");

                                     else
                                    printf("Fail");
                         getch();
}

Program - 15

Q15. A three-digit number is entered through the keyboard. WAP to obtain the reversed number and to determine whether the original number and reversed number are equal or not.
      Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,num1,a,b,c,rev;
clrscr();

printf("Enter the Number: " );
scanf("%d",&num);
a=num%10;
num=num/10;
                                    b=num%10;
                                    rev=a*100+b*10+num;
                        if (num1==rev)
                                    printf("Its equal");

                                    else
                                    printf("No its not equal");

                         getch();


}
















Program - 16

Q16. WAP to find the factors value of any no. ?
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int fact=1 ,num;
clrscr();

printf("Enter the Number: " );
scanf("%d",&num);
for(;num>0;num--)
fact=fact*num;
printf("Factorial=%d ",fact);
                         getch();
}

























 

Program - 17

Q17. WAP to find the factorial value of any number enter to the keyboard?

Ans  
#include<stdio.h>
#include<conio.h>
void main()
{
int number, factorial=1;
clrscr();
printf("enter the number");
scanf("%d",&number);
while (number>0)
{
factorial=factorial*number;
number--;
}
printf("factorial of a number=%d",factorial);
for(factorial=1, number>0,number--)
factorial=factorial*number;
getch();
}

 


















 



 

Program - 18

Q18. Ten numbers are entered from the keyboard into an array. Write programs to find out how many are even numbers and how many are odd?


Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,count,count1;
clrscr();
printf("entre ten numbers");
for(i=0;i<=9;i++)
{
scanf("%d",&a[i]);
}

if(a[i]%2==0)
{
count++;
}
else
{
count1++;
}
printf("total even numbers are %d",count);
printf("total odd numbbers are %d", count1);
getch();
}












Program - 19

Q19. WAP  to add Two 3X3 matrix ?
Ans.
            #include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3],sum[3][3] ;
                        int i ,j ;
clrscr();

printf("Enter the Matrix ");

for(i=0;i<3;i++)
{
            for(j=0;j<3;j++)
            {
            scanf("%d", & a[i][j]);

            }
 }

            printf("enter another Matrix ");
            for(i=0;i<3;i++)
            {
             for(j=0;j<3;j++)
             {  scanf("%d",& b[i][j]);

                         }
               }

               for(i=0;i<3;i++)
               {
                         for(j=0;j<3;j++)
                         {
                        sum[i][j]=a[i][j]+b[i][j];

                         }
               }

               printf("Sum of the Two Matrix is : ");
               for(i=0;i<3;i++)
             {
              for(j=0;j<3;j++)
              {
               printf("%d",sum[i][j]);
               printf("  ");
               }
                        printf("\n");

               }
                        getch();
            }

 









































 

Program - 20

Q20. WAP to obtain transpose of 3x3 matrix ?
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3],sum[3][3] ;
 int i ,j ;
            clrscr();

printf("Enter the Matrix ");

for(i=0;i<3;i++)
{
                        for(j=0;j<3;j++)
            {
            scanf("%d", & a[i][j]);

            }
 }


  for(i=0;i<3;i++)
            {
             for(j=0;j<3;j++)
             {
               printf("%d",a[j][i]);
               printf(" ");

               }
             printf("\n");
             }


                        getch();
            }







Program - 21

Q21. WAP to Multiply two 3x3 Matrix ?
Ans 
#include<stdio.h>
#include<conio.h>
void main()
{
 int i,j,k, a[3][3] , b[3][3] ,  c[3][3] ;
clrscr();

printf("Enter the Matrix ");

for(i=0;i<3;i++)
{
                        for(j=0;j<3;j++)
            {
            scanf("%d", & a[i][j]);

            }
 }
 printf("Enter Another Matrix :");

  for(i=0;i<3;i++)
            {
             for(j=0;j<3;j++)
             {
               scanf("%d",& b[i][j]);

                          }
               }

            for(i=0;i<3;i++)
             {
               for(j=0;j<3;j++)
                        {
                           c[i][j]=0;
                           for(k=0;k<3;k++)
                           {
                                    c[i][j]=c[i][j]+a[i][k]*b[k][j];

                                    }
             printf("%d", c[i][j]);
             }
              printf("  ");
              printf("\n");

               }

                        getch();
            }





































Program - 22

Q22. WAP to calculate area of a circle using function?
Ans.
#include<stdio.h>
#include<conio.h>
int area(int);
void main()
{
float  r ,a;
            clrscr();

            printf("Enter the radius  ");
            scanf("%f", &r);
            a=area(r);

            printf("Area=%f ",a);
            getch();
            }
            int area(int r )
                                    {  int c;
                                                c= 3.14*r*r;
                                                              return(c);
                                                  }





















Program - 23
Q23. Wap to enter 10 no.s and print smallest and largest?
Ans
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,large=0,small=10;
clrscr();

            printf("\nEnter 10 numbers:  ");

            for (i=0;i<10;i++)
   {       scanf("%d", & a[i] );

             }
              for(i=0;i<10;i++)
            {
             if(a[i]>large)
             large=a[i];
            // }
             //for(i=0;i<10;i++)
   //     {
             if(a[i]<small)
             small=a[i];
             }

               printf("Largest: %d",large);
               printf("\nSmallest: %d ",small);


               getch();
               }











Program – 24
Q24. WAP to ask and display details of 10 employees using structures.
Ans.
         #include<conio.h>
#include<stdio.h>
void main()
{  
int i;
         struct emp
{ char empN[15];
int empID;
char empD[10];
long int empcon;
char empHno[50];
} emp[10];

clrscr();

for(i=0; i<10; i++)
{
scanf ("%s%s%s%ld%d", &emp[i].empN, &emp[i].empID, &emp[i].empD, &emp[i].empcon, &emp[i].empHno);
}

for(i=0;i<10;i++)
{ printf("%d%s%s%ld%s The display of 10 employees are:",  emp[i].empN, emp[i].empID, emp[i].empD, emp[i].empcon, emp[i].empHno);
}

getch();
}













Program – 25
Q25. WAP to find out whether the string is a palindrome or not.
Ans.
            #include<conio.h>
#include<stdio.h>
   void main()
         {
char ch;
clrscr();
char ch[20], ch1[20];
int i,j;

puts("Enter the character");
gets(ch);
i=len;
len=strlen(ch);

for(i=len;i>=0;i--)
{
ch[j]=ch[i];
j++;
}

i=strcmp(ch,ch1)

if (i==0)
printf("string is pallindrone");

else
printf("string is not pallindrone");
getch();
}












Program – 26
Q26. WAP to change the case of a character.
Ans.
            #include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter Alphabet");
ch=getche();
printf("%d", ch);
getch();
}






























Program – 27
Q27. WAP to search an element in an array.
Ans.
            #include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
ch=getche();

if(ch>=65 && ch<=90)
{
ch=ch+32;
printf("%c", ch);
}

else
{
ch=ch-32;
printf("%c", ch);
}

getch();
}



















Program – 28
Q28. WAP to accept a single input character and check if it is an upper case digit, lower case digit or a special symbol.
Ans.
            #include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
puts("Enter The character");
ch=getche();

if(ch>= 'A' && ch<= 'Z')
puts("Uppercase Letter");

else if(ch>= 'a' && ch<= 'z')
puts("Lowercase Letter");

else if(ch>= '0' && ch<= '9')
puts("Digit");

else
puts("Special symbol");

getch();
}

















Q29. WAP to perform a pointer function.
Ans.
         #include<stdio.h>
#include<conio.h>
void main()
{
int i=10;
int *j;
clrscr();
j=&i;
printf("%u", &i);
printf("\n%u", j);
printf("\n%d", i);
printf("\n%u", &i);
printf("\n%d", *(&i));
printf("\n%d", *j);
getch();
}


























Q30. WAP tp print the address of a value and to print the next address of it.
Ans.
         #include<stdio.h>
#include<conio.h>
void main()
{
int i=3, *x;
clrscr();
x=&i;
printf("\n%u", x);
x++;
printf("\n%u", x);
getch();
}





























Q31. WAP to find the address and value of a program by using printers and to perform functions on it.
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num[] = {24,34,12,44,56,17};
clrscr();
for(i=0; i<=5; i++)
{
printf("\n%u", &num[i]);
printf("\n%u\t%d", &num[i], *(num+i));
printf("\n%d\t%d", *(i+num), i[num]);
}
getch();
}


No comments:

Post a Comment