Thursday, May 31, 2018

C programming:Simple C Codes with output

1. Program to print Name

#include<stdio.h>
main()
{
    printf("My name is MUBIN");
}



2. Program to print Name;Id;Dept;University name

#include<stdio.h>
main()
{
    printf("My name is MUBIN\n");
    printf("My ID is 1103043\n");
    printf("My Dept. is EEE\n");
    printf("My University is BAUET\n");
}


3. Addition
#include<stdio.h>
main()
{
    int a,b,sum;
    a=10;
    b=20;
    sum=a+b;
    printf("sum= %d + %d = %d \n",a,b,sum);

}


4. Add two numbers (take input from keyboard)

#include<stdio.h>
main()
{
    int a,b,sum;
    printf("Enter a:");
    scanf("%d",&a);
    printf("Enter b:");
    scanf("%d",&b);
    sum=a+b;
    printf("Sum= %d + %d = %d\n",a,b,sum);
}


5. Mini Calculator

#include<stdio.h>
main()
{
    float a,b,sum,sub,multi,div;
    printf("Enter a:"); scanf("%f",&a);
    printf("Enter b:"); scanf("%f",&b);
    sum=a+b;
    sub=a-b;
    multi=a*b;
    div=a/b;
    printf("Sum= %f + %f = %f\n",a,b,sum);
    printf("Sub= %f - %f = %f\n",a,b,sub);
    printf("Multi= %f * %f = %f\n",a,b,multi);
    printf("Div= %f / %f = %f\n",a,b,div);
}

6. Find Series and Parallel Resistances
#include<stdio.h>
main()
{
    float R1,R2,Rs,Rp;
    printf("Enter R1:"); scanf("%f",&R1);
    printf("Enter R2:"); scanf("%f",&R2);
    Rs=R1+R2;
    Rp=(R1*R2)/(R1+R2);
    printf("Rs= %.2f \nRp= %.2f\n",Rs,Rp);
}


7. Y to Delta conversion

#include<stdio.h>
main()
{
    float R1,R2,R3,R12,R23,R31;
    printf("Enter R1, R2, R3 serially:\n");
    scanf("%f %f %f",&R1,&R2,&R3);
    R12=((R1*R2)+(R2*R3)+(R3*R1))/(R3);
    R23=((R1*R2)+(R2*R3)+(R3*R1))/(R1);
    R31=((R1*R2)+(R2*R3)+(R3*R1))/(R2);
    printf("R12= %.2f\nR23=%.2f\nR31=%.2f\n",R12,R23,R31);
}


8. Delta to Y conversion
#include<stdio.h>
main()
{
    float r12,r23,r31,r1,r2,r3;
    printf("Enter r12:"); scanf("%f",&r12);
    printf("Enter r23:"); scanf("%f",&r23);
    printf("Enter r31:"); scanf("%f",&r31);
    r1=(r12*r31)/(r12+r23+r31);
    r2=(r12*r23)/(r12+r23+r31);
    r3=(r23*r31)/(r12+r23+r31);
    printf("*************Y Resistances******************\n");
    printf("r1=%.2f\nr2=%.2f\nr3=%.2f",r1,r2,r3);
}


9. Sum and Average of Three numbers
//sum and average by MUBIN
#include<stdio.h>
main()
{
    float a,b,c;
    printf("Enter a,b,c serially:\n");
    scanf("%f %f %f",&a,&b,&c);
    printf("sum=%.2f \navg= %.2f",a+b+c,(a+b+c)/3);
}


10. Relational operators
#include<stdio.h>
main()
{
    int a=3,b=2;
    printf("a>b :%d\n",a>b);
    printf("a<b :%d\n",a<b);
    printf("a>=b :%d\n",a>=b);
    printf("a<=b :%d\n",a<=b);
    printf("a==b:%d\n",a==b);
    printf("a!=b :%d\n",a!=b);
}


11. Logical Operators
#include<stdio.h>
main()
{
    int a=1,b=2,c=3;
    printf("a==1 && b==2:%d\n",a==1 && b==2);
    printf("a==1 && b==4:%d\n",a==1 && b==4);
    printf("a==1 || b==2:%d\n",a==1 || b==2);
    printf("a==1 || b==4:%d\n",a==1 || b==4);
    printf("a==4 || b==5:%d\n",a==4 || b==5);
    printf("a==1 && b==2 && c==3:%d\n",a==1 && b==2 && c==3);
    printf("a==1 && b==2 && c==4:%d\n",a==1 && b==2 && c==4);
    printf("a==1 || b==2 || c==3:%d\n",a==1 || b==2 || c==3);
    printf("a==4 || b==5 || c==6:%d\n",a==4 || b==5 || c==6);

}


