netoops blog
Showing posts with label Fundamental Program in C. Show all posts
Showing posts with label Fundamental Program in C. Show all posts

Wednesday, 23 October 2013

Write a c program to find out the sum of series 1 + 2 + …. + n.

#include
int main(){
    int n,i;
    int sum=0;
    printf("Enter the n i.e. max values of series: ");
    scanf("%d",&n);
    sum = (n * (n + 1)) / 2;
    printf("Sum of the series: ");
    for(i =1;i <= n;i++){
         if (i!=n)
             printf("%d + ",i);
         else
             printf("%d = %d ",i,sum);
         }
   
    return 0;
}
Sample output:
Enter the n i.e. max values of series: 5
Sum of the series: 1 + 2 + 3 + 4 + 5 = 15

Thursday, 3 October 2013

Write a c program to print Hello world without using any semicolon.

Answer:
void main(){
    if(printf("Hello world")){
    }
}

Sunday, 22 September 2013

Sequential Search in C

#include<stdio.h>
#include<conio.h>

void main()
{
  int a[10],i,v,count;
  clrscr();
  printf("ENTER THE ELEMENT IN ARRAY:");
  for(i=0;i<10;i++)
  {

     scanf("%d",&a[i]);
  }
 
  printf("\nTHE ELEMENTS YOU WANT TO SEARCH:");
  scanf("%d",&v);
  for(i=0;i<10;i++)
  {
    if(a[i]==v)
    {
      printf("\nVALUE FOUNDAT CELL NO.%d=%d",i,v);
      break;
     }
  }
  getch();
}

Selection_Sort

#include<stdio.h>
#include<conio.h>

void main()
{
int arr[5];
clrscr();
initarr(arr);
show(arr);
sort(arr);
show(arr);
getch();
}

sort(int x[5])
{
int i,j,t;
for (i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if (x[i] > x[j])
{
t   = x[i];
    x[i] = x[j];
    x[j] = t;
}
}
}
}

initarr(int x[5])
{
int i;
for (i=0;i<=4;i++)
{
printf ("ENTER ELEMENT :");
scanf ("%d",&x[i]);
}
}



show(int x[5])
{
int i;
for (i=0;i<=4;i++)
printf ("%d\t",x[i]);
printf("\n");
}

Lower_triangular_Matrix_sum

#include<stdio.h>
#include<conio.h>

void main()
{
  int arr[4][4];
  int i,j,u_sum=0,l_sum=0;
  clrscr();
  printf("\nENTER THE VALUE IN ARRAY:");
  for(i=0;i<=3;i++)
  {
     for(j=0;j<=3;j++)
scanf("%d",&arr[i][j]);
  }

  l_sum=0;
  for(i=3;i>=1;i--)
  {
     for(j=0;j<=i-1;j++)
 l_sum=l_sum+arr[i][j];
  }

  printf("SUM OF LOWER MATRIX:%d",l_sum);
  getch();
}

Upper Triangular matrix in C

#include<stdio.h>
#include<conio.h>

void main()
{
  int arr[4][4];
  int i,j,u_sum=0,l_sum=0;
  clrscr();
  printf("\nENTER THE VALUE IN ARRAY:");
  for(i=0;i<=3;i++)
  {
     for(j=0;j<=3;j++)
scanf("%d",&arr[i][j]);
  }

  u_sum=0;
  for(i=0;i<=2;i++)
  {
     for(j=i+1;j<=3;j++)
 u_sum=u_sum+arr[i][j];
  }

  printf("SUM OF UPPER MATRIX:%d",u_sum);
  getch();
}

Tower of Hanoi in C

# include<stdio.h>
# include<conio.h>

void move(int n,char beg,char end,char aux);

void main()
{
 int n;
 clrscr();
 printf("\t\t\t\tTOWER OF HANOI");
 printf("\n\n Enter the number of disks: ");
 scanf("%d",&n);
 printf("\n The disks 1,2,3... are in the increasing order of their sizes.\n\n");
 move(n,'A','B','C');
 getch();
}

void move(int n,char beg,char aux,char end)
{
 if(n>0)
 {
  move(n-1,beg,end,aux);
  printf(" Move disk %d from Peg %c to Peg %c\n",n,beg,end);
  getch();
  move(n-1,aux,beg,end);
 }
}

String Reversing In C

#include<stdio.h>
#include<conio.h>
#define EOLN '\n'

void reverse(void);  //function protype

void main(void)
{
   clrscr();
   printf("\n\n\t\t please enter a name \n");
   reverse();
   getche();
}

void reverse(void)
{
  char c,p;
  if((c=getchar())!=EOLN) reverse();
     putchar(c);
  return;
}

Program for converting Decimal to Roman

