Learn C in Easy Way

/* Area of Circle : declare const pi=3.14*/
#include<stdio.h>
#include<conio.h>
const float pi=3.14;
void main()
{
float area,rad;
clrscr();
printf("\n Enter Value of Radius:");
scanf("%f",&rad);
area=pi*rad*rad;
printf("\n Area of Circle:%f",area);
getch();
}
-------------------------------------------------------------------------------------------------------------------------
/* Conversion of Celcius to Farehenite c=(f-32)/18 */
#include<stdio.h>
#include<conio.h>
void main()
{       float cel,farh;
    clrscr();

   printf("\n Enter the Temp in Farhenite:");
   scanf("%f",&farh);
   cel=(farh-32)/1.8;
   printf("\n Farhenite %f \n Celcius %f",farh,cel);
   getch();

}
----------------------------------------------------------------------------------------------------------------------
/* spliting number into digit*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num,digit;
clrscr();
printf("Enter any num:");
scanf("%d",&num);
while(num>0)
    {  digit=num%10;
        num=num/10;
        printf("\t %d",digit);
    }
    getch();
}
----------------------------------------------------------------------------------------------------------------------------
/* find Average of Three No's */
#include<stdio.h>
#include<conio.h>
void main()
{
  float n1,n2,n3;
  float avg;
  clrscr();
  printf("\n Enter Three Numbers :");
  scanf("%f%f%f",&n1,&n2,&n3);
  avg=(n1+n2+n3)/3;
  printf("Average of \t%f\t%f\t%f is : %f",n1,n2,n3,avg);
  getch();
}
----------------------------------------------------------------------------------------------------------------------------
/* finding Ineterest Amount */
#include<stdio.h>
#include<conio.h>
#define int_rate 10
void main()
{   float PA,period,int_amt;
    clrscr();
    printf("\n Enter Principle Amount anf Period in Years");
    scanf("%f%f",&PA,&period);
    int_amt=(PA*int_rate/100)*period;
    printf("\ Interest Amount for PA: %f for %f Years is : %f",PA,period,int_amt);
    getch();

}
------------------------------------------------------------------------------------------------------------------------
/* swapping of variables without using third variable */
#include<stdio.h>
#include<conio.h>
void main()
{
  int a,b;
  clrscr();
  printf("\n Enter values of a and b:");
  scanf("%d%d",&a,&b);
  printf("\n Values before swapping  a: %d \t b: %d",a,b);
  a=a-b;
  b=a+b;
  a=b-a;
  printf("\n Values before swapping  a: %d \t b: %d",a,b);
  getch();

}
---------------------------------------------------------------------------------------------------------------------------
/* Area and Perimeter of Rectangle */
#include<stdio.h>
#include<conio.h>
void main()
{
    float len,br,area,peri;
    clrscr();
    printf("\n Enter Length and Breadth of Rectangle:");
    scanf("%f%f",&len,&br);
    area=len*br;
    peri=2*(len+br);
    printf("\n Area of Rectangle:%.2f",area);
    printf("\n Perimeter of Ractangle%.2f",peri);
    getch();

}
-------------------------------------------------------------------------------------------------------------------------
/* find maximum of two numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
    int no1,no2,max;
    clrscr();
    printf("\n Enter two numbers:");
    scanf("%d%d",&no1,&no2);
    max=(no1>no2?no1:no2);
    printf("Greater Number is %d",max);
    getch();
}
-------------------------------------------------------------------------------------------------------------------------
/* swapping of two numbers using pointers */
void main()
{
 int x,y,*a,*b,temp;
 clrscr();
 printf("\n Enter the value of x and y:");
 scanf("%d%d",&x,&y);
 printf("\n values of X and Y before swapping X:%d Y:%d",x,y);

 a=&x;
 b=&y;
 temp=*b;
 *b=*a;
 *a=temp;
 printf("\n values of X and Y after swapping X:%d Y:%d",x,y);
 getch();
}
---------------------------------------------------------------------------------------------------------------------
/* find Employee Salary */
#include<stdio.h>
#include<conio.h>
void main()
{
  float basic,hra,da,ta,sal;
  clrscr();
  printf("\n Enter Basic of Employee:");
  scanf("%f",&basic);
  hra=(50*basic)/100;
  da=(25*basic)/100;
  ta=(5*basic)/100;
  sal=basic+hra+da+ta;
  printf("\n Total Salary: %f",sal);
    printf("\n Hra: %f",hra);
      printf("\n Da: %f",da);
    printf("\n ta: %f",ta);
  getch();
}
-----------------------------------------------------------------------------------------------------------------------------
/* Converting no to Hex Oct Dec */
#include<stdio.h>
#include<conio.h>
void main()
{
    int number;
    clrscr();
    printf("\n Enter Number to Convert:");
    scanf("%d",&number);
    printf("Number:%d \nHex:%x \nOctal:%o \nDecimal:%d",number,number,number,number);
    getch();
}
--------------------------------------------------------------------------------------------------------------------------------
/* Number with leading and Trailing Zeros */
#include<stdio.h>
#include<conio.h>
void main()
{
    int number;
    clrscr();
    printf("\n Enter Number:");
    scanf("%d",&number);
    printf("\n Number with leading Zeros:%05d ",number);
    printf("\n Number with Trailing Zeros:%d ",number*100);
    getch();
}
------------------------------------------------------------------------------------------------------------------------------
/* Number with Right and Left Justification */
#include<stdio.h>
#include<conio.h>
void main()
{
    int num;
    clrscr();
    printf("\n Enter the Number");
    scanf("%d",&num);
    printf("\n The Right Justification : %5d",num);
    printf("\n The Left Justification : %-5d",num);
    getch();
}
---------------------------------------------------------------------------------------------------------------------------------
/* Interchange content of Two Varialbes -Swapping */
#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b,temp;
    clrscr();
    printf("\n Enter value for a and b:");
    scanf("%d%d",&a,&b);
    printf("\nBefore Swap a:%d \t b=%d",a,b);
    temp=a;
    a=b;
    b=temp;
    printf("\nAfter Swap a:%d \t b=%d",a,b);
    getch();
}
------------------------------------------------------------------------------------------------------------------------------
/* find Squre and cube of Number */
#include<stdio.h>
#include<conio.h>
void main()
{    float num,sqr,cube;
     clrscr();
     printf("\n Enter Number:");
     scanf("%f",&num);
     sqr=num*num;
     cube=num*num*num;
     printf("Square of number:%f\nCube of number:%f",sqr,cube);
     getch();

}
-----------------------------------------------------------------------------------------------------------------------------
/* Converting Lowercase to Uppper*/
#include<stdio.h>
#include<conio.h>
void main()
{    char upp,low,temp;
    clrscr();
    printf("\n enter Character in Lower case:");
    scanf("%c",&low);
    printf("\n Ascii Vale of %c\t %d",low,low);
    temp=low-32;
    printf("\nAscii Value of : %c \t %d",temp,temp);
    getch();

}
---------------------------------------------------------------------
/* Accept string from user and display only first 7 character */
#include<stdio.h>
#include<conio.h>
void main()
{
   char name[15];
   clrscr();
   printf("\n Enter any String");
   scanf("%s",&name);
   printf("First 7 Charactes of String is: %.7s",name);
   getch();

}}
--------------------------------------------------------------------
/* Type Casting of int to float and vice versa */
#include<stdio.h>
#include<conio.h>
void main()
{    int a,ci;
    float b,cf;
    clrscr();
    printf("\n Enter values of a:");
    scanf("%d",&a);
    printf("\n Enter Value of b:");
    scanf("%f",&b);
    ci=(int)b;
    cf=(float)a;
    printf("\n Casted Inter as Float %f",cf);
    printf("\n Casted Float as Integer %d",ci);
    getch();

}
-------------------------------------------------------------------
/* increment and decrement with assignment operator*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int i=5,j=10;
    clrscr();
    printf("\n Increment Operator %d",++i);
    printf("\nIncrement Operator %d",i++);
    printf("\nDecrement Operator %d",--j);
    printf("\nDecrement Operator %d",j--);
    getch();

}
---------------------------------------------------------------------
/* Odd or Even */
#include<stdio.h>
#include<conio.h>
void main()
{    int n;
    signed int n1;
    clrscr();
    printf("\n Enter any number:");
    scanf("%d",&n);
    printf("\n Enter any number:");
    scanf("%d",&n1);
    if(n1>0)
    {
        printf("\n Positive");
    }
    else
    {
        printf("\n Negative");
    }

    if(n%2==0)
    {
        printf("\n Number is Even");
    }
    else
    {
        printf("\n Number is Odd");
    }
    getch();
}
-------------------------------------------------------------------------
/* Accept Character from user and check is it vowel or constant */
#include<stdio.h>
#include<conio.h>
void main()
{
 char code;
 clrscr();
 printf("Enetr any Character :");
 scanf("%c",&code);
 if(code=='a' || code=='e' || code=='i' || code=='o' || code=='u')
 {
    printf("you have Entered Vowel: %c",code);
 }
 else
 {      printf("you have entered constant: %c",code);
 }
 getch();
}
---------------------------------------------------------------------
/* Switch Case */
#include<stdio.h>
#include<conio.h>
void main()
{
    int choice,n,temp;
    clrscr();
    printf("\n Enter Your choice:");
    printf("\n 1.Square of number:");
    printf("\n 2. Cube of number:");
    printf("\n 3. Octal number:");
    printf("\n 4. Hexadecimal number:");
    scanf("%d",&choice);
    printf("\n Enter Number:");
    scanf("%d",&n);
    switch(choice)
    {
        case 1:
              temp=n*n;
              printf("\n Square : %d",temp);
              break;
        case 2:
              temp=n*n*n;
              printf("\n cube:%d",temp);
              break;
        case 3:
            printf("\n Octal :%o",n);
            break;
        case 4:
            printf("Hexadecimal:%x",n);
            break;
    }
    getch();
}
--------------------------------------------------------------------
/* Accept 3 Numbers and find largest */
#include<stdio.h>
#include<conio.h>
void main()
{
    int n1,n2,n3;
    clrscr();
    printf("\n Enter Three numbers:");
    scanf("%d%d%d",&n1,&n2,&n3);
    if(n1>n2 && n1>n3)
    {
    printf("\nn1 is Greater ");
    }
    else if(n2>n1 && n2>n3)
    {
    printf("\n n2 is greater");
    }
    else if(n3>n1 && n3>n2)
    {
    printf("\n n3 is greater");
    }
    else
    {
    printf("All are euqal");
    }
  getch();
}
-------------------------------------------------------------------------

