Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Thursday, March 3, 2016

Grade Generation or use of else-if statement in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>               //preprocessor , header library file included.

main()                  //main funtion starts.
{
 int marks;                //variable taken  defining datatype
 
 printf("Enter your obtained marks:\t");         //a message to the user regarding the program
 scanf("%d",&marks);              //input taken from user.
                    
  if(marks<=100){              //if starts from here with a condition
  if(marks>=80 && marks<=100) printf("A+\n");       //else ....if ladder starts from here.
   else if(marks>=70 && marks<=79) printf("A-\n");
    else if(marks>=60 && marks<=69) printf("B+\n");
     else if(marks>=50 && marks<=59) printf("C+\n");
      else if(marks>=45 && marks<=49) printf("D\n");
       else if(marks>=0 && marks<=40) printf("F\n");   //else if ladder ends here.
  }                
  else
  printf("Sorry the inputed marks is greater than 100.\n");   //the default message printed.
         
}                   //program ends here.

Sum of a number's components in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>

main()
{
 int i,sum,sum1=0,sum2=0, sum3=0, sum4=0;
 
 {
  scanf("%d",&i);
  sum1=i%10;
  i=i/10;
  sum2=i%10;
  i=i/10;
  sum3=i%10;
  i=i/10;
  sum4=i%10;
  i=i/10;
 }
 sum= sum1+sum2+sum3+sum4;
 printf("%d\n",sum);
 
}

strupr() function example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<stdio.h>
#include<string.h>

main()
{
 char name[20],name2[20];
 printf("Enter your name ?\n");
 gets(name);
 printf("Enter name2?.\n");
 gets(name2);
 strcat(name,name2);
 printf("\n\n\n\n");
 printf("%s",strupr(name));
 
}

strspn() function example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<stdio.h>
#include<string.h>

main()
{
 char name[20],name2[20];
 printf("Enter your name ?\n");
 gets(name);
 printf("Enter name2?.\n");
 gets(name2);

 printf("\n\n\n\n");
 printf("%d",strspn(name,name2));
 
}

strrev() function example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include<stdio.h>
#include<string.h>

main()
{
 char aSimpleString[80];
 printf("Enter your string\n");
 gets(aSimpleString);
 printf("Revers is %s",strrev(aSimpleString));
}

strlwr() function example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<stdio.h>
#include<string.h>

main()
{
 char name[20],name2[20];
 printf("Enter your name ?\n");
 gets(name);
 printf("Enter name2?.\n");
 gets(name2);
 strcat(name,name2);
 printf("\n\n\n\n");
 printf("%s",strlwr(name));
 
}

strcmp() function example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include<stdio.h>
#include<string.h>

main()
{
 char name[20],name2[20];
 int value;
 printf("Enter your name ?\n");
 gets(name);
 printf("Enter name2?.\n");
 gets(name2);
 value = strcmp(name,name2);
 if(value==0){
  printf("The strings are equal.");
 }
 else printf("not");
}

strcpy() function example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<stdio.h>
#include<string.h>

main()
{
 char name[20],name2[20];
 char value[50];
 printf("Enter your name ?\n");
 gets(name);
 printf("Enter name2?.\n");
 gets(name2);
 strcpy(name,name2);
 printf("%s",name);
 
}

strcat() function example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<stdio.h>
#include<string.h>

main()
{
 char name[20],name2[20];
 printf("Enter your name ?\n");
 gets(name);
 printf("Enter name2?.\n");
 gets(name2);
 strcat(name,name2);
 printf("\n\n\n\n");
 printf("%s",name);
 
}

True-false/ on-off printing in C

1
2
3
4
5
6
7
8
9
#include<stdio.h>

main()
{
 int a,b,c;
 scanf("%d %d",&a,&b);
 c = a > b;
 printf("%d",c);
}

Checking leap year in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int main(){
      int year;
      printf("Enter a year: ");
      scanf("%d",&year);
      if(year%4 == 0)
      {
          if( year%100 == 0) /* Checking for a century year */
          {
              if ( year%400 == 0)
                 printf("%d is a leap year.", year);
              else
                 printf("%d is not a leap year.", year);
          }
          else
             printf("%d is a leap year.", year );
      }
      else
         printf("%d is not a leap year.", year);
      return 0;
}

switch case example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>         //header file

main()            //main function
{
 char gradeName;         //variable declaration
 printf("Enter your Grade:");    
 scanf("%c",&gradeName);       //taking input from user
 
  switch(gradeName){       //start point of switch case
   case 'A':
   case 'a':
    printf("Excellent\n");
    break;
   case 'b':
   case 'B':
    printf("Good\n");
    break;
   case 'c':
   case 'C':
    printf("Less good\n");
    break;
   case 'd':
   case 'D':
    printf("Partially good\n");
    break;
   case 'f':
   case 'F':
    printf("Very bad\n");
   default:
    printf("Keyword not found\n");
    break;
  }            //end of switch case
}              //end of program

goto example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <stdio.h>

main()
{
 int i;
 i=1;
 mylevel: if(i%2==0){
  printf("%d\n",i);
  i++;
  
 }
  if(i<=20){
   goto mylevel;
  }
}

Farenhit to Celcius conversion in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include<stdio.h>
#include<math.h>

main()
{
    float far, cel , kelvin;
    while(printf("Enter your temp in farenheit: ")!=EOF){
    scanf("%f",&far);
    cel=(far-32)/1.8;
    kelvin=cel+273;
    printf("Your Kelvin temp is %.2fK\n",kelvin);
    }
}

do while loop in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <stdio.h>

main()
{
 int i=10;
 do{
  printf("Hello %d\n",i);
  i--;
  if(i==5) break;
 }
 
    while(i>=1);
}

Continue example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include<stdio.h>

main()
{
 int i;
 for(i=1; i<=10; i++){
  if(i==6 || i==7){
   continue;
  }
  else printf("%d\n",i);
 }
}

Celcius to Kelvin conversion in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include  <stdio.h>
#include <math.h>

main()
{
 float temp,temp1;
  printf("Enter the temparature in Celcius :");
  scanf("%f",&temp);
   temp1 = temp + 273;
    printf("Converted temparature: %.2f K",temp1);
}

Break example in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <stdio.h>

main()
{
 int n;
 printf("Enter the Integers:");
  while(1)
  {
   scanf("%d",&n);
    if(n==30)
    break;
  }
 printf("30 is entered so no more.");
 
}

Array element's square sum in C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<math.h>

main() 
{
 int i,n;
 printf("Enter how long will be your array:\n");
 scanf("%d",&n);
 float x[n],value,total;
 total = 0.0;
 printf("Enter %d real number:\n",n);
 for(i=0; i<n; i++){
  scanf("%f",&value);
  x[i]=value;
  total = total + pow(x[i],2);
  printf("x[%d] = %.2f\n",i+1,x[i]);
 }
 
 printf("Total = %.2f\n",total);
 
}

Adding two number in C

1
2
3
4
5
6
7
8
9
#include <stdio.h>

main()
{
 int a,b,result;     //variable declaration
 scanf("%d %d",&a,&b);  //input from user
  result = a+b;   //adding two numbers
   printf("%d\n",result);  //printing the result
}