Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Showing posts with label learning c. Show all posts
Showing posts with label learning c. Show all posts

Saturday, November 15, 2014

Program to swap two values without using third variable in C.

Swapping without third variable


Code Box
---------------------------------------------

/*program to swap two values without using third variable */

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

printf("\nEnter two numbers :");
printf("\nEnter first number a= ");
scanf("%d",&a);

printf("\nEnter second number b= ");
scanf("%d",&b);

printf("\nValues before swapping a= %d & b= %d ",a,b);

a=a+b;   //swapping
b=a-b;
a=a-b;

printf("\nValues after swapping a= %d & b= %d ",a,b);

getch();
}

Wednesday, November 5, 2014

Program to print diamond pattern 1 using C.

Diamond Pattern 1

Code Box
---------------------------------------

/* Program to print diamond pattern 1 */
#include <stdio.h>
#include <conio.h>
void main()
{

int i, j, k,n;
clrscr();

printf("Enter the number of stars: ");
scanf("%d",&n);

printf("\n---------------Diamond pattern-------------------\n");

for (i = 1; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
printf(" ");
for (j = 1; j <= i; j++)
printf("*");

for (k = 1; k < i; k++)
printf("*");

printf("\n");
}


for (i = n - 1; i >= 1; i--)
{
for (j = 0; j < (n - i); j++)
printf(" ");

for (j = 1; j <= i; j++)
printf("*");
for (k = 1; k < i; k++)
printf("*");
printf("\n");
}
printf("\n");
printf("\n----------------------------------\n");
getch();
}









Monday, November 3, 2014

Program to find factor of any number in C.




Code Box
-----------------------------------

/* Program to find factor of any number */

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();

int num,i;
printf("\n---------Factor Finder--------");
printf("\nEnter a number:");
scanf("%d",&num);

printf("\nFactors of %d are : ",num);
for(i=1;i<num;i++)
{
if(num%i==0)
{
printf("  %d",i);
}

}

getch();
}

Sunday, November 2, 2014

Program to print square and cube of any number in C.



Code Box
-------------------------------------

/* program to print a square and cube of any number */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,sq,cu;

printf("\nEnter a number:");
scanf("%d",&a);
sq=a*a;
cu=a*sq;

printf("\n--------------------");
printf("\n\t Number:%d",a);
printf("\n\t Square:%d",sq);
printf("\n\t Cube  :%d",cu);

printf("\n--------------------");

getch();
}

Program to print Student information using array in C.


Student Information


Code Box
---------------------------------------

/* Program to print Student information */

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[25],city[20],clas[20];
clrscr();

printf("\nEnter name:");
scanf("%s",name);

printf("\nEnter  age:");
scanf("%d",&age);

printf("\nEnter  class:");
scanf("%s",clas);

printf("\nEnter City:");
scanf("%s",city);

printf("\n------------------------------------------");
printf("\n\t Student Information ");
printf("\n\t Name : %s",name);
printf("\n\t Age  : %d",age);
printf("\n\t Class: %s",clas);
printf("\n\t City : %s",city);

printf("\n------------------------------------------");



getch();
}

program to print square and cube of numbers between a given range by user in C.



Code Box
----------------------------------

/* program to print square and cube of numbers between range*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,sq,cu,br,ij;

printf("\nEnter Start number:");
scanf("%d",&br);
printf("\nEnter End number:");
scanf("%d",&ij);
printf("\n--------------------");

for(i=br;i<=ij;i++)
{
sq=i*i;
cu=i*sq;
printf("\n\t Number:%d",i);
printf("\n\t Square:%d",sq);
printf("\n\t Cube  :%d",cu);

printf("\n--------------------");

}

getch();
}

Program solution with range system in C.



Code Box
-------------------------------------------

/* Program to print fizz in place of multiple of 3 and buzz in place of 5 */

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
printf("\nEnter start:");
scanf("%d",&j);
printf("\nEnter End:");
scanf("%d",&k);


for(i=j;i<=k;i++)
{
if(i%(3*5)==0)
{
printf("BuzzFizz");
}
else if(i%5==0)
{
printf("Buzz");
}
else if(i%3==0)
{
printf("Fizz");
}
else
{
printf("%d",i);
}

if(i%10==0)
{
printf("\n");
}
else
{
printf("\t");
}

}

getch();
}

Program for function with return type and with arguments in C.

codeboxc output


Code Box
--------------------------------

/*Program for  function with return type and with arguments */

#include<stdio.h>
#include<conio.h>
void main()
{
int total,a,b;
int sum(int ,int ); //Function declaration
printf("\nEnter first number:");
scanf("%d",&a);
printf("\nEnter Second number:");
scanf("%d",&b);
printf("Sum of two number:");
total=sum(a,b);
printf("%d",total); //Function call
getch();
}
int sum(int a,int b) //Function Definition
{
int c;
c=a+b;
return c;
}

Program for function without return type and without arguments in C.



Code Box
------------------------------------

/*Program for  function without return type and without arguments*/

#include<stdio.h>
#include<conio.h>
void main()
{
void sum(); //Function Declaration
printf("Sum of two number:");
sum(); //Function call
getch();
}
void  sum() //Function Definition
{
int a,b,c;
a=5;
b=6;
c=a+b;
printf("%d",c);
}

Program to print table of all odd numbers less than 10, Using flow control terms Break & Continue in C.



Code box
--------------------------------------

/*Program to print table of all odd numbers less than 10. Useing flowcontrol
terms  Break & Continue. */


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
for(a=1;;a++)
{

if(a>=10)
{ break;
}
else
if(a%2==0)
{continue;
}
else
{
for(b=1;b<=10;b++)
{
printf("  %d",b*a);
}

}

printf("\n");
}
getch();
}

Program to illustrate the solution of fizz-buzz problem in C.




Code Box
---------------------------------------------

/* Program to print fizz in place of multiple of 3 and buzz in place of 5 */

#include<stdio.h>
#include<conio.h>
void main()
{
int i;

for(i=1;i<=50;i++)
{
if(i%(3*5)==0)
{
printf("BuzzFizz");
}
else if(i%5==0)
{
printf("Buzz");
}
else if(i%3==0)
{
printf("Fizz");
}
else
{
printf("%d",i);
}

if(i%10==0)
{
printf("\n");
}
else
{
printf("\t");
}

}

getch();
}