Basics of c programming

                                        









     Program in c language

Chapter 1

Included parts:

a. over view of C...…


b. Constants, Variable and Data Types...…..


c. Operators And Expressions...…


d. Managing Input and Output Operations...…..


e. Decision Making and Branching...…


f. Decision Making and Looping...…..



























       


Starting of program

WAP=(Write a program)

Q1. WAP a program to print your name.

//Starting of a program
#include<stdio.h>
#include<conio.h>
int main()
{
    printf("prajwol Shrestha");
   
    return 0;
}

Some basic questions

Q2.WAP that converts centigrade into Fahrenheit.

#include<stdio.h>
#include<conio.h>
int main()
{
int c,f;
    printf("enter the centigrade value:");
    scanf("%d",&c);
    f=c*9/5+32;
    printf("the farhenhite value is %d",f);
    return 0;
}


Q3.WAP that prints the perimeter of circle.

#include<stdio.h>
#include<conio.h>
int main()
{
int l,b,p;
    printf("enter the length and breath of rectangle:");
    scanf("%d%d",&l,&b);
    p=2*(l+b);    
    printf("the perimeter of rectangle is%d\n",p);
    return 0;
}



Q4.WAP that takes hours and minutes as input and calculate the total no of second.

#include<stdio.h>
#include<conio.h>
int main()
{
int s,h,m;
    printf("enter the hour  and minutes:");
    scanf("%d%d",&h,&m);
    s=(h*60*60)+(m*60);    
    printf("equivalent second is%d\n",s);
    return 0;
}


Q5.WAP that takes the minute as input and display the total no. of hours and seconds.

#include<stdio.h>
#include<conio.h>
int main()
{
int s,h,m;
    printf("enter the  minutes:");
    scanf("%d",&m);
        h=m/60;
        s=m*60;
    printf("hour=%d\nsecond=%d",h,s);
    return 0;
}

  

Q6.WAP to find the area of triangle if three sides are given.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int s,a,b,c,area,g;
printf("enter the three sides of traingle:");
scanf("%d%d%d",&a,&b,&c);
s=a+b+c/2;
area=s*(s-a)*(s-b)*(s-c);
g=pow(area,0.5);
printf("area of triangle is %d",area);
return 0;
}


Q6. WAP to calculate the simple interest.



#include<stdio.h>
#include<conio.h>
int main()
{
int interest,p,t,r;
printf("enter the principle,time and rate:");
scanf("%d%d%d",&p,&t,&r);
interest=p*t*r/100;
printf("simple interest is %d",interest);
return 0;
}


Q7. WAP to display the size of different data types.

#include<stdio.h>
#include<conio.h>
int main()
{
    printf("int=%d\n",sizeof(int));
    printf("float=%d\n",sizeof(float));
    printf("int=%d\n",sizeof(long int));
    printf("char=%d\n",sizeof(char));
    printf("int=%d\n",sizeof(double));
return 0;
}




#Question related if and else statements.

Q8.WAP to accept two integer and check whether they are equal or not.

#include<stdio.h>
#include<conio.h>
int main()
{
    int num1,num2;
    printf("enter the first number:");
    scanf("%d",&num1);
    printf("enter the second number:");
    scanf("%d",&num2);
    if(num1==num2)
    printf("Given two integer is equal");
    else
    printf("not equal");
return 0;
}


Q9.WAP to check that given character is an alphabet , digit or special number.



#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    printf("enter the character:");
    scanf("%c",&ch);
    if(ch>='A'&&ch<='z' || ch>='a'&&ch<='z')
    {
        printf("given character is alphabet");
    }
    else if(ch>='0' && ch<='9')
    {
        printf("given character is a digit");
    }
    else
    {
        printf("special character");
    }
return 0;
}


Q10.WAP to find the largest among the three number.



#include<stdio.h>
#include<conio.h>
int main()
{
   int num1,num2,num3;
 printf("enter the three number:");
 scanf("%d%d%d",&num1,&num2,&num3);
 if(num1>num2 && num1>num3)
 {
    printf("num1 is greatest number");
 }
 else if(num2>num1 && num2>num3)
 {
    printf("num2 is greatest number");

 }
 else
 {
    printf("num3 is greatest number");
 }
return 0;
}


Q10.WAP to check whether you are eligible for voting or not.



#include<stdio.h>
#include<conio.h>
int main()
{
   int age;
   printf("enter the age:");
   scanf("%d",&age);
   if(age>=18)
   {
    printf("eligible for voting");
   }
   else
   {
    printf("not eligible");
   }

return 0;
}

 

Q11.WAP to check given year is leap year or not.

#include<stdio.h>
#include<conio.h>
int main()
{
  int year;
  printf("enter the year:");
  scanf("%d",&year);
  if(year%4==0)
  {
    if(year%100==0)
    {
      if(year%400==0)
      {
        printf("leap year");
      }
      else
      {
        printf("not a leap year");
      }
    }
    else
    {
      printf("leap year");
    }
   
  }
  else
  {
    printf("not a leap year");
  }
  return 0;
}


Q12. WAP to check whether a given character is a vowel or not.