/* Accept Marks and display user has got which grade */
#include<stdio.h>
#include<conio.h>
void main()
{
    int marks;
    clrscr();
    printf("\n Enter your Marks(Percentage)");
    scanf("%d",&marks);
    if(marks>0 && marks<50)
    {
        printf("\n Fail");
    }
    else if(marks>50 && marks<=55)
    {
        printf("\n Pass Class");
    }
    else if(marks>55 && marks<=59)
    {
        printf("\n Second Class");
    }
    else if(marks>60 && marks<70)
    {
        printf("\n First class");
    }
    else if(marks>=70 && marks<=80)
    {
        printf("\n Distinction");
    }
    else if(marks>80 && marks<=100)
    {
        printf("You are in Merit");
    }
      getch();
}
--------------------------------------------------------------
/* print Username 5 times using while */
#include<stdio.h>
#include<conio.h>
void main()
{
    char name[10];
    int i=0;
    clrscr();
    printf("Enter User Name:");
    scanf("%s",&name);
    while(i<5)
    {
         printf("Your Username : %s",name);
         i++;
    }
    getch();
}
-----------------------------------------------------------
/* print Username 5 times using while */
#include<stdio.h>
#include<conio.h>
void main()
{
    char name[10];
    int i=1;
    clrscr();
    printf("\n Enter User Name:");
    scanf("%s",&name);
    while(i<=5)
    {
         printf("\nYour Username : %s",name);
         i++;
    }
    getch();
}
----------------------------------------------------------------
/* print Odd numbers in 1 To 10 using while */
#include<stdio.h>
#include<conio.h>
void main()
{
    int i=1;
    clrscr();
    while(i<=10)
    {
        if(i%2!=0)
        {
            printf("\t %d",i);
        }
        i++;
    }
    getch();
}
-------------------------------------------------------------
/* Factorial of Given Number */
#include<stdio.h>
#include<conio.h>
void main()
{
    int i=1,no,fact=1;
    clrscr();
    printf("\n Enter Any no");
    scanf("%d",&no);
    while(i<=no)
    {
        fact=fact*i;
        i++;
    }
    printf("\n Factorial of No is : %d",fact);
    getch();

}
---------------------------------------------------------
/* Program to Count no of digits in number */
#include<stdio.h>
#include<conio.h>
void main()
{
    int no,cnt=0,rem;
    clrscr();
    printf("\n Enter Any number");
    scanf("%d",&no);
    while(no!=0)
    {    rem=no%10;
    printf("\n Rem:%d",rem);
         no=no/10;
         cnt++;
    }
    printf("\n Count of Digits: %d",cnt);
    getch();

}
-----------------------------------------------------------
/* Program to Count no of digits in number */
#include<stdio.h>
#include<conio.h>
void main()
{
    int no,cnt=0,rem,sum=0;
    clrscr();
    printf("\n Enter Any number");
    scanf("%d",&no);
    while(no!=0)
    {    rem=no%10;
         printf("\n Rem:%d",rem);
         sum=sum+rem;
         no=no/10;
         cnt++;
    }
    printf("\n Count of Digits: %d Sum:%d",cnt,sum);
    getch();

}
---------------------------------------------------------------
/* Armstrong Number sum of cubes of digit is equal to number */
#include<stdio.h>
#include<conio.h>
void main()
{
    int num,temp,sum=0,rem;    //remainder for digits sum for their sum
    clrscr();
    printf("\nEnter Number:");
    scanf("%d",&num);
    temp=num;
    while(num!=0)
    {
        rem=num%10;    //remainder
        sum=sum+(rem*rem*rem);
    printf("\n Rem:%d",rem);
    printf("\n Sum:%d",sum);
        num=num/10;
    }
    if(sum==temp)
    {
    printf("\n This is Armstrong Number");
    }
    else
    {
    printf("\n This is not Armstrong Number");
    }
    getch();
}
-------------------------------------------------------------
/* Program of Table of Given Number*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int no,i=1;     //i initialised
    clrscr();
    printf("\n Enter Number:");
    scanf("%d",&no);
    do
    {       printf("\t %d",no*i);
        i++;

    }while(i<=10);
    getch();

}
---------------------------------------------------------------
/* program to display multiples 7 14 21 28
of 7 from 1 to 50 */