12. Incremental and Deccremental operators
#include<stdio.h>
main()
{
    int a=1,b=3;
    printf("Before increment,a= %d\n",a);
    a++;//a=a+1

    printf("After increment,a= %d\n",a);
    printf("Before decrement,b= %d\n",b);
    b--;//b=b-1
    printf("After decrement,b= %d\n",b);
}


13. Modulus Operator (Reminder )
#include<stdio.h>
main()
{
    int a,b,rem;
    printf("Enter a:"); scanf("%d",&a);
    printf("Enter b:"); scanf("%d",&b);
    rem=a%b;
    printf("Reminder= %d \n",rem);
}


14. Find Area of a Circle
//problem to find circular area
#include<stdio.h>
#define PI 3.1416
main()
{
    float r,area;
    printf("Enter radius:"); scanf("%f",&r);
    area=PI*r*r;
    printf("Area= %.2f",area);
}


15. if else statement
#include<stdio.h>
main()
{
    int today;
    int sat=1,sun=2,mon=3,tue=4,wed=5,thu=6,fri=7;
    printf("Enter today:");
    scanf("%d",&today);
    if(today==thu)
    {
        printf("Go to Home\n");
    }
    else
    {
        printf("Go to Hell\n");
    }
}

16. if else ladder

#include<stdio.h>
main()
{
    int today;
    int sat=1,sun=2,mon=3,tue=4,wed=5,thu=6,fri=7;
    printf("Enter today:\n");
    printf("sat=1\nsun=2\nmon=3\ntue=4\nwed=5\nthu=6\nfri=7\n");
    scanf("%d",&today);
    if(today==sat){printf("Goto DHAKA\n");}
    else if(today==sun){printf("Goto RAJSHAHI\n");}
    else if(today==mon){printf("Goto KHULNA\n");}
    else if(today==tue){printf("Goto SYLHET\n");}
    else if(today==wed){printf("Goto CHATTAGRAM\n");}
    else if(today==thu){printf("Goto BARISHAL\n");}
    else if(today==fri){printf("Goto RANGPUR\n");}
    else {printf("Goto HELL\n");}
}


17. Positive, negative, zero

#include<stdio.h>
main()
{
    int n;
    printf("Enter a number:");
    scanf("%d",&n);
    if(n>0)
    {
        printf("It is a positive number!!\n");
    }
    else if(n<0)
    {
        printf("It is a negative number!!\n");
    }
    else
    {
        printf("It is zero\n");
    }
}



18. Leap Year

#include<stdio.h>
main()
{
    int year;
    printf("Enter a year:");
    scanf("%d",&year);
    if(year%4==0)
    {
        printf("It is a LEAP YEAR!!\n");
    }
    else
    {
        printf("It is not a LEAP YEAR!!\n");

    }
}


19. Grading System

#include<stdio.h>
main()
{
    int m;
    printf("Enter marks:"); scanf("%d",&m);
    if(m>=80 && m<=100){printf("A+\n");}
    else if(m>=70 && m<=79){printf("A\n");}
    else if(m>=60 && m<=69){printf("A-\n");}
    else if(m>=50 && m<=59){printf("B\n");}
    else if(m>=40 && m<=49){printf("C\n");}
    else if(m>=33 && m<=39){printf("D\n");}
    else if(m>=0 && m<=32){printf("F\n");}
    else {printf("INVALID INPUT, PLZ TRY AGAIN\n");}
}


20. parallel switch

#include<stdio.h>
main()
{
    int s;
    printf("Enter s:"); scanf("%d",&s);
    if(s==1){printf("RED bulb ON\n");}
    else if(s==2){printf("GREEN bulb ON\n");}
    else if(s==3){printf("BLUE bulb ON\n");}
    else {printf("NO bulb ON\n");}

}

21. switch case

#include<stdio.h>
main()
{
    int s;
    printf("Enter s:"); scanf("%d",&s);
    switch(s)
    {
        case 1: printf("RED bulb ON\n");
                break;
        case 2: printf("GREEN bulb ON\n");
                break;
        case 3: printf("BLUE bulb ON\n");
                break;
        default: printf("NO bulb is ON\n");
                 break;
    }
}


22. Leap year - using switch case

#include<stdio.h>
main()
{
    int year;
    printf("Enter year:");
    scanf("%d",&year);
    switch(year%4)
    {
        case 0: printf("It is a LEAP YEAR\n");
                break;
        case 1: printf("It is not a LEAP YEAR\n");
                break;
        default:printf("It is not a LEAP YEAR\n");
                break;
    }
}

