0% found this document useful (0 votes)
50 views7 pages

Vuram Success

The document contains several C program code snippets that demonstrate different programming concepts: 1. A program that prints a triangle pattern using nested for loops and whitespace formatting. 2. A program that prints a number pattern using nested for loops and incrementing/printing a number variable. 3. A program that divides two numbers without using the division operator through repeated subtraction. 4. A program that checks if a substring is present in a given string through nested for loops and flag variables. 5. A program that swaps two variables without using a third variable by adding and subtracting the two variables from each other in a specific order.

Uploaded by

Lakshmi Prabha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

Vuram Success

The document contains several C program code snippets that demonstrate different programming concepts: 1. A program that prints a triangle pattern using nested for loops and whitespace formatting. 2. A program that prints a number pattern using nested for loops and incrementing/printing a number variable. 3. A program that divides two numbers without using the division operator through repeated subtraction. 4. A program that checks if a substring is present in a given string through nested for loops and flag variables. 5. A program that swaps two variables without using a third variable by adding and subtracting the two variables from each other in a specific order.

Uploaded by

Lakshmi Prabha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1. #include <stdio.

h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

2.#include <stdio.h>
int main() {
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
1
2 3
4 5 6
7 8 9 10

3.DIVIDE WITHOUT DIVISION

#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1,num2,result=0;//variable declaration
// from the user - input from keyboard
printf("Enter two number for input: ");
scanf("%d %d",&num1,&num2);//Reading the input for num1 and num2
while(num1>=num2) {
num1=num1-num2;
result++;
}
printf("Division is: %d",result);
getch();
return 0;
}

4.SUBSTRING IS PRESENT OR NOT


#include<stdio.h>
 
int main()
{
char str[80], search[10];
int count1 = 0, count2 = 0, i, j, flag;
 
printf("Enter a string:");
gets(str);
printf("Enter search substring:");
gets(search);
while (str[count1] != '\0')
count1++;
while (search[count2] != '\0')
count2++;
for (i = 0; i <= count1 - count2; i++)
{
for (j = i; j < i + count2; j++)
{
flag = 1;
if (str[j] != search[j - i])
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
if (flag == 1)
printf("SEARCH SUCCESSFUL!");
else
printf("SEARCH UNSUCCESSFUL!");
 
return 0;
}

5.SWAP WITHOUT THIRD

1. #include<stdio.h>  
2.  int main()    
3. {    
4. int a=10, b=20;      
5. printf("Before swap a=%d b=%d",a,b);      
6. a=a+b;//a=30 (10+20)    
7. b=a-b;//b=10 (30-20)    
8. a=a-b;//a=20 (30-10)    
9. printf("\nAfter swap a=%d b=%d",a,b);    
10. return 0;  
11. } 

6.POSITION PROGRAM IN PHONE

7.SECOND SMALLEST ELEMENT IN ARRAY

#include <stdio.h>

void sort(int arr[], int n)

int i, j;

for (i = 0; i < n-1; i++)

for (j = 0; j < n-i-1; j++)


{

if (arr[j] > arr[j+1])

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

int main()

int n,i;

printf("\nEnter the number of elements : ");

scanf("%d",&n);

int arr[n];

printf("\nInput the array elements : ");

for(i = 0; i < n; i++)

scanf("%d",&arr[i]);

sort(arr, n);

printf("\nThe second smallest element is %d \n",arr[1]);

return 0;

8.DUPLICATE ELEMENTS IN ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int i,arr[20],j,no;
clrscr();
printf("Enter size of array: ");
scanf("%d",&no);
printf("Enter any %d elements in array: ",no);
for(i=0;i<no;i++)
{
scanf("%d",&arr[i]);
}
printf("Duplicate elements are: ");
for(i=0; i<no; i++)
{
for(j=i+1;j<no;j++)
{
if(arr[i]==arr[j])
{
printf("%d\n",arr[i]);
}
}
}
getch();
}

9.NEXT PRIME NUMBER


#include<stdio.h>
#include <stdlib.h>

int main()
{
int n;
scanf(“%d”,&n);

for(int i=n+1;i>0;i++)
{
int count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2){
printf(“%d”,i);
break;
}
}

10.ANAGRAM PROGRAM

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

int main (void) {


char s1[] = "recitals";
char s2[] = "articles";

char temp;

int i, j;
int n = strlen(s1);
int n1 = strlen(s2);

// If both strings are of different length, then they are not


anagrams

if( n != n1) {
printf("%s and %s are not anagrams! \n", s1, s2);
return 0;
}

// lets sort both strings first −

for (i = 0; i < n-1; i++) {


for (j = i+1; j < n; j++) {
if (s1[i] > s1[j]) {
temp = s1[i];
s1[i] = s1[j];
s1[j] = temp;
}
if (s2[i] > s2[j]) {
temp = s2[i];
s2[i] = s2[j];
s2[j] = temp;
}
}
}

// Compare both strings character by character

for(i = 0; i<n; i++) {


if(s1[i] != s2[i]) {
printf("Strings are not anagrams! \n", s1, s2);
return 0;
}
}

printf("Strings are anagrams! \n");


return 0;
}

11.PROGRAM TO FIND THE DATATYPE


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
# define MAX_INPUT 100
  
int main()
{
    // To read input
    char value[MAX_INPUT] = "";
  
    // To store numeric value of input if a 
    // number (float or integer)
    double temp;
  
    // To store integral value of input
    int n;
  
    // To store string value of input
    char str[MAX_INPUT] = "";
  
    // Precision for integer checking
    double val = 1e-12;
  
    fgets(value, 100, stdin); // Read input
  
    // Check for integers.
    if (sscanf(value, "%lf", &temp) == 1) 
    {
        n = (int)temp; // typecast to int.
        if (fabs(temp - n) / temp > val) 
            printf("The input is a floating point\n");        
        else 
            printf("The input is an integer\n");        
    }
  
    // Check for string 
    else if (sscanf(value, "%s", str) == 1)     
        printf("The input is a string\n");
      
    else // No match.    
        printf("input not recognized\n");    
}

You might also like