C - INTRO - PART004

 ID:6110 Print 1 to N (Using for Loop)


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int a;

    scanf("%d",&a);

    for(int i=1;i<a+1;i++){

        printf("% d",i);

    }


}



ID:6111 Print N to 1 ( Using for Loop)


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int N;

    scanf("%d",&N);

    for(int i=N;i>=1;i--)

   {

       printf("% d",i);

   }

}

ID:6161 Square of N (Using Function)


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int a;

    scanf("%d",&a);

    printf("%d",a*a);

    

}

ID:6160 Asterisk Pattern Printing


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int n,i,j;

    scanf("%d",&n);

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

        for(j=1;j<=i;++j){

            printf("* ");

        }

        printf("\n");

    }


}



ID:6159 NxN Grid with Asterisk - Pattern Printing


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int row,i,j;

    scanf("%d",&row);

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

        for(j=0;j<row;j++){

            printf("* ");

        }

        printf("\n");

    }


}


ID:6158 Pattern Printing - Integer Triangular Pattern


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int row,i,j,number=1;

    scanf("%d",&row);

    for(i=1;i<=row*2;++i){

        for(j=1;j<=i;++j){

            printf("% d",number);

            ++number;

        }

      

        printf("\n");

    }

   return 0;


}

ID:6164 Leap Year or Not (Using Function)


#include<stdio.h>

#include<stdbool.h>//boolean library


bool checkyear(int year)

{

    if(year % 400 ==0)//if a year is multiple of 400 then is a leap year

        return true;

        

    if(year % 100 == 0)//else it's multiple of 100 then it is not an leap year

        return false;

    

    if(year % 4==0)//if multiple of 4 it's also an leap year

        return true;

    return false;


}

int main()

{

    int year;

    scanf("%d",&year);

    checkyear(year)?printf("Leap Year"):printf("Not a Leap Year");

    return 0;

}

ID:6153 Skips the iteration


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int a,i;

    for(i=1;i<=20;i++)

    {

        if(i!=4)

            printf("% d",i);

    }

    


}


ID:6165 Find HCF of Two Integers


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int a,b,i,hcf;

    scanf("%d%d ",&a,&b);

    for(i=1; i<=a || i<=b ;i++)

    {

        if(a%i==0 && b%i==0)

        hcf=i;

    }

    printf("%d",hcf);

return 0;


}



ID:6167 Find LCM of Two Integers


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int a,b,step,max,lcm=0;

    scanf("%d%d ",&a,&b);

    if(a>b)

        max=step=a;

    else

        max=step=b;

        

    while(1)

    {

        if(max%a==0 &&max%b==0)

        {

            lcm=max;

            break;

        }

        max+=step;

    }

    printf("%d",lcm);

    return 0;



}

ID:6162 Check If Prime Number (Using Function)


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int n,i,flag=0;

    scanf("%d",&n);

    for(i=2;i<=n/2;++i)

    {

        if(n%i == 0)

        {

            flag = 1;

            break;

        }

    }

    

    if(flag==0)

        printf("Yes");

    else

        printf("No");

        

}


ID:6143 Convert Decimal to Binary


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int a[10],n,i;

    //enter the number to be converted

    scanf("%d",&n);

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

    {

        a[i]=n%2;

        n=n/2;

    }

    for(i=i-1;i>=0;i--)

    {

        printf("%d",a[i]);

    }

    return 0;


}


ID:6171 Convert Decimal to Base


#include<stdio.h>

#include<stdlib.h>


int main()

{

    int b,n,i,r,digit,p,count=0;

    char a[100];

    scanf("%d%d ",&n,&b);

    p=n;

    do{

        r=p%b;

        digit='0'+r;

        if(digit>'9')

            digit=digit+7;

        a[count]=digit;

        count++;

        p=p/b;

    }while(p!=0);

    for(i=count-1;i>=0;--i)

        printf("%c",a[i]);

    printf("\n");


}


ID:6163 Print the Count of Factors (Using Function)


#include<stdio.h>

#include<math.h>


int main()

{

    int num,i,count=0;

    scanf("%d",&num);

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

    {

        if(num%i == 0)

            count++;

             }

        printf("%d",count);

    }

ID:2588 Head Count - Birds and Animals


head=int(input())

legs=int(input())

animals=int(legs/2)-head

birds=head-animals

print(birds,animals)

ID:2590 Reverse Number Sign


a=int(input())

print(-(a))


ID:2583 Count of common characters in two strings


a=input()

b=input()

count=0

c=list(set(a)&set(b))

for i in c:

    count=count+1

print(count)


ID:2584 Reverse String Till Underscore


a=input()