23. Month & Days switch case (generalized)

#include<stdio.h>
main()
{
    int month;
    printf("Enter month:\n");
    printf("1.Jan\n2.Feb\n3.March\n4.April\n5.May\n6.June\n7.Jul\n8.Aug\n9.Sep\n10.Oct\n11.Nov\n12.Dec\n");
    scanf("%d",&month);
    switch(month)
    {
        case 2:
            printf("DAYs=28\n"); break;
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            printf("DAYs=31\n"); break;
        case 4:
        case 6:
        case 9:
        case 11:
            printf("DAys=30\n"); break;
    }
}


24. Month & Days switch case (advanced)

#include<stdio.h>
main()
{
    int month,year;
    printf("Enter year:");
    scanf("%d",&year);
    if(year%4==0)
    {
    printf("Enter month:\n");
    printf("1.Jan\n2.Feb\n3.March\n4.April\n5.May\n6.June\n7.Jul\n8.Aug\n9.Sep\n10.Oct\n11.Nov\n12.Dec\n");
    scanf("%d",&month);
    switch(month)
    {
        case 2:
            printf("DAYs=29\n"); break;
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            printf("DAYs=31\n"); break;
        case 4:
        case 6:
        case 9:
        case 11:
            printf("DAys=30\n"); break;
    }
    }
    else
    {
    printf("Enter month:\n");
    printf("1.Jan\n2.Feb\n3.March\n4.April\n5.May\n6.June\n7.Jul\n8.Aug\n9.Sep\n10.Oct\n11.Nov\n12.Dec\n");
    scanf("%d",&month);
    switch(month)
    {
        case 2:
            printf("DAYs=28\n"); break;
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            printf("DAYs=31\n"); break;
        case 4:
        case 6:
        case 9:
        case 11:
            printf("DAys=30\n"); break;
    }
    }
}

25. Relationship between three variables

#include<stdio.h>
main()
{
    int a,b,c;
    printf("Enter a,b,c:\n");
    scanf("%d %d %d",&a,&b,&c);
    if(a>b && a>c)
    {
        if(b>c){printf("a>b>c");}
        else {printf("a>c>b");}
    }
        if(b>a && b>c)
    {
        if(a>c){printf("b>a>c");}
        else {printf("b>c>a");}
    }
        if(c>a && c>b)
    {
        if(a>b){printf("c>a>b");}
        else {printf("c>b>a");}
    }
}

26. DBBL Mobile banking

#include<stdio.h>
main()
{
    printf("**********Welcome to DBBL Mobile Banking******\n");
    int a1=1,a2=2,a3=3,a4=4,b1=1,b2=2,b3=3,c1=1,c2=2,d1=1,d2=2,op;
    int a,m;
    printf("1.Payment\n2.My Account\n3.Topup\n4.Exit\n");
    printf("Enter option:"); scanf("%d",&op);
    if(op==3)
    {
        printf("1.Self\n2.Others\n3.Exit\n");
        scanf("%d",&op);
        if(op==1){
                printf("Enter amount:"); scanf("%d",&a);
                printf("%d Taka has been Flexi loaded!!",a);
                }
        else if(op==2){printf("Enter Mob No:"); scanf("%d",&m);}
        else if(op==3){main();}
        else {printf("ERROR!!");}
    }
    else
    {
        main();
    }
}


27. while loop

#include<stdio.h>
main()
{
    int i=1;
    while(i<=5)
    {
        printf("My name is MUBIN\n");
        i++;
    }

}

28. do while loop

#include<stdio.h>
main()
{
    int i=1;
    do
    {
        printf("My name is MUBIN\n");
        i++;
    }

    while(i<=5);

}

29. for loop

#include<stdio.h>
main()
{
    int i;
    for(i=1;i<=5;i++)
    {
        printf("My name is Mubin\n");
    }
}

30. print 1 to 100

#include<stdio.h>
main()
{
    for(int i=1;i<=100;i++)
    {
        printf("%d\n",i);
    }
}


31. print 100 to 1

#include<stdio.h>
main()
{
    for(int i=100;i>=1;i--)
    {
        printf("%d\n",i);
    }
}


32. sum of a linear series print file as .cpp)

#include<stdio.h>
main()
{
    int sum=0;
    for(int i=1;i<=10;i++)
    {
        sum=sum+i;
    }
    printf("sum=%d\n",sum);
}


33. Jumping from a loop

#include<stdio.h>
main()
{
    for(int i=1;i<=5;i++)
    {
        if(i==3){break;}
        else{printf("i=%d\n",i);}
    }
}