#include<stdio.h>
#include<conio.h>
int main()
{
  char ch;
  printf("enter the character:");
  scanf("%c",&ch);
  if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
  {
    printf("vowel letter");
  }
  else
  {
    printf("consonant letter");
  }
  return 0;
}


Q13.WAP to accept a coordinate of x-y coordinate system and determine in which quadrant the coordinate lies.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a,b;
  printf("enter XY coordinate:");
  scanf("%d%d",&a,&b);
  if(a>0 && b>0)
  {
    printf("given coordinate lies on 1st quadrant");
  }
  else if(a<0 && b>0)
  {
  printf("given coordinate lies on 2nd quadrant");
  }
  else if(a<0 && b<0)
  {
  printf("given coordinate lies on 3rd quadrant");
  }
  else if(a>0 && b<0)
  {
    printf("given coordinate lies on 4th quadrant");
  }
  else
  {
    printf("given coordinate lies on origin");
  }
 return 0;
}


Q14.WAP to check whether triangle is equilateral, isoceles or scalane.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a,b,c;
 printf("enter the three sides of triangle:");
 scanf("%d%d%d",&a,&b,&c);
 if(a==b==c)
{
  printf("equilateral triangle");
}
 else if(a==b||b==c||c==a)
 {
  printf("isoceles triangle");
 }
 else
 {
  printf("scalane triangle");
 }
 return 0



#looping related problems.

Q15. WAP to find sum of first 10 natural number.

#include<stdio.h>
#include<conio.h>
int main()
{
  int sum=0;
 for(int i=1;i<=10;i++)
 {
   sum=sum+i;
 }
 printf("sum of 10 natural number is %d",sum);
 return 0;
}


Q16.WAP to read 10 number and find their sum and average.

#include<stdio.h>
#include<conio.h>
int main()
{
 float sum=0,i,avg;
 float a;
 printf("enter the 10 numbers:");
 for(i=0;i<10;i++)
 {
  scanf("%f",&a);
  sum=sum+a;
  }
 avg=sum/10;
 printf("sum of given 10 number is %2f\n",sum);
 printf("average of 10 number is %2f\n",avg);
return 0;
}


Q17.WAP to display the multiplication to be of a given integer.



#include<stdio.h>
#include<conio.h>
int main()
{
  int m=1,i,n;
  printf("enter the number:");
  scanf("%d",&n);
  printf("multiplication of no %d is\n",n );
  for(i=1;i<=10;i++)
  {
   m=n*i;
   printf("%d*%d=%d\n",n,i,m);
  }

return 0;
}


Q18.WAP to display n terms of odd number and their sum .

#include<stdio.h>
#include<conio.h>
int main()
{
 int n,i,j,sum=0;
 printf("enter the number to be displayed:");
 scanf("%d",&n);
 printf("odd numbers are");
 for(i=1;i<=n;i++)
{
  printf("%d\n",2*i-1);
  sum=sum+2*i-1;
}
printf("sum 0f all odd numbers upto %d term is %d",n,sum);
return 0;
}


Q19.WAP to calculate factorial of given number.

#include<stdio.h>
#include<conio.h>
int main()
{
 int n,fact=1;
 printf("enter a number:");
 scanf("%d",&n);
 for(int i=1;i<=n;i++)
 {
  fact=fact*i;
 }
 printf("factorial of no. %d is %d",n,fact);

return 0;
}


Q20.WAP to display the Fibonacci series upto nth term.

#include<stdio.h>
#include<conio.h>
int main()
{
 int i,c,a=0,b=1,n;
 printf("enter the no of terms...");
 scanf("%d",&n);
 printf("Fibonacci series up to %d term are\n",n);
 for(i=0;i<n;i++)
 {
  printf("%d ",a);
  c=a+b;
  a=b;
  b=c;
 }
 return 0;
}


#using while loop..

Q21.WAP that input a number until user says no using while loop.

#include<stdio.h>
#include<conio.h>
int main()
{
 char ch;
 int n;
 while(1)
 {
  printf("enter the number:");
  scanf("%d",&n);
  fflush(stdin);
  printf("do you want to enter another no(yes/no)?");
  scanf("%c",&ch);
  if(ch=='n'||ch=='N')
  {
    break;
  }

 }
 return


#using do while loop.

Q22.WAP that prints all alphabets.

#include<stdio.h>
#include<conio.h>
int main()
{
   char ch='a';
   do
   {
      printf("%c\n",ch);
      ch++;
   } while (ch<='z');

   return 0;
}



#using switch case.

Q23.WAP that prints the days.

#include<stdio.h>
#include<conio.h>
int main()
{
   int n;
   printf("enter the number:");//sunday=1,monday=2,tuesday=3,wednesday=4,thursday=5,friday=6,saturday=7
   scanf("%d",&n);
   switch (n)
   {
   case 1 :printf("sunday");
   break;
   case 2 :printf("Monday");
   break;
   case 3:printf("Tuesday");
   break;
   case 4 :printf("Wednesday");
   break;
   case 5 :printf("Thursday");
   break;
   case 6 :printf("Friday");
   break;
   case 7 :printf("saturday");
   break;
   default:printf("you are form other world :)....not a valid day!!");
   break;
   }

   return 0;
}






























































































































Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.