#include<stdio.h>
#include<conio.h>
void main()
{
    int i=1;    // i initialised
    clrscr();
    do
    {
        if(i%7==0)
        {
        printf("\n %d",i);
        }
        i++;
    }while(i<=50);
    getch();
}
-----------------------------------------------------------
/* for loop for Even number from 1 to 100 */
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,sum=0;
    clrscr();
    for(i=0;i<=100;i++)
    {
        if(i%2==0)
        {
        printf("\t %d",i);
        sum=sum+i;
        }
    }
    printf("\n Sum of Even Numbers %d",sum);
    getch();
}
------------------------------------------------------------------
/* printf Series of 2,6,12,20,30,42*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,n;     //i for loop and n for stop condition
    clrscr();
    printf("\n Enter value of n:");
    scanf("%d",&n);
    for(i=0;i<=n;i++)
    {
        printf("\t%d",(i*i)+i);   //series get by sum of its square and itself
    }
    getch();


}
-----------------------------------------------------------------
/* program to accept 10 nos and count positive negative and zeros */
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,no,cnp=0,cnn=0,cnz=0;
    clrscr();
    for(i=0;i<=10;i++)
    {
        printf("\n Enter number :");
        scanf("%d",&no);
        if(no==0)
        {
            cnz++;
        }
        if(no>0)
        {
            cnp++;
        }
        if(no<0)
        {
            cnn++;
        }
    }
    printf("\n No of Positive Values:%d",cnp);
    printf("\n No of Negative Values:%d",cnn);
    printf("\n No of Zeroes:%d",cnz);
    getch();
}
----------------------------------------------------------------
/* print 11 to 30 numbers in reverse order */
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,sum=0;
    clrscr();
    for(i=30;i>=11;i--)
    {
        printf("\t %d",i);
        sum=sum+i;
    }
    printf("\n Sum of 11 To 30 %d",sum);
    getch();
}
----------------------------------------------------------------

No comments:

Post a Comment