Pages

Showing posts with label If-Else-If (Conditions). Show all posts
Showing posts with label If-Else-If (Conditions). Show all posts

Write a program in c language to compare two strings by using pointers.

Sunday, 6 July 2014

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

int compare_strings(char*, char*);

int main()
{
    char str1[100], str2[100], result;

    printf("Enter first string: ");
    gets(str1);

    printf("Enter second string: ");
    gets(str2);

    result = compare_strings(str1, str2);

    if ( result == 0 )
       printf("Strings are same.\n");
    else
       printf("Entered strings are not equal.\n");

    getch();
}

int compare_strings(char *str1, char *str2)
{
   while(*str1==*str2)
   {
      if ( *str1 == '\0' || *str2 == '\0' )
         break;

      str1++;
      str2++;
   }
   if( *str1 == '\0' && *str2 == '\0' )
      return 0;
   else
      return 1;
}

Write a program in c language to compare two strings without strcmp function or any other function

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

int main()
{
   char str1[100], str2[100], ch;
   int i, check = 0, count = 0;

for(i=0; i<=100; i++)
{
str1[i] = str2[i] = 0;
}

   printf("Enter the first string: ");
   while(ch != 13)
   {
ch = getche();
str1[count] = ch;
count++;
   }
 
 
count = 0;
ch = NULL;
   printf("\nEnter the second string: ");
   while(ch != 13)
   {
ch = getche();
str2[count] = ch;
count++;
   }

for(i=0; i<=99; i++)
{
if(str1[i] != str2[i])
check = 1;
}


   if( check == 0 )
   {
      printf("\nEntered strings are equal.\n");
   }
   else
   {
      printf("\nEntered strings are not equal.\n");
   }
 
getch();
}

Write a program to Compare two strings by using strcmp function in c language

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

int main()
{
   char str1[100], str2[100];

   printf("Enter the first string: ");
   gets(str1);

   printf("Enter the second string: ");
   gets(str2);

   if( strcmp(str1, str2) == 0 )
   {
      printf("Entered strings are equal.\n");
   }
   else
   {
      printf("Entered strings are not equal.\n");
   }
 
getch();
}

Write a program to check whether the given number is palindrome or not?

Friday, 21 February 2014

Note: Polindrome is a number that is same if we reverse it. e.g: 151 reverse is 151



#include <iostream>
using namespace std;

int main()
{
    int number, ans = 0, temp;
    cout << "Enter a number: ";
    cin >> number;
    temp = number;
    while( number != 0)
    {
           ans = ans*10;
           ans = ans + number%10;
           number = number/10;
    }
    if(ans == temp)
    {
           cout << "Number is polindrome.";
    }
    else
    {
           cout << "Number is not polindrome.";
    }
    system("pause");
}

Write a program calculate tomorrow's date.

Friday, 19 April 2013


#include <stdio.h>
#include <conio.h>
struct date
{
       int day;
       int month;
       int year;
 
} today, tomorrow;