34. Skipping a part of a loop

#include<stdio.h>
main()
{
    for(int i=1;i<=5;i++)
    {
        if(i==3){continue;}
        else{printf("i=%d\n",i);}
    }
}


35. Descending Triangle

#include<stdio.h>
main()
{
    int n;
    printf("Enter n:"); scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=n-i;j++)
        {
            printf("*");
        }
        printf("\n");
    }
}


36. Ascending Triangle


37. Pyramid

#include<stdio.h>
main()
{
    int n;
    printf("Enter n:");
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=n-i;j++){printf(" ");}
        for(int j=1;j<=i;j++){printf(" *");}
        printf("\n");
    }
}


38. Factorial of n

#include<stdio.h>
main()
{
    int n,sum=1;
    printf("Enter n:");
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        sum=sum*i;
    }
    printf("%d!= %d\n",n,sum);
}



39. Prime number

#include<stdio.h>
main()
{
    int n,dummy=0;
    printf("Enter n:");
    scanf("%d",&n);
    for(int i=2;i<n;i++)
    {
        if(n%i==0){dummy=1;}
        else {dummy=0;}
    }
    if(dummy==1)
    {
        printf("Prime");
    }
    else
    {
        printf("Not Prime");
    }
}


40. Febonacii Series

#include<stdio.h>
main()
{
    int i,n,t1=0,t2=1,nextterm;
    printf("Enter no. of limit n:");
    scanf("%d",&n);
    printf("Febonacii series:");
    for(i=1;i<=n;i++)
    {
        if(i==1){printf("%d\n",t1); continue;}
        if(i==2){printf("%d\n",t2);continue;}
        nextterm=t1+t2;
        t1=t2;
        t2=nextterm;
        printf("%d\n",nextterm);
    }
}

41. Jumping using "break"

#include<stdio.h>
main()
{
    for(int i=1;i<=5;i++)
    {
        if(i==4){break;}
        printf("i=%d I am Mr. Nobody\n",i);
    }
}

42. Skipping using "continue"

#include<stdio.h>
main()
{
    for(int i=1;i<=5;i++)
    {
        if(i==4){continue;}
        printf("i=%d I am Mr. Nobody\n",i);
    }
}

43. Nested for loop

#include<stdio.h>
main()
{
    for(int i=1;i<=3;i++)
    {
        for(int j=1;j<=3;j++)
        {
            printf("i=%d j=%d I Frankenestine\n",i,j);
        }
    }
}

44.  Sum and average of a linear series

#include<stdio.h>
main()
{
    int sum=0,n;
    float avg;
    printf("Enter n:");
    scanf("%d",&n);
    printf("0");
    for(int i=1;i<=n;i++)
    {
        sum=sum+i;
        printf("+%d",i);
    }
    avg=float(sum)/n;
    printf("=%d \naverage=%.2f\n\n",sum,avg);
}

45. Sum of even numbers

#include<stdio.h>
main()
{
    int sum=0,n;
    float avg;
    printf("Enter n:");
    scanf("%d",&n);
    printf("0");
    for(int i=2;i<=n;i=i+2)
    {
        sum=sum+i;
        printf("+%d",i);
    }
    avg=float(sum)/(n/2);
    printf("=%d \naverage=%.2f\n\n",sum,avg);
}

46. Sum of Odd numbers

#include<stdio.h>
main()
{
    int sum=0,n;
    float avg;
    printf("Enter n(even):");
    scanf("%d",&n);
    printf("0");
    for(int i=1;i<=n;i=i+2)
    {
        sum=sum+i;
        printf("+%d",i);
    }
    avg=float(sum)/(n/2);
    printf("=%d \naverage=%.2f\n\n",sum,avg);
}

47. Sum of non linear series
#include<stdio.h>
main()
{
    int sum=0,n;
    float avg;
    printf("Enter n(even):");
    scanf("%d",&n);
    printf("0");
    for(int i=1;i<=n;i=i*2)
    {
        sum=sum+i;
        printf("+%d",i);
    }

    printf("=%d \n\n",sum);
}



48.Array- Run time initialization\

//run time initializatioin
#include<stdio.h>
main()
{
    int a[5];
    printf("Enter 5 elements of array:\n");
    for(int i=0;i<5;i++){scanf("%d",&a[i]);}
    printf("\nYou have entered:\n");
    for(int i=0;i<5;i++){printf("%d \n",a[i]);}
}