#include <stdio.h>
#include <conio.h>

void main()
{
int yr;
clrscr();
printf ("ENTER ANY YEAR :");
scanf ("%d",&yr);
yr = roman (yr,1000,"m");
yr = roman (yr,500,"d");
yr = roman (yr,100,"c");
yr = roman (yr,50,"l");
yr = roman (yr,10,"x");
yr = roman (yr,9,"ix");
yr = roman (yr,5,"v");
yr = roman (yr,4,"iv");
yr = roman (yr,1,"i");
getch();
}

roman (int yr, int n, char *ch)
{
int t,i;
t = yr/n;
for (i = 1; i <= t; i++)
printf ("%s",ch);
return(yr-t*n);
}

Friday, 20 September 2013

Write a c program to print Hello world without using any semicolon.

Explanation:
Solution: 1
void main(){
    if(printf("Hello world")){
    }
}

Solution: 2
void main(){
    while(!printf("Hello world")){
    }
}

Solution: 3
void main(){
    switch(printf("Hello world")){
    }
}

Sunday, 25 August 2013

Sum_of_Digit

#include <stdio.h>
#include <conio.h>

void main()
{
long int s = 0,d;
clrscr();
printf ("ENTER ANY NUMBER :");
scanf ("%ld",&n);
while (n)
{
d = n % 10;
s = s + d;
n /= 10;
}
printf ("SUM OF DIGIT IS %d ",s);
getch();
}

Sum_Avg_First_20_Natural_No

#include <stdio.h>
#include <conio.h>

void main()
{
    int i;
    float avg, s = 0.0;
    clrscr();
    for (i=1;i<=20;i++)
    s = s + i;
    avg = s / 20;
    printf("The sum is %d\n", s);
    printf("The average is %f\n", avg);
    getch();
}

Saturday, 24 August 2013

Tower_Of_Hanoi

# include<stdio.h>
# include<conio.h>

void move(int n,char beg,char end,char aux);

void main()
{
 int n;
 clrscr();
 printf("\t\t\t\tTOWER OF HANOI");
 printf("\n\n Enter the number of disks: ");
 scanf("%d",&n);
 printf("\n The disks 1,2,3... are in the increasing order of their sizes.\n\n");
 move(n,'A','B','C');
 getch();
}

void move(int n,char beg,char aux,char end)
{
 if(n>0)
 {
  move(n-1,beg,end,aux);
  printf(" Move disk %d from Peg %c to Peg %c\n",n,beg,end);
  getch();
  move(n-1,aux,beg,end);
 }
}

Factorial_No

#include <stdio.h>
#include <conio.h>

void main()
{
int i,n,fact =1;
clrscr();
printf ("ENTER THE NUMBER");
scanf ("%d",&n);
for (i=2;i<=n;i++)
fact = fact *i;
printf ("%d!=%d",n,fact);
getch();
}

ASCII_Value_Display

#include <stdio.h>
#include <conio.h>

void main()
{
 int i;
 clrscr();
 for(i=0;i<=255;i++)
   printf("%c -----> %d\n ",i,i);
 getch();
}

ASCII Code Display

# include <stdio.h>
# include <conio.h>
main()
{
int i;
clrscr();
for(i=0;i<=200;i++)
{       printf("%d------------> %c  \n",i,i);
if((i%20)==0)
{
getch();
clrscr();}
}
getch();
}

A_Power_M

#include <stdio.h>
#include <conio.h>

void main()
{
float a, ans =1.0;
int  i,m;
clrscr();
printf ("ENTER A & M : ");
scanf ("%f %d",&a,&m);

     for (i=1;i<=abs(m);i++)
ans = ans * a;
    if (m < 0)
ans = 1/ans;
   
    printf ("%f POWER %d = %f ",a, m, ans);
    getch();
}

Factorial_No

#include <stdio.h>
#include <conio.h>

void main()
{
int i,n,fact =1;
clrscr();
printf ("ENTER THE NUMBER");
scanf ("%d",&n);
for (i=2;i<=n;i++)
fact = fact *i;
printf ("%d!=%d",n,fact);
getch();
}

Armstrong Number

#include<stdio.h>
#include<conio.h>

int main()
{
long int s = 0,d,n,t;

printf ("ENTER ANY NUMBER :");
scanf ("%ld",&n);
t = n;
while (t)
{
d = t % 10;
s = s + d*d*d;
t /= 10;
}
if (s == n)
printf ("%d IS ARMSTRONG NUMBER ",n);
else
printf ("%d IS NOT ARMSTRONG NUMBER ",n);


return 0;
getch();
}

 
Subscribe For Free Updates!

We'll not spam mate! We promise.

Become Our Fan on Social Sites!