int main()
{
    char again;
    again:
     struct date;
     printf("Enter today's date: ");
     scanf("%d", &today.day);
     printf("Enter month: ");
     scanf("%d", &today.month);
     printf("Enter year:  ");
     scanf("%d", &today.year);

     switch(today.month)
     {
              case 1:
                   if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/%d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else
                   printf("Enter a valid date, month and year");              
              break;
              case 2:
                                      if(today.day==28)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<28 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                else
                   printf("Enter a valid date, month and year");
              break;
              case 3:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else
                   printf("Enter a valid date, month and year");
              break;
              case 4:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                     else
                   printf("Enter a valid date, month and year");
              break;
              case 5:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                               
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 6:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 7:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 8:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 9:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                   else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 10:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                  else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 11:
                                      if(today.day==30)
                                    {
                                             tomorrow.month=today.month+1;
                                             tomorrow.day=1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                    else if(today.day<30 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
              case 12:
                                      if(today.day==31)
                                    {
                                             tomorrow.month=1;
                                             tomorrow.day=1;
                                             tomorrow.year=today.year+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, tomorrow.year);    
                                    }
                   else if(today.day<31 && today.day>=1)
                                    {
                                             tomorrow.month=today.month;
                                             tomorrow.day=today.day+1;
                                             printf("Todays date is: %02d/%02d/%4d\n", today.day, today.month, today.year);
                                             printf("Tomorrow date will be: %02d/%02d/d", tomorrow.day, tomorrow.month, today.year);    
                                    }
                                      else
                   printf("Enter a valid date, month and year");
              break;
         
              default:
                      printf("Enter a valid date, month and year");              
                   
     }
     printf("\nWould you like to input again? y/n ");
     again=getche();
     printf("\n");
     if(again=='y' || again=='Y')
     {
                   goto again;        
     }

     getch();
}

Weather Temperature Conversion Calculator with heading

#include <stdio.h>

#include <conio.h>
main()
{
      int farh, cels;
      int lower=0, upper=300, step=20;
      farh=lower;
      printf("\"Weather Temperature Conversion Calculator\"\n\n");
      printf("Fahrenhiet\tCelsius\n");
      while(farh<=upper)
      {
                        cels=5*(farh-32)/9;
                        printf("%d\t\t%d\n", farh, cels);
                        farh=farh+step;                
      }
      getch();
}

Convert temprature from Farnhieght to CELSIES and Celcies to farnhiegh

#include <stdio.h>

#include <conio.h>

main()
{
      char scale;
      int temp=0;
      float ans;
      printf("Enter F to convert Farnhieght to CELSIES.\nEnter C to convert Celcies to farnhiegh.");
      scale=getche();
      printf("\n");
      if(scale=='F' || scale=='f')
      {
                    printf("Enter temperature in farnhieght: ");
                    scanf("%d", &temp);
                    ans=(temp-32)*5/9;
                    printf("%dF celsies is = %fC \n", temp, ans);                    
      }
      else
      {
                         printf("Enter temperature in celsies: ");
                    scanf("%d", &temp);
                    ans=temp*(9/5)+32;
                 
                    printf("%dC celsies is = %fF",temp, ans);
                    }
 getch();  
}

Get Maximum number from given numbers using function

#include <stdio.h>

#include <conio.h>
// Define of function
int max_return(int a, int b, int c, int d, int e)
{
int max;

max = a;

if(max < b) max = b;
if(max < c) max = c;
if(max < d) max = d;
if(max < e) max = e;

return (max);

}

int main(void)
{
int a[5];
int max, i;


for(i=1; i<=5; i++)
{
         printf("Enter number %d :", i);
         scanf("%d", &a[i]);
}
// Recall function
max = max_return(a[0], a[1], a[2], a[3], a[4]);
printf("Maximum = %d\n", max);


getch();
}

Write a Function to Check Number is +ve or -ve



#include <stdio.h>
#include <conio.h>
char _positive(int num)
{
     if(num<0)
     {
              printf("Number is -ve.");    
     }
     else if(num>0)
     {
              printf("Number is +ve.");      
     }
     else
 
     {
              printf("Number is -ve nor +ve.");      
     }
}
main()
{
      int num;
  printf("Enter a number: ");
  scanf("%d", &num);
  printf("%c", _positive(num));
  getch();
}

write a program to get 4 numbers from user and print greater number using IF statement.


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

int main()
{
    int a,b,c,d;
    printf(" value of a,b,c,d from user\n");
    printf("enter the a value\n");
    scanf("%d",&a);
      printf("enter the b value\n");
    scanf("%d",&b);
      printf("enter the c value\n");
    scanf("%d",&c);
      printf("enter the d value\n");
    scanf("%d",&d);
    printf("\t*********greater number is ********\n");
    if(a>b)
    if(a>c) 
    if(a>d){
                  printf("   a is greater \n");
                  }
                                         
                        if(b>a) 
                         if(b>c)
                           if(b>d){
                               printf("b is greater \n");}
                             
                                   if(c>a) 
                                    if(c>b)  
                                    if(c>d){
                                          printf("c is greater \n");}
                                              if(d>a)
                                                 if(d>b)
                                                  if (d>c)
                                               {
                                                      printf("  d is greater ");}                         
                  getch();
    
}

Write a program print squire pyramid using for loop.


Square Pyramid through For Loop:

#include<stdio.h>
#include<conio.h>
int main()
{
 int colmn,rows,r,c,m;
 printf("Enter no. of columns: ");
 scanf("%d", &colmn);
 printf("Enter no. of rows: ");
 scanf("%d", &rows);
 for(r=1; r<=colmn; r++)
 {
  if(r==1 || r==colmn)
      printf(" ");
  else
      printf("*");
 }
 printf("\n");
 for(c=1; c<=rows-2; c++)
 {
   printf("*");
   for(m=1; m<=colmn-2; m++)
       printf(" ");
   printf("*\n");
 }
 for(r=1; r<=colmn; r++)
 {
   if(r==1 || r==colmn)
       printf(" ");
   else
       printf("*");
 }
 getch();
 }
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *