Search This Blog

Sunday, 15 June 2014

C Programs for Solving simple Problems!

0 comments
Problem 1: 
Write a C program, to find whether the given number N is odd or even by using conditional operator.  (Don't use if elsestatement).


Solution:
#include <stdio.h>
int main(void) {
    int n;
    printf("Enter an Integer N:");
    scanf("%d", &n);
    n%2 ? printf("The Number %d is Odd", n) : printf("The Number %d is Even", n);
}

Problem 2:
Write a C program to find how many three digit numbers are available, that satisfies the following condition:
                 "The sum of first two digits is equal to the third digit (Example: 235, 437 etc.)"

Solution:

#include <stdio.h>
int main(void) {
    // your code goes here
    int i, n1, n2, n3, count=0;
    for(i=100; i<=999; i++)
    {
        n1 = (i/100);
        n2 = (i/10)%10;
        n3 = i%10;
        if ((n1+n2) == n3)
        {
                printf("%d\t", i);
                count = count + 1;
        }
    }
    printf("Total Number of 3 digit Numbers that satisfies the property is : %d", count);
} 

Output:

101 112 123 134 145 156 167 178 189 202 213 224 
235 246 257 268 279 303 314 325 336 347 358 369 
404 415 426 437 448 459 505 516 527 538 549 606 
617 628 639 707 718 729 808 819 909 
Total Number of 3 digit Numbers that satisfies the property is : 45
 
 
 
Problem 3: 
Write a C program to Read a string 'COMPUTER' and print the above string in the following format:

Solution:
#include <stdio.h>
int main(void) {
int i, j, k;
char a[50];
printf("Enter a String for Printing:");
scanf("%s", a);
printf("\n");
for(i=0; a[i] != '\0'; i++)
{
j = i+1;
for(k=0; k<j; k++)
printf("%c", a[k]);
printf("\n");
}
}
 
Output:
Enter a String for Printing:
COMPUTER
 
C
CO
COM
COMP
COMPU
COMPUT
COMPUTE
COMPUTER
 
 
Problem 4: 
Write a user defined function to find the sum of individual digits of a number N and write a main program to call the above function.

Solution:

#include <stdio.h>

int Sum_Digits(int n)
{
int sum=0;
if(n>0)
while (n>0)
{
sum = sum + (n%10);
n = n / 10;
}
else
return 0;
return sum;
}

void main()
{
int a, res;
printf("Enter an Integer having N digits:");
scanf("%d", &a);
res = Sum_Digits(a);
printf("\nThe sum of given Number %d is: %d", a, res);
}
  
Output:
Enter an Integer having N digits:
12345
The sum of given Number 12345 is: 15
 
 
Problem 5: 
Define a structure data type called 'time' containing three integer members - hour, minute and second.  Develop a program that would assign values to the individual members and display the time in the format hh:mm:ss (Example: 16:40:51)

Solution:

#include <stdio.h>

struct time
{
    int hh;
    int mm;
    int ss;
} t1;

int main(void) {
    printf("Enter the Hour, Minute and Seconds of Current Time one by one:");
    scanf("%d", &t1.hh);
    scanf("%d", &t1.mm);
    scanf("%d", &t1.ss);
    printf("\nThe given Time is %d:%d:%d", t1.hh, t1.mm, t1.ss);
    return 0;
}

  
Output:
Enter the Hour, Minute and Seconds of Current Time one by one:
16
40
51
The given Time is 16:40:51
 
 
Problem 6: 
Write a C program to read through an array of integer using pointer.  Also writ the code to scan through the array to find a particular value.

Solution:
#include <stdio.h>
#define N 50
int main(void) {
    int n, *p, a[N], i, m;
    printf("Enter the Number of elements you want to enter (Max-50):");
    scanf("%d", &n);
    
    if(n>50)
        n = 50;
        
    printf("\nEnter the elements of the Array one by one :\n");
    
    for(i=0; i<n; i++)
        scanf("%d", &a[i]);
    
    printf("\nThe elements of the Array are:");
    p = a;
    for(i=0; i<n; i++,p++)
        printf("%d\t", *p);
        
    printf("\n Enter an Integer for scanning through the Array:");
    scanf("%d", &m);
    p = a;
    for(i=0; i<n; i++,p++)
        if(m == *p)
        {
            printf("\nThe element %d exists in the Array",m);
            break;
        }
    if(i==n)
        printf("\nThe element %d does not exist in the Array",m);
    
    return 0;
}



Output 1:
Enter the Number of elements you want to enter (Max-50):5

Enter the elements of the Array one by one :
10
15
20 
25
30

The elements of the Array are:10    15    20    25    30   
 Enter an Integer for scanning through the Array:20

The element 20 exists in the Array


Output 2:
Enter the Number of elements you want to enter (Max-50):4

Enter the elements of the Array one by one :
25
50
75
100

The elements of the Array are:25    50    75    100   
 Enter an Integer for scanning through the Array:125

The element 125 does not exist in the Array

Leave a Reply