Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

Tuesday, September 23, 2014

Program to print out numbers separated by stars with the occurrence defined by the number printed.

This will print out something like this
1
2*2
3*3*3
4*4*4*4
and then the reverse image.
If you have any doubt, please do ask.

#include<stdio.h>
main()
{
int i=1,j=2,z=4,k;
printf("%d\n",i);
for(k=0;k<3;k++)
{
for(i=2;i<z-1;i++)
{
printf("%d*",j);
}
printf("%d\n");
j++;
z++;
}
j=4;
for(k=0;k<3;k++)
{
for(i=2;i<z-2;i++)
{
printf("%d*",j);
}
printf("%d\n");
j--;
z--;
}
printf("%d",1);
}

Saturday, August 9, 2014

Factorial program of a number

Simple C program to find factorial of a number. If you need help please feel free to ask

#include <stdio.h> main() {int fact,n; printf("enter a number\n"); scanf("%d",&n); for(fact=1;n>0;n--) fact=fact*n; printf("factorial of the number is %d\n",fact); }

Program to check whether a number is a palindrome.

Program to find if a number is a palindrome. One of the most basic programs for any CSE student to learn. Comment below if you need help

#include<stdio.h>
void main()
{
int x,num,sum=0,rem;
printf("enter the number");
scanf("%d",&num);
x=num;
while(num>0)
{
rem=num%10;
sum=(sum*10)+rem;
num=num/10;
}
if(sum==x)
printf("palindrome");
else
printf("no");
}

Pascal's Triangle Program

This is Pascal's triangle program. Was probably one of the hardest programs I had during my second year. But looking back, its not as hard as it seems. For those who have trouble with this program, I suggest you to write it down, step by step with pencil and paper to see how the program works. If you need help, feel free to ask.

#include<stdio.h>
main()
{
int d,i,j,n,a[50],k,b[50];
printf("enter limit");
scanf("%d",&n);
d=n;
for(i=0;i<n;i++)
{
a[0]=1;
a[i]=1;
for(j=1;j<i;j++)
{
a[j]=b[j-1]+b[j];
}
for(k=0;k<(d-1);k++)
{
printf(" ");
}
d--;
for(k=0;k<=i;k++)
{
printf("%d",a[k]);
printf(" ");
}
for(k=0;k<=i;k++)
printf("\n");
for(k=0;k<=i;k++)
b[k]=a[k];
}
}

Program to count number of occurrences of a word in a sentence.

Simple C program to count the number of occurrences of a word in a sentence. If you need help, feel free to ask.


#include<stdio.h>
#include<string.h>
main()
{
  int len,c=0,i=0,j=0,ans=0;
  char str1[50],str2[20],new[20];
  printf("\n enter the text: ");
  gets(str1);
  printf("\n enter the word: ");
  gets(str2);
  len=strlen(str1);
  for(i=0;i<=len;i++)
   {
     if(str1[i]!=' ')
      {
       new[j]=str1[i];
       j++;
      }
     if(str1[i]==' '||str1[i]=='\0')
       {
           new[j]='\0';
           j=0;
           ans=strcmp(new,str2);
           if(ans==0)
             c++;
        }
    }
   printf("\n the number of occurence is %d\n",c);
}