49. Array- Run time initialization alternative
//run time initializatioin
#include<stdio.h>
main()
{

    int a[50],n;
    printf("Enter n:"); scanf("%d",&n);
    printf("Enter %d elements of array:\n",n);
    for(int i=0;i<n;i++)
        {
        printf("Enter a[%d]:",i);
        scanf("%d",&a[i]);
        }
    printf("\nYou have entered:\n");
    for(int i=0;i<n;i++)
        {
        printf("a[%d]=%d \n",i,a[i]);
        }
}

50. Program for "Bubble Sorting"
#include<stdio.h>
#include<conio.h>
main()
{
 int i,j,n,t; int a[15];
printf("enter n:");
scanf("%d",&n);
printf("enter %d element of array:\n",n);
for(i=1;i<=n;i++)
{scanf("%d",&a[i]);}

for(i=1;i<=n-1;i++)
{
    for(j=1;j<=n-i;j++)
    {
        if(a[j]<a[j+1])
        {t=a[j];
        a[j]=a[j+1];
        a[j+1]=t;}
        else continue;
    }
}
printf("after sorting:\n");
for(i=1;i<=n;i++)
{printf("%d\n",a[i]);}

}

51. Input and output of string
#include<stdio.h>
main()
{
    char a[30];
    printf("Enter a word:");
    scanf("%s",a);
    printf("You have Entered: ");
    printf("%s",a);
}
 
52. String- scan a sentence from keyboard
#include<stdio.h>
main()
{
    char a[30];
    printf("Enter a word:");
    scanf("%[^\n]",a);
    printf("You have Entered: ");
    printf("%s",a);
}

53. String- take a sentence from keyboard (using gets and puts)
#include<stdio.h>
main()
{
    char a[30];
    printf("Enter a word:");
    gets(a);
    printf("You have Entered: ");
    puts(a);
}

54. Pointer- Understanding pointer (Access the value of a variable using pointer
#include<stdio.h>
main()
{
    int a;
    int *p;
    a=10;
    p=&a;
    printf("The value of a= %d\n",a);
    printf("The address of a= %u\n",&a);
    printf("The address of a= %u\n",p);
    printf("The value of a= %d\n",*p);

}

55. User Defined Function- Sum

#include<stdio.h>
int sum(int a,int b);
main()
{
    int a,b;
    sum(2,3);
    sum(10,20);
    sum(30,40);
}
int sum(int a,int b)
{
    printf("sum= %d+ %d = %d\n",a,b,a+b);
}

56. User Defined Function- Design a mini calculator

#include<stdio.h>
int sum(int a,int b){printf("sum= %d + %d = %d\n",a,b,a+b);}
int sub(int a,int b){printf("sub= %d - %d = %d\n",a,b,a-b);}
int multi(int a,int b){printf("multi= %d * %d = %d\n",a,b,a*b);}
int div(int a,int b){printf("div= %d / %d = %d\n",a,b,a/b);}
main()
{
    int a,b,option;
    printf("Enter an option:\n1.sum\n2.sub\n3.multi\n4.div\n");
    scanf("%d",&option);
    printf("Enter a:"); scanf("%d",&a);
    printf("Enter b:"); scanf("%d",&b);
    if(option==1)     {sum(a,b);}
    else if(option==2){sub(a,b);}
    else if(option==3){multi(a,b);}
    else if(option==4){div(a,b);}
    else{printf("Sorry. Please try again\n");}

}

57. C++ (Print name, ID, Dept, University

#include<iostream>
using namespace std;
main()
{
    cout<<"MUBIN"<<endl<<"1103043"<<endl<<"EEE"<<endl<<"KUET"<<endl;

}
58. C++ (Find sum)
#include<iostream>
using namespace std;
main()
{
    int a,b,sum;
    cout<<"Enter a number:";
    cin>>a;
    cout<<"Enter another number:";
    cin>>b;
    sum=a+b;
    cout<<"sum="<<a<<"+"<<b<<"=o"<<sum;

}

59. Write a program in C++ to understand Object Oriented Programming (OOP)


60. Understanding OOP

//using same function in different class
#include<iostream>
using namespace std;
class biral
{
public:
    void name(){cout<<"WHITE CAT"<< endl;}
    void barking(){cout<<"MEW MEW..."<< endl;}
};
class sagol
{
public:
    void name(){cout<<"LALTU"<< endl;}
    void barking(){cout<<"VA VA....."<< endl;}

};
class goru
{
public:
    void name(){cout<<"MOHESH"<< endl;}
    void barking(){cout<<"HUMBA HUMBA...."<< endl;}
};
main()
{

    biral obj1;
    sagol obj2;
    goru obj3;
    obj1.name();
    obj1.barking();
    obj2.name();
    obj2.barking();
    obj3.name();
    obj3.barking();
}




No comments:

Post a Comment