Saturday, July 2, 2016

C Program to print Odd number series 1 3 5 7 ... N for the given value of N.



C Program to print Odd number series 1 3 5 7 ... N for the given value of N.

For example, if N=10, output should be
1 3 5 7 9


#include<stdio.h>
int main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=1; i<=N; i=i+2)
 {
   printf("%d ",i);
 }
 return 0;
}

Output of program

Enter N:10
1 3 5 7 9

C Program to print series 5, 4, 3, 2, 1 for the given value of N.


C Program to print series 5, 4, 3, 2, 1 for the given value of N.

For example, if N=5, output should be
5, 4, 3, 2, 1


#include<stdio.h>
int main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=N; i>0; i--)
 {
   printf("%d",i);
   //Logic avoid last , 
   if(i!=1)
     printf(", ");
 }
 return 0;
}

Output of program

Enter N:5
5, 4, 3, 2, 1


C Program to print series 1, 2, 3, 4, 5.....N for the given value of N.

C Program to print series 1, 2, 3, 4, 5.....N for the given value of N.
For example, if N=5, output should be
1, 2, 3, 4, 5


#include<stdio.h>
int main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=1; i<=N; i++)
 {
  printf("%d",i);
 
  //Logic avoid last , 
  if(i!=N)
   printf(", ");
 }
 return 0; }

Output of program
Enter N:5
1, 2, 3, 4, 5


Friday, July 1, 2016

C Program to Calculate Simple Interest based on given values.


#include<stdio.h>
int main()
{
 float p, r, n, si;
 
 printf("Enter your principle amount:");
 scanf("%f",&p);
 
 printf("Enter your rate of interest:");
 scanf("%f",&r);
 
 printf("Enter number of years:");
 scanf("%f",&n);
 
 si = (p*r*n)/100;
 
 printf("Simple Interest is %.2f", si);
 return 0;
}

Output of program

Enter your principle amount:1000
Enter your rate of interest:12
Enter number of years:2
Simple Interest is 240.00

C Program to Print inverted Number Pattern



C Program to Print inverted Number Pattern as given below:
For N=5,

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5


#include<stdio.h>
int main()
{
 int i, j, n=5;

 for(i=0; i<5; i++)
 {
  for(j=5; j>i; j--)
  {
    printf("%d ",j);
  }
    printf("\n");
 }
 return 0; 
}

Output of program

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5


C Program to Print given number Pattern.


C Program to Print Number Pattern as given below:
For N=5,

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include<stdio.h>
int main()
{
 int i, j, n=5;

 for(i=0; i<5; i++) //this loop tracks no. of lines
 {
  for(j=0; j<=i; j++)//this loop tracks numbers in a row
  {
    printf("%d ",j+1); //this line prints a number
  }
    printf("\n");
 } 
 return 0;
}

Output of program

1
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

 

Algorithm to C program conversion

/* Example of Algorithm to C program conversion.

Problem Definition: Write an algorithm to add given two numbers.

Step 1: Start.
Step 2: Read two numbers A and B.
Step 3: Total = A+B.
Step 4: Display Total.
Step 5: Stop.

*/

#include<stdio.h>
int main()
{
 int A, B, Answer;

 printf("Enter number A:");
 scanf("%d",&A);

 printf("Enter number B:");
 scanf("%d",&B);

 Answer = A + B;
 printf("%d", Answer);
 return 0;
}


Output of program:

Enter number A:5
Enter number B:10
15