if '_' in a:

    b=a.index("_")    #till _

    if b==len(a)-1:

        c=a[:len(a)-1]

        print(c[::-1]+'_')

    else:

        print(a[:b][::-1]+a[b:])

else:

    print(a[::-1])


Given Number Is Prime Number Or Not

#include<stdio.h>


void main()

{

    int x;

    scanf("%d",&x);


int i,m=0,flag=0;

m=x/2;

for(i=2;i<=m;i++)

{

    if(n%i==0){

        if(i%2!=0){

            printf("odd");

            

        }

        else{

            printf("even");

        }

        flag=1;

        break;

    }


if(flag==0)

printf("prime");

return 0;

}

String Modification     Remove all vowels and replace with '.'

} #include<stdio.h>

#include<stdlib.h>


int main()

{

    char str[101];

    scanf("%s",str);

    int index=0;

    while(str[index]!='\0'){

        if(str[index]!='a' && str[index]!='e' && str[index]!='i' &&str[index]!='o' && str[index]!='u'){

            printf(".%c",str[index]);

        }

        index++;

    }

    return 0;


}

To Reverse The String The String Value

#include<stdio.h>

#include<string.h>


int main()

{

    int n;

    scanf("%d",&n);

    char str[n][1001];

    for(int index=0;index<n;index++){

        scanf("%s",str[index]);

    }

    for(int index=0;index<n;index++)

    {

        for(int cmpindex=index+1;cmpindex<n;cmpindex++){

            if(strcmp(str[index],str[cmpindex])<0)

            {

                char temp[1001];

                strcpy(temp,str[index]);

                strcpy(str[index],str[cmpindex]);

                strcpy(str[cmpindex],temp);

            }

        }

        printf("%s\n",str[index]);

    }

}


Weight Of String..

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

int main()
{
    char str[101];
    int index=0,weight=0;
    scanf("%s",str);
    while(str[index]!='\0'){
        char ch = tolower(str[index]);
        weight+=ch-'a'+1;
        index++;
    }
    printf("%d",weight);

}
Space(s) after Punctuation Mark(s)

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

int main()
{
    char str[101];
    scanf("%[^\n]",str);
    for(int index=0;str[index]!='\0';index++){
        printf("%c",str[index]);
        if(str[index] == ',' || str[index] =='.'|| str[index] == '!' || str[index]=='?'){
            printf(" ");
        }
    }

}

Camel Case String

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

int main()
{
    char str[101];
    scanf("%s",str);
    int index;
    for(index=0;str[index]!='\0';index++){
        printf("%c",tolower(str[index]));
        if(isupper(str[index+1])){
            printf(" ");
        }
    }

}

Alphabet at Index

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

int main()
{
    char str[101];
    scanf("%s",str);
    int index;
    for(index=0;str[index]!='\0';index++)
    {
        printf("%c",str[str[index]-'a']);
    }

}
String Related

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

int main()
{
    int n;
    scanf("%d",&n);
    char str[101];
    for(int ctr=1;ctr<=n;ctr++){
        scanf("%s",str);
        int len = strlen(str);
        if(len<=10){
            printf("%s\n",str);
        }
        else{
            printf("%c%d%c\n",str[0],len-2,str[len-1]);
        }
    }

}
Money with Baba

#include <bits/stdc++.h>
 
using namespace std;

int main(int argc, char** argv)
{
    int amounthand,beggarcount;
    scanf("%d%d",&amounthand,&beggarcount);
    int initialamount=amounthand;
    int counter=1;
    while(counter<=beggarcount){
        initialamount=initialamount*2;
        counter++;
    }
    printf("%d",initialamount);


}
Sum of odd digits in a number ID:2295


import java.util.*;
public class Hello {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int sum,lastdigit;
        for(sum=0;a!=0;a/=10){
            lastdigit=a%10;
            if(lastdigit%2!=0)
                sum+=lastdigit;
        }
        System.out.printf("%d",sum);

    }
}Sum of N numbers  ID:2302


import java.util.*;
public class Hello {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int sum=0,b;
        int[] array=new int[100];
        for(int i=0;i<n;i++){
            array[i]=sc.nextInt();
        }
        for(int i=0;i<n;i++){
            sum+=array[i];
        }
        System.out.printf("%d",sum);
}
}Largest among N numbers ID:2303

import java.util.*;
public class Hello {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] array=new int[100];
        for(int i=0;i<n;i++){
            array[i]=sc.nextInt();
        }
        int temp;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if(array[i]>array[j])
                {
                    temp=array[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
            }
        }
        System.out.printf("%d",array[n-1]);
            
    }
}

Comments

Popular posts from this blog

alphabet matrix minimum sum path

C--Starter-part007---skillrack