Example programs  C,P1

Photo by Ian Keefe on Unsplash

Example programs C,P1

A simple program to convert binary numbers to normal numbers in C.

#include<stdio.h>
int main(){
    int num,binary_num,decimal_num=0,base=1,rem;
    printf("Enter a binary number :");
    scanf("%d",&num);
    binary_num=num;
    while(num>0)
    {
        rem=num%10;//Takes the last 2 numbers
        decimal_num=decimal_num+rem*base;
        num=num/10;//Takes the 1st 3 numbers
        base=base*2;//Increases value
    }
    printf("%dAns=%d",binary_num,decimal_num);

    return 0;
}

Question to print the nth Fibonacci number in C, logic explained in the previous blog.

#include<stdio.h>
int main(){
    int n1=1,n2=2,n3,num;
    printf("Enter a number :");
    scanf("%d",&num);
    printf("%d\n%d\n",n1,n2);
    for (int i=2;i<num;i++)
    {
        n3=n1+n2;//Adds 2 numbers
        printf("%d\n",n3);//Prints
        n1=n2;//Changes value 
        n2=n3;//Changes value
    }
    printf("");

    return 0;
}

A C program to add the sum of the digits of a multi-digit number.

#include<stdio.h>
int main(){
    int m,div,num;
    printf("Enter a number :");
    scanf("%d",&num);
    printf("%d\n",num);
    while(num>0)

    {
        div=num%10;//Gets the last digit
        m=m+div;//Adds
        num=num/10;//Gets the 1st numbers
    }
    printf("Ans=%d",m);

    return 0;
}

C program to make a triangle with numbers by taking the number of rows from the user.

#include<stdio.h>
int main(){
    int num,count;
    printf("Enter a number :");
    scanf("%d",&num);
    printf("%d\n",num);
    for(int i=0;i<num;i++){//No of rows
        for(int j=0;j<=i;j++){//No of colunms
            printf("%d",count);
            count++;
        }printf("\n");
        }


    return 0;
}

C Program to input the number of units consumed, charge them and print the bill.

#include<stdio.h>
#include<string.h>
void main()
{
    int cust_no,unit_con;
    float charge,surcharge=0,amt,total_amt;
    char nm[25];

    printf("Enter the costomer idno :\n");
    scanf("%d",&cust_no);
    printf("Enter the customer Name :\t");
   scanf("%s",nm);
   printf("Enter the unit consumed by customer  :\t");
   scanf("%d",&unit_con);

   if(unit_con<200)//To add the charge
    {charge=0.80;}
    else if(unit_con>=200&&unit_con<300)
    {charge=0.90;}
    else
    charge=1.00;

    amt=unit_con*charge;
    if(amt>400)
    {surcharge=amt*15/100.0;}
    total_amt=amt+surcharge;

    printf("\t\t\t\nElectricity Bill\n\n");
   printf("Customer IDNO                       :\t%d",cust_no);
   printf("\nCustomer Name                       :\t%s",nm);
   printf("\nunit Consumed                       :\t%d",unit_con);
   printf("\nAmount Charges @Rs. %4.2f  per unit  :\t%0.2f",charge,amt);
   printf("\nSurchage Amount                     :\t%.2f",surcharge);
   printf("\nMinimum meter charge Rs             :\t%d",100);
   printf("\nNet Amount Paid By the Customer     :\t%.2f",total_amt+100);

}

A program to use character classification function in C.

#include<stdio.h>
#include<ctype.h>

int main()
{
    char ch;
    printf("enter the character to be examined:");
    scanf("%c", &ch);

    if(islower(ch))//Cheaks if its lower
        printf("You have entered a lower case letter\n");
    else if(isupper(ch))
        printf("You have entered a UPPER case letter\n");
    else if(isdigit(ch))
        printf("You have entered a digit\n");
    else if(ispunct(ch))
        printf("You have entered a Punctuation character\n");
    else if(isspace(ch))
        printf("You have entered a Whitespace character\n");
    else
        printf("You have entered  a control character\n");

    return 0;
}

Program to demonstrate automatic and type casting numeric in C.

#include<stdio.h>
#include<stdbool.h>
int main()
{
    //local declarations
    bool b=true;
    char c='G';
    float d=244.3;
    int i=3120;
    short s=69;

    //statements
    printf("bool+char is char:%c\n",b+c);
    printf("int * short is int: %d\n",i*s);
    printf("float * char is float: %f\n",d*c);

    c=c+b;
    d=d+c;
    b=false;
    b=-d;

    printf("\n After execution...\n");
    printf("char +true=  %c\n",c);
    printf("float +char =%f\n",d);
    printf("bool =-float=%f\n",b);

    return 0;
}

Thankyou for reading this blog, do not forget to follow me. These are the programs I have practised for my test hope this blog is helpful.